Python如何绘制每个KFold混淆矩阵

时间:2018-04-19 01:01:45

标签: python scikit-learn logistic-regression confusion-matrix

如何编写For循环以显示每个KFold的混淆矩阵,以便我可以分析为什么某些召回分数为0。

以下代码为KFold

from sklearn import model_selection
from sklearn.model_selection import cross_val_score
kfold = model_selection.KFold(n_splits=6, random_state=19)
modelCV = LogisticRegression()
recall = model_selection.cross_val_score(modelCV, X_under, y_under, cv=kfold, scoring='recall')
print(recall)

以下代码用于绘制confusion_matrix

cnf_matrix = confusion_matrix(y_test,y_pred)
np.set_printoptions(precision=2)

print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()

0 个答案:

没有答案