[英]Can any sklearn module return average precision and recall scores for negative class in k-fold cross validation?
I am trying to get the mean of precision and recall for BOTH positive and negative class in a 10-fold cross validation.我试图在 10 倍交叉验证中获得正类和负类的精度和召回率的平均值。 My model is a binary classifier.我的模型是一个二元分类器。
I ran the codes below and unfortunately it only returned the mean precision and recall for the positive class.我运行了下面的代码,不幸的是它只返回了正类的平均精度和召回率。 How can I tell the algorithm to return the mean precision and recall scores for the negative class as well?我怎样才能告诉算法返回负类的平均精度和召回分数?
from sklearn.metrics import make_scorer, accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import cross_validate
scoring = {'accuracy' : make_scorer(accuracy_score),
'precision' : make_scorer(precision_score),
'recall' : make_scorer(recall_score),
'f1_score' : make_scorer(f1_score)}
results = cross_validate(model_unbalanced_data_10_times_weight, X, Y, cv=10, scoring=scoring)
np.mean(results['test_precision'])
np.mean(results['test_recall'])
I've also tried printing the classification report using the command " classification_report(y_test, predictions)
" which resulted in the printout in screenshot below.我还尝试使用命令“ classification_report(y_test, predictions)
”打印分类报告classification_report(y_test, predictions)
这导致打印输出在下面的屏幕截图中。 However, I believe the precision/recall scores from the classification report is based on 1 run only and not the average over 10 folds (correct me if I am wrong).但是,我相信分类报告中的精度/召回分数仅基于 1 次运行,而不是超过 10 倍的平均值(如果我错了,请纠正我)。
Based on our discussion above, I do believe that computing predictions for every cv fold and computing cross_validation_report
on them should be the right way to go.基于我们上面的讨论,我确实相信计算每个 cv 折叠的预测并计算它们的cross_validation_report
应该是正确的方法。 Results should now take the number of cv folds into account:结果现在应该考虑 cv 折叠的数量:
>>> from sklearn.metrics import classification_report
>>> from sklearn.datasets import load_iris
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.model_selection import cross_val_predict
>>>
>>> iris = load_iris()
>>>
>>> rf_clf = RandomForestClassifier()
>>>
>>> preds = cross_val_predict(estimator=rf_clf,
... X=iris["data"],
... y=iris["target"],
... cv=15)
>>>
>>> print(classification_report(iris["target"], preds))
precision recall f1-score support
0 1.00 1.00 1.00 50
1 0.92 0.94 0.93 50
2 0.94 0.92 0.93 50
accuracy 0.95 150
macro avg 0.95 0.95 0.95 150
weighted avg 0.95 0.95 0.95 150
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.