一种方法是按类分离数据集,并在 OCSVM 中单独训练每个类。这是一个返回内点 (1) 和异常值 (-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)