One-class svm classification error : Classification metrics can't handle a mix of unknown and binary targets
One-class svm classification error : Classification metrics can't handle a mix of unknown and binary targets
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state = 2020, stratify=y)
X_train_user = X_train[y_train == 'ji2hwh']
X_train_attacker = X_train[y_train != 'ji2hwh']
outlier_prop = len(X_train_user) / len(X_train_attacker)
svm = OneClassSVM(kernel='rbf', nu=outlier_prop, gamma=0.000001)
svm.fit(X_train_user)
pred = svm.predict(X_test)
y_test[y_test == 'ji2hwh'] = 1
y_test[y_test != 1] = -1
print(accuracy_score(y_test, pred))
我在上面的代码中收到分类指标无法处理未知目标和二元目标混合错误。 'ji2hwh' 只是一个用户 ID,我正在考虑将其作为我的 one-class class 化的目标用户,而其余用户则是攻击者。 x 是一个特征向量,y 包含用户的 ID。我无法弄清楚为什么会出现此错误,因为变量 pred returns 具有 [-1,1] 值和 y_test 的 ndarray 似乎正确分配了适当的值,也在同一组值中[-1,1]。我该怎么做才能克服这个编译错误?
整个错误信息:
File "C:\Users\User\Desktop\MobileUserAuth\data_exploration.py", line 94, in <module>
print(accuracy_score(y_test, pred))
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 72, in inner_f
return f(**kwargs)
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\metrics\_classification.py", line 187, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\metrics\_classification.py", line 90, in _check_targets
raise ValueError("Classification metrics can't handle a mix of {0} "
ValueError: Classification metrics can't handle a mix of unknown and binary targets
我针对几天后发现的问题发布了一个解决方案,也许它可以帮助面临同样问题的人,因为我没有。
只需将y_test
类型转换为int
即可解决关于未知目标的错误。所以在调用 accuracy_score
类型之前:
y_test = y_test.astype('int')
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state = 2020, stratify=y)
X_train_user = X_train[y_train == 'ji2hwh']
X_train_attacker = X_train[y_train != 'ji2hwh']
outlier_prop = len(X_train_user) / len(X_train_attacker)
svm = OneClassSVM(kernel='rbf', nu=outlier_prop, gamma=0.000001)
svm.fit(X_train_user)
pred = svm.predict(X_test)
y_test[y_test == 'ji2hwh'] = 1
y_test[y_test != 1] = -1
print(accuracy_score(y_test, pred))
我在上面的代码中收到分类指标无法处理未知目标和二元目标混合错误。 'ji2hwh' 只是一个用户 ID,我正在考虑将其作为我的 one-class class 化的目标用户,而其余用户则是攻击者。 x 是一个特征向量,y 包含用户的 ID。我无法弄清楚为什么会出现此错误,因为变量 pred returns 具有 [-1,1] 值和 y_test 的 ndarray 似乎正确分配了适当的值,也在同一组值中[-1,1]。我该怎么做才能克服这个编译错误?
整个错误信息:
File "C:\Users\User\Desktop\MobileUserAuth\data_exploration.py", line 94, in <module>
print(accuracy_score(y_test, pred))
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 72, in inner_f
return f(**kwargs)
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\metrics\_classification.py", line 187, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\User\anaconda3\lib\site-packages\sklearn\metrics\_classification.py", line 90, in _check_targets
raise ValueError("Classification metrics can't handle a mix of {0} "
ValueError: Classification metrics can't handle a mix of unknown and binary targets
我针对几天后发现的问题发布了一个解决方案,也许它可以帮助面临同样问题的人,因为我没有。
只需将y_test
类型转换为int
即可解决关于未知目标的错误。所以在调用 accuracy_score
类型之前:
y_test = y_test.astype('int')