【问题标题】:Scikit's Average Precision Score bad input shapeScikit 的平均精度得分错误的输入形状
【发布时间】:2018-09-22 16:59:40
【问题描述】:

我正在尝试绘制精确/召回分数曲线。这是我的代码:

    lbl_enc = preprocessing.LabelEncoder()
    labels = lbl_enc.fit_transform(test_tags)

    y_score = clf.predict_proba(test_set)

    average_precision = average_precision_score(labels, y_score)
    print('Average precision-recall score: {0:0.2f}'.format(average_precision))

    precision, recall, _ = precision_recall_curve(labels, y_score)

    plt.step(recall, precision, color='b', alpha=0.2,
             where='post')
    plt.fill_between(recall, precision, step='post', alpha=0.2,
                     color='b')

    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.0])
    plt.title('2-class Precision-Recall curve: Average P-R = {0:0.2f}'.format(
        average_precision))

在计算 average_precision_score 时,我得到了由“y_score”变量引起的“ValueError: bad input shape (119, 2)”。

y_score 的格式如下:

array([[0.45953712, 0.54046288],
   [0.78289908, 0.21710092],
   [0.13488789, 0.86511211],
   [0.56162583, 0.43837417],
   (...)
   [0.4595595 , 0.5404405 ]])

标签在此:

array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1])

我怎样才能使这项工作来计算平均精度分数?提前致谢。

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    documentation 中,它说:

    y_score : 数组,形状 = [n_samples] 或 [n_samples, n_classes]

    目标分数,可以是正数的概率估计 类别、置信度值或决策的非阈值度量 (由某些分类器上的“decision_function”返回)。

    因此我相信你只需要这样做:

    average_precision  = average_precision_score(labels, y_score[:,1])
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2015-07-02
      • 2018-04-30
      • 2013-12-18
      • 2016-02-15
      • 2015-01-04
      • 2018-07-03
      • 2012-11-30
      • 2019-05-11
      相关资源
      最近更新 更多