【问题标题】:Plotting ROC curve in Python在 Python 中绘制 ROC 曲线
【发布时间】:2017-09-11 21:46:49
【问题描述】:

我正在尝试绘制仅使用数据集中两个特征的分类器的 ROC 曲线。谁能告诉我如何解决下面的错误。

from sklearn.metrics import roc_curve, auc
from scipy import interp
from sklearn.cross_validation import StratifiedKFold
from sklearn.svm import SVC

X_train2 = X_train[:, [0, 1]]
X_train2
cv = StratifiedKFold(y_train, n_folds=3, random_state=1)

fig = plt.figure(figsize=(7, 5))
mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
all_tpr = []

for i, (train, test) in enumerate(cv):
    probas = SVC.fit(X_train2[train], y_train[train]).predict_proba(X_train2[test])
fpr, tpr, thresholds = roc_curve(y_train[test], probas[:, 1], pos_label=1)
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)'% (i+1, roc_auc))

这是错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-163-3eea9731f8d5> in <module>()
      1 from sklearn.svm import SVC
      2 for i, (train, test) in enumerate(cv):
----> 3     probas = SVC.fit(X_train2[train], y_train[train]).predict_proba(X_train2[test])
      4 fpr, tpr, thresholds = roc_curve(y_train[test], probas[:, 1], pos_label=1)
      5 mean_tpr += interp(mean_fpr, fpr, tpr)

TypeError: unbound method fit() must be called with SVC instance as first argument (got ndarray instance instead)

新错误:进行更改后,我收到以下错误:

代码如下:

estimator= SVC(C=10)
for i, (train, test) in enumerate(cv):
    probas = estimator.fit(X_train2[train], y_train[train]).predict_proba(X_train2[test])
fpr, tpr, thresholds = roc_curve(y_train[test], probas[:, 1], pos_label=1)
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)'% (i+1, roc_auc))

这是错误:

AttributeError:predict_proba 在probability=False 时不可用

【问题讨论】:

  • probability=True 与您的 SVC 实例一起使用以启用概率估计。检查docs 以供参考。

标签: python scikit-learn roc


【解决方案1】:

错误信息非常清楚:“fit() 必须以 SVC 实例作为第一个参数来调用”。

fit() 是 SVC 类的一个方法。您需要先创建一个SVC 类实例,然后在其上调用fit()

estimator = SVC(probability=True)
probas = estimator.fit(X_train2[train], y_train[train]).predict_proba(X_train2[test])

【讨论】:

  • 感谢您的评论。我进行了更改,但出现了另一个错误。我已经修改了我的初始帖子。
  • 你试过SVC(probability=True)吗?
  • @Shelly 将代码中的这一行 estimator= SVC(C=10) 更改为这一行 estimator= SVC(C=10, probability=True)
【解决方案2】:

您首先需要实例化支持向量分类器:

svc = SVC()
probas = SVC.fit(X_train2[train], y_train[train]).predict_proba(X_train2[test])

这将创建一个带有default parameters 的分类器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 2014-09-20
    • 1970-01-01
    • 2012-01-01
    • 2013-08-10
    • 2016-12-26
    • 2016-02-05
    • 2016-02-04
    相关资源
    最近更新 更多