从此处跟进:Making ROC curve using python for multiclassification 我的代码如下:
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])
y_test_bi
和y_pred_bi
的形状都是(300,46)
,因为有46个类和300个测试数据点。
这两个矩阵的格式是每列代表一个类,由0
s或1
组成。
但是我收到了这个错误:
C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py:688: UserWarning: No positive samples in y_true, true positive value should be meaningless
warnings.warn("No positive samples in y_true, "
Traceback (most recent call last):
File "C:\Users\app\Documents\Python Scripts\gbc_classifier_test.py", line 153, in <module>
roc_auc[i] = auc(fpr[i], tpr[i])
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 172, in auc
x, y = check_arrays(x, y)
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 233, in check_arrays
_assert_all_finite(array)
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 27, in _assert_all_finite
raise ValueError("Array contains NaN or infinity.")
ValueError: Array contains NaN or infinity.
runfile('C:/Users/app/Documents/Python Scripts/gentleboost_c_class_jit.py', wdir=r'C:/Users/app/Documents/Python Scripts')