简体   繁体   English

使用 sklearn 获得精度和召回率

[英]Getting Precision and Recall using sklearn

Using the code below, I have the Accuracy .使用下面的代码,我有Accuracy Now I am trying to现在我正在尝试

1) find the precision and recall for each fold (10 folds total) 1) 找到每个折叠的precisionrecall (总共 10 倍)

2) get the mean for precision 2)获得precisionmean

3) get the mean for recall 3)得到recallmean

This could be similar to print(scores) and print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) below.这可能类似于下面的print(scores)print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

Any thoughts?有什么想法吗?

import numpy as np
from sklearn import cross_validation
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import StratifiedKFold

iris = datasets.load_iris()
skf = StratifiedKFold(n_splits=10)
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validation.cross_val_score(clf, iris.data, iris.target, cv=10)
print(scores)  #[ 1. 0.93333333   1.  1. 0.86666667  1.  0.93333333   1.  1.  1.]
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) # Accuracy: 0.97 (+/- 0.09)

This is a bit different, because cross_val_score can't calculate precision/recall for non-binary classification, so you need to use recision_score, recall_score and make cross-validation manually.这有点不同,因为cross_val_score 无法计算非二元分类的precision/recall,所以需要使用recision_score、recall_score 并手动进行交叉验证。 Parameter average='micro' calculates global precision/recall.参数 average='micro' 计算全局精度/召回率。

import numpy as np
from sklearn import cross_validation
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import precision_score, recall_score

iris = datasets.load_iris()
skf = StratifiedKFold(n_splits=10)
clf = svm.SVC(kernel='linear', C=1)

X = iris.data
y = iris.target
precision_scores = []
recall_scores = []
for train_index, test_index in skf.split(X, y):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    y_pred = clf.fit(X_train, y_train).predict(X_test)
    precision_scores.append(precision_score(y_test, y_pred, average='micro'))
    recall_scores.append(recall_score(y_test, y_pred, average='micro'))

print(precision_scores)
print("Recall: %0.2f (+/- %0.2f)" % (np.mean(precision_scores), np.std(precision_scores) * 2))
print(recall_scores)
print("Recall: %0.2f (+/- %0.2f)" % (np.mean(recall_scores), np.std(recall_scores) * 2))
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix, recall_score, precision_score, 
                            accuracy_score, f1_score,roc_auc_score
          
def binary_classification_performance(y_test, y_pred):
    tp, fp, fn, tn = confusion_matrix(y_test, y_pred).ravel()
    accuracy = round(accuracy_score(y_pred = y_pred, y_true = y_test),2)
    precision = round(precision_score(y_pred = y_pred, y_true = y_test),2)
    recall = round(recall_score(y_pred = y_pred, y_true = y_test),2)
    f1_score = round(2*precision*recall/(precision + recall),2)
    specificity = round(tn/(tn+fp),2)
    npv = round(tn/(tn+fn),2)
    auc_roc = round(roc_auc_score(y_score = y_pred, y_true = y_test),2)


    result = pd.DataFrame({'Accuracy' : [accuracy],
                         'Precision (or PPV)' : [precision],
                         'Recall (senitivity or TPR)' : [recall],
                         'f1 score' : [f1_score],
                         'AUC_ROC' : [auc_roc],
                         'Specificty (or TNR)': [specificity],
                         'NPV' : [npv],
                         'True Positive' : [tp],
                         'True Negative' : [tn],
                         'False Positive':[fp],
                         'False Negative':[fn]})
    return result


binary_classification_performance(y_test, y_pred)

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

相关问题 sklearn 的 Plot 精度和召回率 - Plot precision and recall with sklearn 使用不同分类器的 sklearn precision_recall_curve 函数 - Using sklearn precision_recall_curve function with different classifiers 精度,召回率,F1得分与sklearn相等 - Precision, recall, F1 score equal with sklearn Sklearn Precision 和召回给出错误的值 - Sklearn Precision and recall giving wrong values Keras分类器的Sklearn精度,召回率和FMeasure度量 - Sklearn Metrics of precision, recall and FMeasure on Keras classifier sklearn.metrics.precision_recall_curve:为什么精度和重新调用返回的数组而不是单个值 - sklearn.metrics.precision_recall_curve: Why are the precision and recall returned arrays instead of single values Sklearn:使用precision_score 和recall_score 计算时召回率和精度是否交换? - Sklearn: are recall and precision swapped when are computed with precision_score and recall_score? 如何使sklearn模型达到预定的精度或召回某个类? - How to make sklearn model reach a predefine precision or recall on some class? 与 sklearn 一起交叉验证精度、召回率和 f1 - Cross-validate precision, recall and f1 together with sklearn 在sklearn中进行超参数调整后,查找模型的准确性,精确度和召回率 - Finding accuracy, precision and recall of a model after hyperparameter tuning in sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM