且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

10个交叉折叠的混淆矩阵-如何做 pandas 数据框df

更新时间:2023-12-03 23:27:46

来自 cross_validate的帮助页面,它不会返回用于交叉验证的索引.您需要使用示例数据集从(分层)KFold访问索引:

From the help page for cross_validate it doesn't return the indexes used for cross-validation. You need to access the indices from the (Stratified)KFold, using an example dataset:

from sklearn import datasets, linear_model
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_predict
from sklearn.ensemble import RandomForestClassifier

data = datasets.load_breast_cancer()
X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.34, random_state=66)

skf = StratifiedKFold(n_splits=10,random_state=111,shuffle=True)
skf.split(X_train,y_train)

rfc = RandomForestClassifier(n_estimators=200, random_state=39, max_depth=4)
y_pred = cross_val_predict(rfc, X_train, y_train, cv=skf)

我们应用 cross_val_predict 来获取所有预测:

We apply cross_val_predict to get all the predictions:

y_pred = cross_val_predict(rfc, X, y, cv=skf)

然后使用索引将该y_pred拆分为每个混淆矩阵:

Then use the indices to split this y_pred to each confusion matrix:

mats = []
for train_index, test_index in skf.split(X_train,y_train):
    mats.append(confusion_matrix(y_train[test_index],y_pred[test_index]))
    

看起来像这样:

mats[:3]

[array([[13,  2],
        [ 0, 23]]),
 array([[14,  1],
        [ 1, 22]]),
 array([[14,  1],
        [ 0, 23]])]

检查矩阵列表和总和的和是否相同:

Check that the addition of the matrices list and total sum is the same:

np.add.reduce(mats)

array([[130,  14],
       [  6, 225]])

confusion_matrix(y_train,y_pred)

array([[130,  14],
       [  6, 225]])