0

I have a multi-class dataset and am trying to use OneClassSVM() to classify each class.

from sklearn.svm import OneClassSVM
clf = OneClassSVM(gamma='auto').fit(df)
x_train,x_test,y_train,y_test = train_test_split(df,target,test_size=0.30, random_state=25)
inliers=df[clf.predict(df)==1]
outliers=df[clf.predict(df)==-1]

so I would like to know how can I train OneClassSVM() on each class?

Mario
  • 1,631
  • 2
  • 21
  • 51
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it *in the question itself*. See: "[How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)". – the Tin Man Feb 24 '22 at 23:36

1 Answers1

0

One way to do this is to separate the dataset by class and each class be trained separately in OCSVM. Here is a code that returns different evaluation metrics for inliers (1) and outliers (-1).

from sklearn.model_selection import train_test_split
from sklearn.svm import OneClassSVM
from sklearn.metrics import classification_report

def evaluation_one_class(preds_interest, preds_outliers):
  y_true = [1]*len(preds_interest) + [-1]*len(preds_outliers)
  y_pred = list(preds_interest)+list(preds_outliers)
  return classification_report(y_true, y_pred, output_dict=False)

def evaluate_model(X_train, X_test, X_outlier, model):
  
  one_class_classifier = model.fit(X_train)

  Y_pred_interest = one_class_classifier.predict(X_test)
  
  Y_pred_ruido = one_class_classifier.predict(X_outlier)

  print(evaluation_one_class(Y_pred_interest, Y_pred_ruido))


class_of_interest = ''

df_interest = df[df['target'] == class_of_interest]
df_outlier = df[df['target'] != class_of_interest]

df_train_int, df_test_int = train_test_split(df_interest,test_size=0.30, random_state=25) 

clf = OneClassSVM(gamma='auto')

evaluate_model(df_train_int, df_test_int, df_outlier, clf)