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

时间:2018-10-19 12:11:48

标签: python machine-learning scikit-learn classification cross-validation

假设我有这种情况:

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')

我还能从model_selection.cross_val_score()得到什么?有没有办法查看每个实际折痕内部发生的情况?我可以每折获得精确召回率吗?预测值?如何从训练有素的模型中对看不见的数据进行预测呢?

1 个答案:

答案 0 :(得分:4)

您可以使用xtrainnorm = normalizer.transform(xtrain) xtestnorm = normalizer.transform(Xtest) 函数查看每折的情况。

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)

那第一折发生了什么?

{'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])}

FOLD, METRIC = (0, 'test_precision') cross_val_scores[METRIC][FOLD] 稳定吗?

precision score