用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. Zookeeper配置安装

    单机模式: 1:修改hosts文件 ​ 2:下载解压zookeeper.tar文件 3:创建data目录和logs目录 进入zookeeper目录下创建 ​ 4:复制一份zoo_sample.cfg配 ...

  2. python中实现一个简单的进度条

    import time def progress(n,a): m = int(n / a * 100) x = int(n / a * 10) time.sleep(0.2) #只是为了让进度条明显 ...

  3. 操作socket笔记

    网络编程 1.tcp协议 #tcpserver #单纯一对一发 tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建tcp套接字 参 ...

  4. 专为简化 C 开发而设计的编程语言 Trad

    Trad 是一个专为简化 C 开发而设计的编程语言,它: 基于 C: 经编译器编译后能得到干净可读的标准 C 代码,其与 C 的关系就像 TypeScript 与 JavaScript 一样 专为 U ...

  5. POJ-2891-Strange Way to Express Integers(线性同余方程组)

    链接: https://vjudge.net/problem/POJ-2891 题意: Elina is reading a book written by Rujia Liu, which intr ...

  6. Jmeter逻辑控制之if控制器

    一.背景 在实际工作中,当使用Jmeter做性能脚本或者接口脚本时,有可能会遇到需要对不同的条件做不同的操作,基于这种诉求,在Jmeter中可使用if控制器来实现 二.实际操作 逻辑控制器位置: 在线 ...

  7. django 渲染模板与 vue 的 {{ }} 冲突解决方法

    如果不可避免的在同一个页面里既有 django 渲染又有 vue 渲染的部分,可有 2 种方式解决 方法一: 采用 vue 的 delimiters  分隔符. new Vue({ delimiter ...

  8. WebService操作

    webservice是一种服务器通信技术,封装了socket.

  9. bootstrap富文本编辑

    先把设定富文本框架 <div class="form-group"> <label class="col-sm-2 control-label" ...

  10. 后台(一)vue+element-ui(全局配置)

    vue init webpack    项目名称 npm install axios                    //先安装! npm install --save axios vue-ax ...