Search code examples
scikit-learnsklearn-pandas

To change the output class label value of a predict function in OneclassSVM


When I use OneClassSVM, we confirm that the results obtained by estimator.predict (X_test) derive the results as 1 and -1, respectively. Each means an outlier value and an internal value. But what I want is to label it with different values, like 0,1 not -1,1. I thought I could give a specific argument to predict to do so, but I couldn't find the search result I wanted.

from sklearn import OneClassSVM

check = OneClassSVM(kernel='rbf', gamma='scale')
check.fit(X_train, y_train)
check.predict(X_test)

I used the above code.


Solution

  • There is no built-in function to specify the labels. However, you can perform this operation using np.where():

    import numpy as np
    pred = np.array([-1, 1, -1, 1])
    
    np.where(pred==-1, 'outlier_value', 'internal_value')
    

    Output:

    array(['outlier_value', 'internal_value', 'outlier_value',
       'internal_value'], dtype='<U14')