简体   繁体   English

如何在 python 中计算一类 SVM 的准确度、F1 分数、召回率、精度和 EER?

[英]How to compute accuracy, F1-score, recall, precision and EER for one-class SVM in python?

I have a touch dataset collected by a user, this dataset is 1000x31 matrix of floating numbers.我有一个用户收集的触摸数据集,这个数据集是 1000x31 的浮点矩阵。 How can I apply one-class SVM classifier to detect an anomaly detection?如何应用一类 SVM 分类器来检测异常检测? How I can implement this in python and compute accuracy, recall, precision, F1-score, and EER?我如何在 python 中实现它并计算准确率、召回率、精度、F1 分数和 EER?

I started with this link but I couldn't compute the above performance metrics!!我从这个链接开始,但我无法计算上述性能指标!! https://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.html#sphx-glr-auto-examples-svm-plot-oneclass-py https://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.html#sphx-glr-auto-examples-svm-plot-oneclass-py

First, structure your data by finding out which of these 1000 examples are from the class that will be trained by OCSVM.首先,通过找出这 1000 个示例中的哪些来自将由 OCSVM 训练的类来构建您的数据。 Then separate into train and test for the OCSVM to use the train set for training and the test set for validating (generate metrics like accuracy, recall, F1, accuracy).然后将 OCSVM 分为训练和测试,以使用训练集进行训练和测试集进行验证(生成准确率、召回率、F1、准确率等指标)。

If all 1000 examples are of the class of interest, you will only be able to find the recall (TP/(TP+FN)) since you only have positive documents that can be classified as true positives (TP, ie, OCSVM got it right) or false negatives (FN, i.ie, the OCSVM was wrong).如果所有 1000 个示例都属于感兴趣的类别,则您将只能找到召回率 (TP/(TP+FN)),因为您只有可以归类为真正例的正例文档(TP,即 OCSVM 得到了它正确)或假阴性(FN,即 OCSVM 是错误的)。

To generate the precision (TP/(TP+FP)), F1, and accuracy (TP+TN/(TP+TN+FP+FN)), you need to create some examples that do not belong to the class of interest since with these contrary examples, OCSVM can generate true negatives (TN) and false positives (FP).要生成精度 (TP/(TP+FP))、F1 和准确度 (TP+TN/(TP+TN+FP+FN)),您需要创建一些不属于感兴趣类别的示例,因为有了这些相反的例子,OCSVM 可以生成真阴性 (TN) 和假阳性 (FP)。

Here is a code that returns the metrics given a set of documents of the class of interest for train and test, and a set of the non-class of interest for testing.这是一个代码,它返回度量给定一组用于训练和测试的感兴趣类别的文档,以及一组用于测试的非感兴趣类别的文档。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 scikit learn 计算多类案例的准确率、召回率、准确率和 f1 分数? - How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn? 如何使用交叉验证在多类数据集中对精度、召回率和 f1-score 进行评分? - how to score precision, recall and f1-score in a multi-class dataset using cross-validate? 打印包含每个查询的准确率、精确度、召回率和 F1 分数的字典 - Print dictionary containing accuracy, precision, recall and F1-score for each query 一类的精确召回pos_label python - precision recall pos_label python for one-class 如何从Sklearn分类报告中返回精确度,召回率和F1分数的平均分数? - How to return average score for precision, recall and F1-score from Sklearn Classification report? 如何显示进动、召回和 F1 分数? - How to show Precession, Recall and F1-Score? 完美的精度,召回率和f1得分,但预测不佳 - Perfect precision, recall and f1-score, yet bad prediction 如何计算神经网络模型中的准确率、召回率和 F1 分数? - How can I calculate precision, recall and F1-score in Neural Network models? Tensorflow:计算精度、召回率、F1 分数 - Tensorflow: Compute Precision, Recall, F1 Score 如何计算 python 中平衡逻辑回归 model 的精度、召回率和 f1 分数 - How to compute precision,recall and f1 score of an balanced logistic regression model in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM