遇见错误: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 或者计算TP\FP\TN\FN出现错误
原因
这是因为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型
方法三(强烈推荐):
如果是在二分类任务中使用了one-hot编码,那么当 0 位置的概率 小于 1 位置的概率时 该值为 1 ,否则为0。如预测出来[0.6,0,8] 那么值为1,如预测出来[0.2,0,3] 值还是1,可如果以0.5为界限判断0、1显然是不合理的。因此使用以下代码更加合理:
yy_pred = model.predict(x_test, batch_size=b) # 预测出[[0.4,0.45],[0.8,0.3],[0.6,0.71]]
y_pred = np.argmax(yy_pred, axis=1) # 选择max值进行输出0,或1
总结:
使用上面三种方法预测的结果,就可以直接中sklearn的包进行求精确度等各种数据了
遇见错误:ValueError: Classification metrics can‘t handle a mix of binary and continuous targets相关推荐
- ValueError: Classification metrics can‘t handle a mix of binary and continuous targets
背景 在训练模型时想看一下混淆矩阵的形式,使用的是sklearn中的confusion_matrix函数,具体使用方式见官网:sklearn.metrics.confusion_matrix. 报错 ...
- ValueError: Classification metrics can‘t handle a mix of multiclass and unknown targets
ValueError: Classification metrics can't handle a mix of multiclass and unknown targets Keras显示召回率(c ...
- ValueError: Classification metrics can‘t handle a mix of continuous-multioutput and multiclass targe
1.问题描述: 使用scikit-learn的accuracy_score方法对logicRegression三分类模型预测准确率进行评估时报错: ValueError: Classification ...
- accuracy_score ValueError: Classification metrics can‘t handle a mix of multiclass and cont
ValueError: Classification metrics can't handle a mix of multiclass and cont 导读 本文意在提供解决此类方法的一个方法,仅参 ...
- Classification metrics can't handle a mix of continuous and multiclass targets
报错情景 今天笔者在使用sklearn的metrics.accuracy_score时遇到 "Classification metrics can't handle a mix of con ...
- 问题解决: ValueError: Can't Handle mix of binary and continuous
昨天做了随机森林回归预测,但是在做模型评估时报了如题所示的错误: ValueError: Can't Handle mix of binary and continuous 报错代码如下: preci ...
- tensorflow读取jpg格式图片报错 ValueError: Only know how to handle extensions: ['png']; with Pillow installed
当运行mpimg.imread("img.jpg")时,spyder 出现如下错误: ValueError: Only know how to handle extensions: ...
- raise ValueError(‘Only know how to handle PNG; with Pillow ‘ValueError: Only know how to handle PNG;
报错如下 raise ValueError('Only know how to handle PNG; with Pillow ' ValueError: Only know how to handl ...
- sklearn评估指标Classification metrics常用指标详解及小坑
sklearn评估指标Classification metrics常用指标详解及小坑 计算评估指标需要用到预测结果和真实标签,首先说一下获得预测结果的model.predict()和model.pre ...
最新文章
- 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击
- ubuntu16.04下Sublime Text3中文无法输入解决方法和创建桌面快捷方式
- 快速撑握C#知识点系列之(struct)结构
- Java 性能优化实战记录(1)---定位并分析耗cpu最多的线程
- Caffe代码导读(0):路线图
- bigdecimal不保留小数_金钱要使用BigDecimal数据类型(使用double的已经被公司开除了)...
- 只了解View的事件分发是不够的,来看下输入系统对事件的处理
- springMVC接收数据和响应返回
- 飞龙的计算机公开课推荐
- Vue.js 介绍及其脚手架工具搭建
- oracle form执行后左上角没出现oracle标记,Oracle FORM 开发技巧(1)
- JQuery 常用积累(五)Datetimepicker和Lodop
- mysql server再次安装失败_MySQL在windows上多次安装失败
- Fragment事务管理源码分析
- 关于idea中运行maven项目报错显示找不到包或符号的问题——终极方案
- FudanNLP 复旦分词系统 小试一把
- 韩昊20190912-3 词频统计
- 什么是线程安全?如何保证线程安全?
- ”记录集为只读“怎么解决?请高手帮忙看看。感激不尽……
- C语言求1到100的和