获取每个交叉验证折叠的混淆矩阵

Nic*_*ick 3 pipeline confusion-matrix scikit-learn cross-validation

我正在使用 scikit-learn 通过交叉验证运行逻辑回归管道。我在下面的代码中从每个折叠中获得分数。我如何获得混淆矩阵?

clf = make_pipeline(MinMaxScaler(), LogisticRegression())
scores = cross_val_score(clf, X_train, y_train, cv=3)
Run Code Online (Sandbox Code Playgroud)

Bri*_*ell 6

我想你想要的是:

clf = make_pipeline(MinMaxScaler(), LogisticRegression())

from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
y_pred = cross_val_predict(clf, X_train, y_train, cv=3)
conf_mat = confusion_matrix(y, y_pred)
Run Code Online (Sandbox Code Playgroud)

来自scikit-learn 在线文档的3.1.1.2

函数 cross_val_predict 具有与 cross_val_score 类似的接口,但为输入中的每个元素返回该元素在测试集中时获得的预测。只能使用将所有元素仅分配给测试集一次的交叉验证策略(否则会引发异常)。

请注意,此计算的结果可能与使用 cross_val_score 获得的结果略有不同,因为元素以不同的方式分组。