【问题标题】:Sklearn precision recall curve pos_label for unbalanced dataset which class probability to use不平衡数据集的 Sklearn 精度召回曲线 pos_label 使用哪个类概率
【发布时间】:2021-05-17 00:04:01
【问题描述】:


我想使用精确召回分数来评估我的模型,因为我的数据不平衡。因为我有一个二元分类,所以我在 NN 的末尾使用了一个 softmax。 输出分数和真实标签看起来像:

y_score = [[0.4, 0.6],
           [0.6, 0.4],
           [0.3, 0.7],
              ...   ]
y_true = [1,
          0,
          0,
         ...]

其中y_score[:,0]对应0类的概率。
我的正面标签0,因此负面标签在我的例子中是1

由于我的数据集是不平衡的(负数多于正数),我想使用精确召回分数 (AUPRC) 来评估我的分类器。函数sklearn.metrics.precision_recall_curve 接受一个参数pos_label,我将其设置为pos_label = 0。但是参数probas_pred 采用形状概率的 ndarray (n_samples,)。

我的问题是,既然我设置了pos_label = 0,我应该将我的y_score 列中的哪一个作为probas_pred

我希望我的问题很清楚。
提前谢谢!

【问题讨论】:

    标签: python scikit-learn precision-recall


    【解决方案1】:

    它应该是上面示例中的第一列,这里是您可以检查的方法。

    使用示例数据集:

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.neural_network import MLPClassifier
    from sklearn.datasets import make_blobs
    from sklearn.metrics import precision_recall_curve
    
    X, y = make_blobs(n_samples=[400,2000], centers=None,n_features=5,random_state=999,cluster_std=5)
    
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=111)
    

    训练分类器:

    clf = MLPClassifier(hidden_layer_sizes=(3, 3), random_state=999)
    clf.fit(X_train, y_train)
    

    检查类:

    clf.classes_
    array([0, 1])
    

    你可以把它放在dataframe上看看是否正确:

        0   1   actual
    0   0.999734    0.000266    0
    1   0.001253    0.998747    1
    2   0.000137    0.999863    1
    3   0.000113    0.999887    1
    4   0.003173    0.996827    1
    ... ... ... ...
    475 0.014316    0.985684    1
    476 0.012767    0.987233    1
    477 0.062735    0.937265    1
    478 0.000048    0.999952    1
    479 0.999733    0.000267    0
    

    然后计算:

    prec,recall,thres = precision_recall_curve(y_true=y_test , probas_pred= clf.predict_proba(X_test)[:,0], pos_label=0)
    

    并绘制它.. 如果你翻转你的值,这看起来真的很奇怪,但低于它的正确值:

    plt.plot(prec,recall)
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2021-05-05
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2018-07-04
      • 2017-10-28
      • 2019-12-27
      相关资源
      最近更新 更多