背景

在训练模型时想看一下混淆矩阵的形式,使用的是sklearn中的confusion_matrix函数,具体使用方式见官网:sklearn.metrics.confusion_matrix

报错

conf_m = confusion_matrix(Ytest_vector, predtest)

ValueError: Classification metrics can’t handle a mix of binary and continuous targets

原因

分别打印 Ytest_vector 和 predtest两个变量,可以看到都是array。打印predtest可以很明显看到数据为小数且都不同,应该是probability的形式。 Ytest_vector 变量很难知道具体的每行数据情况,因此使用numpy中的unique函数来确定无重复的元素是什么,可以看到只有0或者1。

np.unique(Ytest_vector) # 输出 0 和 1

解决

把probability的形式转换成0/1的格式,直接使用round()函数即可。

conf_m = confusion_matrix(Ytest_vector, predtest.round())

这样就可以看到混淆矩阵啦~🎉
这样就可以看到混淆矩阵啦

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐