栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

遇见错误:ValueError: Classification metrics can‘t handle a mix of binary and continuous targets

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

遇见错误:ValueError: Classification metrics can‘t handle a mix of binary and continuous targets

tensorflow 遇见错误ValueError: Classification metrics can’t handle a mix of binary and continuous targets keras 遇见错误:ValueError: Classification metrics can’t handle a mix of binary and continuous targets 使用sklearn中的函数 confusion_matrix、classification_report 或者 accuracy_score 或者计算TPFPTNFN出现错误

原因

这是因为sklearn函数的输入参数的数据类型不匹配导致的,有可能是输入的y_true为[0,1,1,0,1]的int型数据,而y_predict是[0.8,0.9,0.1,0.5]这样的概率数据。因此需要把概率数据转换为整型数据即可。可能使用了**model.predict_proba()**函数进行了预测

解决

方法一:
在预测时使用:

y_predict = model.predict_classes(x_test)  # 输出 [0,1,1,1]格式的数据
y_scores = model.predict_proba(x_test)# 输出[ 负的概率], 正的概率] 格式的数据 根据预测任务的不同而不同

model是训练的模型。
这种方法就是将预测值与预测概率分开。
注意! 这种方法对有些编码方式可能不适用,请尽量使用方法二!

方法二(推荐):
使用model.predict_proba()预测之后将结果转化为int类型的数据

y_scores = model.predict_proba(x_test)# 输出[ 负的概率], 正的概率] 格式的数据 
y_pred = y_socres[:,1] # 取第二列 正的概率,根据每个人的数据不同而不同!
y_pred = np.around(y_pred,0).astype(int) # .around()这个时四舍五入的函数 第二个参数0表示保留0位小数,也就只保留整数!! .astype(int) 将浮点数转化为int型

总结:
使用上面两种方法预测的结果,就可以直接中sklearn的包进行求精确度等各种数据了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号