2017-04-15 185 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