【问题标题】:How to get the result auc using scikit如何使用 scikit 获得结果 auc
【发布时间】:2017-06-05 18:01:10
【问题描述】:

您好,我想将训练/测试拆分与交叉验证相结合,并在 auc 中获得结果。

我的第一种方法我明白了,但很准确。

# split data into train+validation set and test set
X_trainval, X_test, y_trainval, y_test = train_test_split(dataset.data, dataset.target)
# split train+validation set into training and validation sets
X_train, X_valid, y_train, y_valid = train_test_split(X_trainval, y_trainval)
# train on classifier
clf.fit(X_train, y_train)
# evaluate the classifier on the test set
score = svm.score(X_valid, y_valid)
# combined training & validation set and evaluate it on the test set
clf.fit(X_trainval, y_trainval)
test_score = svm.score(X_test, y_test)

我没有找到如何申请 roc_auc,请帮忙。

【问题讨论】:

  • 您的数据中有多少类?
  • 你好,我有两个班 0-1

标签: python-3.x scikit-learn cross-validation train-test-split


【解决方案1】:

使用 scikit-learn 你可以做到:

import numpy as np
from sklearn import metrics
y = np.array([1, 1, 2, 2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)

现在我们得到:

print(fpr)

数组([ 0. , 0.5, 0.5, 1. ])

print(tpr)

数组([ 0.5, 0.5, 1. , 1. ])

print(thresholds)

数组([ 0.8 , 0.4 , 0.35, 0.1 ])

【讨论】:

    【解决方案2】:

    在您的代码中,训练分类器后,通过以下方式获得预测:

    y_preds = clf.predict(X_test)
    

    然后用这个来计算auc值:

    from sklearn.metrics import roc_curve, auc
    
    fpr, tpr, thresholds = roc_curve(y, y_preds, pos_label=1)
    auc_roc = auc(fpr, tpr)
    

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2020-07-07
      • 2017-02-06
      相关资源
      最近更新 更多