简体   繁体   English

如何在scikit-learn中使用k折交叉验证来获得每折的精确召回率?

[英]How can I use k-fold cross-validation in scikit-learn to get precision-recall per fold?

Let's say I have this scenario: 假设我有这种情况:

from sklearn import model_selection
from sklearn.linear_model import LogisticRegression

kfold = model_selection.KFold(n_splits=5, random_state=7)
acc_per_fold = model_selection.cross_val_score(LogisticRegression(),
               x_inputs, np.ravel(y_response), cv=kfold, scoring='accuracy')

What else can I get from model_selection.cross_val_score() ? 我还能从model_selection.cross_val_score()得到什么? Is there a way to see what happens inside every actual fold? 有没有办法查看每个实际折痕内部发生的情况? Can I get precision-recall per fold? 我可以得到每折的精确召回率吗? Predicted values? 预测值? How about using a trained model from a fold to make predictions on unseen data? 如何使用训练有素的模型来对看不见的数据进行预测?

You can use the cross_validate function to see what happens in each fold. 您可以使用cross_validate函数查看每折的情况。

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_validate
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, recall_score, roc_auc_score, precision_score

X, y = make_classification(
    n_classes=2, class_sep=1.5, weights=[0.9, 0.1],
    n_features=20, n_samples=1000, random_state=10
)
clf = LogisticRegression(class_weight="balanced")
scoring = {'accuracy': 'accuracy',
           'recall': 'recall',
           'precision': 'precision',
           'roc_auc': 'roc_auc'}
cross_val_scores = cross_validate(clf, X, y, cv=3, scoring=scoring)

The output is the following, 输出如下:

{'fit_time': array([ 0.        ,  0.        ,  0.01559997]),
 'score_time': array([ 0.01559997,  0.        ,  0.        ]),
 'test_accuracy': array([ 0.9251497 ,  0.95808383,  0.93674699]),
 'test_precision': array([ 0.59183673,  0.70833333,  0.63636364]),
 'test_recall': array([ 0.85294118,  1.        ,  0.84848485]),
 'test_roc_auc': array([ 0.96401961,  0.99343137,  0.96787271]),
 'train_accuracy': array([ 0.96096096,  0.93693694,  0.95209581]),
 'train_precision': array([ 0.73033708,  0.62376238,  0.69148936]),
 'train_recall': array([ 0.97014925,  0.94029851,  0.95588235]),
 'train_roc_auc': array([ 0.99426906,  0.98509954,  0.99223039])}

So what happened in the first fold ? 那么第一折发生了什么?

FOLD, METRIC = (0, 'test_precision')
cross_val_scores[METRIC][FOLD]

And is the precision score stable ? precision score稳定?

np.std(cross_val_scores[METRIC])

暂无
暂无

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

相关问题 Scikit-Learn中的分层标记K-fold交叉验证 - Stratified Labeled K-Fold Cross-Validation In Scikit-Learn scikit-learn中的随机分层k-fold交叉验证? - Randomized stratified k-fold cross-validation in scikit-learn? 在scikit-learn中使用交叉验证时绘制Precision-Recall曲线 - Plotting Precision-Recall curve when using cross-validation in scikit-learn 任何 sklearn 模块都可以在 k 折交叉验证中返回负类的平均精度和召回分数吗? - Can any sklearn module return average precision and recall scores for negative class in k-fold cross validation? 使用 scikit-learn 运行 k-fold 后如何访问数据集? - How do I access the datasets after running k-fold with scikit-learn? scikit-learn:为什么这个 2 折交叉验证图看起来像 4 折交叉验证? - scikit-learn: Why does this 2-fold cross-validation figure looks like 4-fold cross-validation? 如何在回归神经网络中使用 k 折交叉验证而不是 train_test_split - How to use k-fold cross-validation instead of train_test_split for Regression Neural Network k-fold分层交叉验证与不平衡类 - k-fold stratified cross-validation with imbalanced classes 如何在 sklearn 中使用 K 折交叉验证进行负二项式回归? - How can I use K-fold cross validation for negative binomial regression in sklearn? 对多类问题执行 K 折交叉验证,评分 = 'f1 or Recall or Precision' - performing K-fold Cross Validation with scoring = 'f1 or Recall or Precision' for multi-class problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM