用sklearn的DecisionTreeClassifer训练模型,然后用roc_auc_score计算模型的auc。代码如下

clf = DecisionTreeClassifier(criterion='gini', max_depth=6, min_samples_split=10, min_samples_leaf=2)
clf.fit(X_train, y_train)
y_pred = clf.predict_proba(X_test)
roc_auc = roc_auc_score(y_test, y_pred)

报错信息如下

/Users/wgg/anaconda/lib/python2.7/site-packages/sklearn/metrics/ranking.pyc in _binary_clf_curve(y_true, y_score, pos_label, sample_weight)
297 check_consistent_length(y_true, y_score)
298 y_true = column_or_1d(y_true)
--> 299 y_score = column_or_1d(y_score)
300 assert_all_finite(y_true)
301 assert_all_finite(y_score) /Users/wgg/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.pyc in column_or_1d(y, warn)
560 return np.ravel(y)
561
--> 562 raise ValueError("bad input shape {0}".format(shape))
563
564 ValueError: bad input shape (900, 2)

目测是你的y_pred出了问题,你的y_pred是(900, 2)的array,也就是有两列。

因为predict_proba返回的是两列。predict_proba的用法参考这里

简而言之,你上面的代码改成这样就可以了。

y_pred = clf.predict_proba(X_test)[:, 1]
roc_auc = roc_auc_score(y_test, y_pred)

原文:http://sofasofa.io/forum_main_post.php?postid=1001678

sklearn里计算roc_auc_score,报错ValueError: bad input shape的更多相关文章

  1. 标记编码报错ValueError: bad input shape ()

    <Python机器学习经典实例>2.9小节中,想自己动手实践汽车特征评估质量,所以需要对数据进行预处理,其中代码有把字符串标记编码为对应的数字,如下代码 input_data = ['vh ...

  2. keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'

    在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor w ...

  3. matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

    Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...

  4. 安装 r 里的 igraph 报错

    转载来源:http://genek.tv/article/40 1186 0 0 安装 r 里的 igraph 报错: foreign-graphml.c: In function ‘igraph_w ...

  5. dbfread报错ValueError错误解决方法

    问题 我在用dbfread处理.dbf数据的时候出现了报错 ValueError("could not convert string to float: b'.'",) 然后查找. ...

  6. moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法

    ☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...

  7. Linux部署Django:报错 nohup: ignoring input and appending output to ‘nohup.out’

    一.部署 Django 到远程 Linux 服务器 利用 xshell 通过 ssh 连接到 Linux服务器,常规的启动命令是 python3 manage.py runserver 但是,关闭 x ...

  8. tensorflow-TFRecord报错ValueError: Protocol message Feature has no "feature" field.

    编写代码用TFRecord数据结构存储数据集信息是报错:ValueError: Protocol message Feature has no "feature" field.或和 ...

  9. datetime.strptime格式转换报错ValueError

    今天遇到一个报错:ValueError: time data '2018-10-10(Wednesday) AM0:50' does not match format '%Y-%m-%d(%A) %p ...

随机推荐

  1. MySql 8.0服务端安装后,用navicat12连接时报2059错误_解决

    先看连接错误 连接失败:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: .... 解决方法: 进入MySQ ...

  2. Python-共享引用

    A会改变么? 下面三小段代码,A的值都会改变么? >>> A = "spam" >>> B = A >>> B = " ...

  3. Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)

    http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...

  4. Nginx入门(三)——正向代理

    server { resolver 114.114.114.114; #指定DNS服务器IP地址 listen 443; location / { proxy_pass https://$host$r ...

  5. python多线程实现ping多个ip

    #!/usr/bin/env python # -*- coding:utf-8 -*- import subprocess import logging import datetime import ...

  6. 网络编程---scoket使用,七层协议,三次挥手建连接,四次挥手断连接

    目录 == 网络编程 == 软件开发架构 网络编程 互联网协议 TCP协议的工作原理 Socket == 网络编程 == 软件开发架构 开发软件 必须要开发一套 客户端与服务端 客户端与服务端的作用 ...

  7. MyBatis中jdbcType=INTEGER、VARCHAR作用

    Mapper.xml中 pid = #{pid,jdbcType=INTEGER} pid = #{pid} 都可以用 Mybatis中什么时候应该声明jdbcType? 当Mybatis不能自动识别 ...

  8. 【HTTP】图解HTTPS

    我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL ...

  9. Python学习之--基础语法

    一.定义 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 二.Python变量的命名规则 1. 变量名只能包含字母.数字和下划线: 2. 变量名不能包含空格: 3. 不要将Py ...

  10. [Luogu] 运输问题 -- 00

    https://www.luogu.org/problemnew/show/4015 #include <bits/stdc++.h> #define gc getchar() using ...