简体   繁体   English

在Python中绘制ROC曲线

[英]Plotting ROC curve in Python

I am trying to plot the ROC curve of a classifier that only uses two features from a dataset. 我正在尝试绘制仅使用数据集中两个特征的分类器的ROC曲线。 Can anybody tell me how I can solve the error below. 谁能告诉我如何解决以下错误。

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))

Here is the error: 这是错误:

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)

NEW ERROR : After making the changes, I got the error below: 新错误 :进行更改后,出现以下错误:

Here is the code: 这是代码:

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))

And here is the Error: 这是错误:

AttributeError: predict_proba is not available when probability=False AttributeError:当概率= False时,predict_proba不可用

The error message is pretty clear: "fit() must be called with SVC instance as first argument". 错误消息非常清楚:“必须以SVC实例作为第一个参数来调用fit()”。

fit() is a method of the SVC class. fit()是SVC类的方法。 You need to create an SVC class instance first, then call fit() on it: 您需要先创建一个SVC类实例,然后在其上调用fit()

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

You first need to instantiate the Support Vector Classificator: 您首先需要实例化支持向量分类器:

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

This will create a classificator with the default parameters . 这将使用默认参数创建一个分类器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM