【问题标题】:ValueError: Data is not binary and pos_label is not specified for roc_curveValueError:数据不是二进制的,并且没有为 roc_curve 指定 pos_label
【发布时间】:2016-06-27 11:17:18
【问题描述】:

我正在尝试计算 roc_curve 但收到此错误消息

Traceback (most recent call last):
  File "script.py", line 94, in <module>
    fpr, tpr, _ = roc_curve(y_validate, status[:,1])
  File "/usr/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 501, in roc_curve
    y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 308, in _binary_clf_curve
    raise ValueError("Data is not binary and pos_label is not specified")
ValueError: Data is not binary and pos_label is not specified

我的代码

status = rf.predict_proba(x_validate)
fpr, tpr, _ = roc_curve(y_validate, status[:,1]) //error generated here
roc_auc = auc(fpr, tpr)
print roc_auc

P.S:不太了解这个解决方案 (ValueError: Data is not binary and pos_label is not specified),因为它似乎并不真正相关。

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    要使 ROC 曲线的计算有效,您必须指定您将其视为“真”或“正”标签的标签。 Scikit-learn 假设提供给它的数据将始终具有标签 0 和 1(在您的情况下为变量 y_validate),其中一个被任意选择为正标签(我不完全知道如何 - 我是确保您可以挖掘源代码并弄清楚)。

    如您的错误消息中所述 - 您的数据不具有这种预期的二进制格式。即使您的数据是二进制的,但标签是“T”和“F”,它也会抛出这个错误。因此,根据roc_curve() function from scikit-learn 的文档,您需要准确指定要用作“正类”的字符串标签。因此,如果您的标签是 y_validate 变量中的“T”和“F”,您可以这样做:fpr, tpr, _ = roc_curve(y_validate, status[:,1], pos_label='T')。

    【讨论】:

    • 我先试试吧。
    猜你喜欢
    • 2013-08-26
    • 2019-09-21
    • 2021-09-24
    • 2020-05-22
    • 2014-03-16
    • 2021-04-16
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多