且构网

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

如何使用K折交叉验证来计算准确性和混淆矩阵?

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

为了准确起见,我将使用函数 cross_val_score 来实现您的工作寻找。它输出30个验证精度的列表,然后您可以计算它们的平均值,标准偏差等,并创建某种类型的置信区间(平均值+-2 * std)

For accuracy, I would use the function cross_val_score that does exactly what you are looking for. It outputs a list of 30 validation accuracies and you can then compute their mean, standard deviation, etc and create some kind of a confidence interval (mean +- 2*std) .

由于不能将混淆矩阵视为性能指标(不是单个数字而是矩阵),我建议创建一个列表,然后迭代地将其附加一个相应的验证混淆矩阵(当前只打印它) 。最后,您可以使用此列表提取很多有趣的信息。

Since confusion matrix cannot be seen as a performance metric (not a single number but a matrix) I would recommend creating a list and then iteratively just append it with a corresponding validation confusion matrix (currently you just print it). At the end, you can use this list to extract a lot of interesting information.

更新:

...
...
cm_holder = []
for train_index, test_index in kf.split(previsores):
    X_train, X_test = previsores[train_index], previsores[test_index]
    y_train, y_test = classe[train_index], classe[test_index]

    logmodel.fit(X_train, y_train)
    cm_holder.append(confusion_matrix(y_test, logmodel.predict(X_test))))

请注意, len(cm_holder) = 30,每个元素都是一个 shape =(n_classes,n_classes)的数组。

Note that the len(cm_holder) = 30 and each of the elements is an array of shape=(n_classes, n_classes).