如何修复ValueError:不支持多类格式

2022-04-05 00:00:00 python scikit-learn spyder roc

问题描述

这是我的代码,我试图计算ROC分数,但我遇到了ValueError的问题:不支持多类格式。我已经在找科学工具包学习了,但它没有帮助。最后,我仍然有ValueError:不支持多类格式。

这是我的代码

from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import confusion_matrix,zero_one_loss
from sklearn.metrics import classification_report,matthews_corrcoef,accuracy_score
from sklearn.metrics import roc_auc_score, auc


dtc = DecisionTreeClassifier()
bc = BaggingClassifier(base_estimator=dtc, n_estimators=10, random_state=17)
bc.fit(train_x, train_Y)
pred_y = bc.predict(test_x)

fprate, tprate, thresholds = roc_curve(test_Y, pred_y)
results = confusion_matrix(test_Y, pred_y)
error = zero_one_loss(test_Y, pred_y)
roc_auc_score(test_Y, pred_y)

FP = results.sum(axis=0) - np.diag(results)  
FN = results.sum(axis=1) - np.diag(results)
TP = np.diag(results)
TN = results.sum() - (FP + FN + TP)



print('
 Time Processing: 
',time.process_time())
print('
 Confussion Matrix: 
', results)
print('
 Zero-one classification loss: 
', error)
print('
 True Positive: 
', TP)
print('
 True Negative: 
', TN)
print('
 False Positive: 
', FP)
print('
 False Negative: 
', FN)
print ('
 The Classification report:
',classification_report(test_Y,pred_y, digits=6))
print ('MCC:', matthews_corrcoef(test_Y,pred_y))
print ('Accuracy:', accuracy_score(test_Y,pred_y))
print (auc(fprate, tprate))
print ('ROC Score:', roc_auc_score(test_Y,pred_y))

这是回溯


解决方案

来自文档的roc_curve:"注意:此实现仅限于二进制分类任务。"

您的标签分类(Y)是1还是0?如果不是,我认为您必须将pos_label参数添加到roc_curve调用中。

fprate, tprate, thresholds = roc_curve(test_Y, pred_y, pos_label='your_label')

或:

test_Y = your_test_y_array  # these are either 1's or 0's
fprate, tprate, thresholds = roc_curve(test_Y, pred_y)

相关文章