Scikit ROC auc增加了ValueError:在y_true中只有一个类。ROC AUC评分在这种情况下没有定义

时间:2021-04-25 20:34:31

Trying to create a ROC curve.

尝试创建一个ROC曲线。

model = RandomForestClassifier(500, n_jobs = -1);
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

probas = model.predict_proba(X_test)[:, 1]
precision = metrics.precision_score(y_test, y_pred)    # returns 0.72

recall = metrics.recall_score(y_test.values, y_pred)   # returns 0.35
y_test.shape                                           # (39257, 1)

auc = metrics.roc_auc_score(y_test, probas)            # fails.

ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.

ValueError:在y_true中只有一个类。在这种情况下,ROC AUC评分没有定义。

1 个解决方案

#1


4  

Ended up answering my own question:

最后回答了我自己的问题:

Had imported y_test as a pandas DataFrame instead of a Series (had saved it using to_csv and imported elsewhere with from_csv).

将y_test导入为熊猫DataFrame,而不是一个系列(使用to_csv保存它,并从其他地方导入from_csv)。

This confused scikit on the ROC curves, but it seems quite happy with that everywhere else.

ROC曲线上的这个模糊的scikit,但是它似乎和其他任何地方都很相似。

I'll leave this here in the (unlikely) case someone runs into the same thing.

我把这个放在这里,以防有人遇到同样的事情。

#1


4  

Ended up answering my own question:

最后回答了我自己的问题:

Had imported y_test as a pandas DataFrame instead of a Series (had saved it using to_csv and imported elsewhere with from_csv).

将y_test导入为熊猫DataFrame,而不是一个系列(使用to_csv保存它,并从其他地方导入from_csv)。

This confused scikit on the ROC curves, but it seems quite happy with that everywhere else.

ROC曲线上的这个模糊的scikit,但是它似乎和其他任何地方都很相似。

I'll leave this here in the (unlikely) case someone runs into the same thing.

我把这个放在这里,以防有人遇到同样的事情。