首页 > 解决方案 > 错误:不支持多类格式,AUROC

问题描述

我想计算 ROC 分数,但错误被标记为“不支持多类格式”我不太确定为什么会出现错误,因为我已经将 y 更改为二进制。

y = dat['category'].map( {'sport': 0, 'business': 1, 'politics': 2, 'tech': 3, 'entertainment': 4} ).astype(int)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=2)

我已经建立了分类器并计算了概率

from sklearn.metrics import roc_curve, roc_auc_score

r_auc = roc_auc_score(y_test, r_probs)
lr_auc = roc_auc_score(y_test, lr_probs)
rf_auc = roc_auc_score(y_test, rf_probs)
gb_probs = roc_auc_score(y_test, gb_probs)

print("Random Prediction: AUROC = %.3f" %(r_auc))
print("Logistic Regression: AUROC = %.3f" %(lr_auc))
print("Random Forest: AUROC = %.3f" %(rf_auc))
print("Gradient Boosting: AUROC = %.3f" %(gb_probs))

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-230-4ba9c6da7108> in <module>
      2 from sklearn.metrics import roc_curve, roc_auc_score
      3 
----> 4 r_auc = roc_auc_score(y_test, r_probs)
      5 lr_auc = roc_auc_score(y_test, lr_probs)
      6 rf_auc = roc_auc_score(y_test, rf_probs)

~\Anaconda3\lib\site-packages\sklearn\metrics\ranking.py in roc_auc_score(y_true, y_score, average, sample_weight, max_fpr)
    353     return _average_binary_score(
    354         _binary_roc_auc_score, y_true, y_score, average,
--> 355         sample_weight=sample_weight)
    356 
    357 

~\Anaconda3\lib\site-packages\sklearn\metrics\base.py in _average_binary_score(binary_metric, y_true, y_score, average, sample_weight)
     71     y_type = type_of_target(y_true)
     72     if y_type not in ("binary", "multilabel-indicator"):
---> 73         raise ValueError("{0} format is not supported".format(y_type))
     74 
     75     if y_type == "binary":

ValueError: multiclass format is not supported

有人可以帮忙吗:(

标签: pythonjupyter-notebookroc

解决方案


推荐阅读