用随机森林计算 ROC AUC

时间:2021-03-15 13:57:13

标签: scikit-learn random-forest roc auc

我在多类问题中使用随机森林分类器。

rf = RandomForestClassifier(()
rf.fit(train_X, train_y)

然后进行预测:

pred = rf.predict(test_X)

那么我想像这样计算roc_auc_score

roc_value = roc_auc_score(test_y, pred, average='weighted', 
    multi_class='ovr',labels=[0,1,2,3,4])

但这会产生错误:

numpy.AxisError: axis 1 is out of bounds for array of dimension 1

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

如果您对 ROC AUC 感兴趣,pred 需要是概率,而不是预测标签。在您的情况下,这是一个多类分类问题,因此 pred 需要是形状为 (n_samples, n_classes) 的矩阵。要获得它,您只需要使用 predict_proba 而不是 predict

pred = rf.predict_proba(test_X)