简体   繁体   中英

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))

I am getting Classification metrics can't handle a mix of unknown and binary targets error in the above code. 'ji2hwh' is just a userID who I am considering as my target user for my one-class classification and the rest of the users as attackers. x is a feature vector and y contains the user's ID. I cannot figure out why I am getting this error, since the variable pred returns a ndarray with [-1,1] values and y_test seems correctly assigned the appropriate values,also in the same set of values [-1,1]. What can I do to overcome this compile error?

The whole error message:

  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

I am posting one solution to my problem I found after some days, perhaps it could help someone facing the same issue and since I didn't got one.

Just simply type cast the y_test to int and the error about the unknown target will be solved. So before calling the accuracy_score type:

y_test = y_test.astype('int')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM