2017-04-15 184 views
0

我想绘制仅使用数据集中两个要素的分类器的ROC曲线。任何人都可以告诉我如何解决下面的错误。在Python中绘制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 is not available when probability=False

+0

使用'概率= TRUE'与SVC的实例,使概率估计。查看[docs](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)以供参考。 – acidtobi

回答

0

您首先需要实例化支持向量Classificator:

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

这将创建与default parameters一个classificator。

2

错误消息非常明确:“必须使用SVC实例作为第一个参数调用fit()”。

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

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

感谢您的评论。我做了改变,但是我又遇到了一个错误。我修改了我的第一篇文章。 – Shelly

+0

你尝试过'SVC(probability = True)'吗? – acidtobi

+0

是的,错误还没有解决。 – Shelly