使用python制作ROC曲线进行多分类 [英] Making ROC curve using python for multiclassification

查看:478
本文介绍了使用python制作ROC曲线进行多分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从此处跟踪:我想为我的46个班级中的每个班级绘制ROC曲线.我有300个测试样本,已对其运行分类器以进行预测.

I want to draw ROC curves for each of my 46 classes. I have 300 test samples for which I've run my classifier to make a prediction.

y_test是真实的类,而y_pred是我的分类器预测的结果.

y_test is the true classes, and y_pred is what my classifier predicted.

这是我的代码:

    from sklearn.metrics import confusion_matrix, roc_curve, auc
    from sklearn.preprocessing import label_binarize
    import numpy as np

    y_test_bi = label_binarize(y_test, classes=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,2,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,3,40,41,42,43,44,45])
    y_pred_bi = label_binarize(y_pred, classes=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,2,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,3,40,41,42,43,44,45])
    # Compute ROC curve and ROC area for each class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(2):
        fpr[i], tpr[i], _ = roc_curve(y_test_bi, y_pred_bi)
        roc_auc[i] = auc(fpr[i], tpr[i])

但是,现在出现以下错误:

However, now I'm getting the following error:

Traceback (most recent call last):
  File "C:\Users\app\Documents\Python Scripts\gbc_classifier_test.py", line 152, in <module>
    fpr[i], tpr[i], _ = roc_curve(y_test_bi, y_pred_bi)
  File "C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 672, in roc_curve
    fps, tps, thresholds = _binary_clf_curve(y_true, y_score, pos_label)
  File "C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 505, in _binary_clf_curve
    y_true = column_or_1d(y_true)
  File "C:\Users\app\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 265, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (300L, 46L)

推荐答案

roc_curve采用形状为[n_samples]的参数(

roc_curve takes parameter with shape [n_samples] (link), and your inputs (either y_test_bi or y_pred_bi) are of shape (300, 46). Note the first

我认为问题是y_pred_bi是通过调用clf.predict_proba(X)创建的一系列概率(请确认这一点).由于您的分类器已经过全部46个类别的训练,因此它为每个数据点输出46维向量,而label_binarize对此无能为力.

I think the problem is y_pred_bi is an array of probabilities, created by calling clf.predict_proba(X) (please confirm this). Since your classifier was trained on all 46 classes, it outputs a 46-dimensional vectors for each data point, and there is nothing label_binarize can do about that.

我知道有两种解决方法:

I know of two ways around this:

    通过在clf.fit()之前调用label_binarize来训练46个 binary 分类器,然后计算ROC曲线
  1. 切片300 x 46输出数组的每一列,并将其作为第二个参数传递给roc_curve.我假设y_pred_bi包含概率
  2. ,这是我的首选方法
  1. Train 46 binary classifiers by invoking label_binarize before clf.fit() and then compute ROC curve
  2. Slice each column of the 300-by-46 output array and pass that as the second parameter to roc_curve. This is my preferred approach by I am assuming y_pred_bi contains probabilities

这篇关于使用python制作ROC曲线进行多分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆