【问题标题】:Getting perfect ROC-AUC score for Linear SVC获得线性 SVC 的完美 ROC-AUC 分数
【发布时间】:2018-07-24 23:18:49
【问题描述】:

我正在为我的情绪分析模型评估不同的分类器。我正在查看所有可用的指标,虽然大多数指标都达到了相似的精度、召回率、F1 分数和 ROC-AUC 分数,但线性 SVM 似乎获得了 完美 ROC-AUC 分数。请看下面的图表:

缩写:MNB=多项朴素贝叶斯,SGD=随机梯度下降,LR=逻辑回归,LSVC=线性支持向量分类

以下是 LSVC 的其他性能指标,它们与其他分类器非常相似:

             precision    recall  f1-score   support

        neg       0.83      0.90      0.87     24979
        pos       0.90      0.82      0.86     25021

avg / total       0.87      0.86      0.86     50000

如您所见,数据集对于 pos 和 neg cmets 是平衡的。

以下是相关代码:

def evaluate(classifier):
    predicted = classifier.predict(testing_text)
    if isinstance(classifier.steps[2][1], LinearSVC):
        probabilities = np.array(classifier.decision_function(testing_text))
        scores = probabilities
    else:
        probabilities = np.array(classifier.predict_proba(testing_text))
        scores = np.max(probabilities, axis=1)

    pos_idx = np.where(predicted == 'pos')
    predicted_true_binary = np.zeros(predicted.shape)
    predicted_true_binary[pos_idx] = 1
    fpr, tpr, thresholds = metrics.roc_curve(predicted_true_binary, scores)
    auc = metrics.roc_auc_score(predicted_true_binary, scores)

    mean_acc = np.mean(predicted == testing_category)
    report = metrics.classification_report(testing_category, predicted)
    confusion_matrix = metrics.confusion_matrix(testing_category, predicted)

    return fpr, tpr, auc, mean_acc, report, confusion_matrix

我对所有分类器都使用predict_proba,除了LSVC,它使用decision_function(因为它没有predict_proba方法`)

发生了什么事?

编辑:根据@Vivek Kumar 的 cmets 进行更改:

def evaluate(classifier):
    predicted = classifier.predict(testing_text)
    if isinstance(classifier.steps[2][1], LinearSVC):
        probabilities = np.array(classifier.decision_function(testing_text))
        scores = probabilities
    else:
        probabilities = np.array(classifier.predict_proba(testing_text))
        scores = probabilities[:, 1]  # NEW

    testing_category_array = np.array(testing_category)  # NEW
    pos_idx = np.where(testing_category_array == 'pos')
    predicted_true_binary = np.zeros(testing_category_array.shape)
    predicted_true_binary[pos_idx] = 1
    fpr, tpr, thresholds = metrics.roc_curve(predicted_true_binary, scores)
    auc = metrics.roc_auc_score(predicted_true_binary, scores)

    mean_acc = np.mean(predicted == testing_category)
    report = metrics.classification_report(testing_category, predicted)
    confusion_matrix = metrics.confusion_matrix(testing_category, predicted)

    return fpr, tpr, auc, mean_acc, report, confusion_matrix

这会产生这个图表:

【问题讨论】:

  • 您正在使用predicted_true_binaryscores 的预测值。本质上,您将预测与 roc_curveroc_auc_score 中的预测进行比较,而实际上它应该将真实标签 (testing_category) 作为第一个参数。
  • 其次,scores 理想情况下应该是正类的概率,而不是您在 np.max() 中所做的最大概率。
  • 1) 我明白你的意思,我会改正的。 2)predicted_proba 返回(pos_prob, neg_prob) 的列表 - 我正在做max 以便概率与类别匹配。否则我最终会得到与pos 概率相对应的neg 类别,不是吗?
  • @VivekKumar 我已经用新的变化编辑了我的帖子
  • 好。似乎是正确的。无需将概率包装在 np.array 中。它们已经是 np 数组。您现在可以将此添加为答案并接受而不是编辑问题。

标签: scikit-learn svm sentiment-analysis roc auc


【解决方案1】:

我认为将方法 predict_proba 和 decision_function 进行比较是无效的。 LSVC 决策函数docs 中的第一句“预测样本的置信度分数”。不得将其解读为“预测概率”。第二句澄清了,类似于一般SVC.的决策函数

您可以将 predict_proba 用于带有 sklearn 的线性 SVC;那么您需要将一般 SVC 下的内核指定为“线性”。但是,您正在更改底层的实现(远离“LIBLINEAR”)。

【讨论】:

  • 不,没关系。 roc_curveroc_auc_score 可以同时处理(决策函数置信度值和 predict_proba 概率)。
猜你喜欢
  • 2017-10-25
  • 2012-04-15
  • 2018-04-16
  • 2020-11-27
  • 2020-03-12
  • 1970-01-01
  • 2017-06-04
  • 2020-10-01
  • 2020-02-23
相关资源
最近更新 更多