2016-11-30 45 views
0

我要繪製的不同ROC對同一地塊的多個分類,但我不如何從它們中的一些做:ROC與多個分類蟒蛇交叉驗證

這裏是我的代碼片段:

# Learn to predict each class against the other 
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, 
           random_state=random_state)) 
y_score = classifier.fit(X_train, y_train).decision_function(X_test) 
n_classes=2 
# Compute ROC curve and ROC area for each class 
fpr = dict() 
tpr = dict() 
roc_auc = dict() 
for i in range(n_classes): 
    fpr[i], tpr[i], _ = roc_curve(y_test, y_score) 
    roc_auc[i] = auc(fpr[i], tpr[i]) 

# Compute micro-average ROC curve and ROC area 
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel()) 
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]) 
plt.figure(1) 
lw = 1 
plt.plot(fpr[1], tpr[1], color='darkorange', 
     lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[1]) 

# Learn to predict each class against the other 
from sklearn.linear_model import LogisticRegression 
classifier = LogisticRegression() 
y_score = classifier.fit(X_train, y_train).decision_function(X_test) 

# Compute ROC curve and ROC area for each class 
fpr = dict() 
tpr = dict() 
roc_auc = dict() 
for i in range(n_classes): 
    fpr[i], tpr[i], _ = roc_curve(y_test, y_score) 
    roc_auc[i] = auc(fpr[i], tpr[i]) 

# Compute micro-average ROC curve and ROC area 
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel()) 
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]) 

plt.figure(1) 
lw = 1 
plt.plot(fpr[1], tpr[1], color='darkblue', 
     lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[1]) 
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') 
plt.xlim([0.0, 1.0]) 
plt.ylim([0.0, 1.05]) 
plt.xlabel('False Positive Rate') 
plt.ylabel('True Positive Rate') 
plt.title('Receiver operating characteristic example') 
plt.legend(loc="lower right") 
plt.show() 

代碼將編譯,但只打印第二分類結果
我的問題是:

  1. 我如何可以繪製爲d多個ROC曲線不同的分類器?
  2. 如果我想使用沒有decision_function()的分類器,我該如何替換它?

謝謝!

回答

0

對於點1),這對我的作品在PDF(打印結果)

ClassesList = [0,1,2,3] 
y = label_binarize(y_test, classes=ClassesList) 
n_classes = y.shape[1] 

# Compute ROC curve and ROC area for each class 
fpr = {} 
tpr = {} 
roc_auc = {} 


for i in range(n_classes): 

    fpr[i], tpr[i], _ = metrics.roc_curve(y[:,i], y_score[:, i]) 
    roc_auc[i] = metrics.auc(fpr[i], tpr[i]) 


# Compute micro-average ROC curve and ROC area 
fpr["micro"], tpr["micro"], _ = metrics.roc_curve(y.ravel(), y_score.ravel()) 
roc_auc["micro"] = metrics.auc(fpr["micro"], tpr["micro"]) 

# Plot ROC curves for the multiclass problem 

# Compute macro-average ROC curve and ROC area 

# First aggregate all false positive rates 
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)])) 

# Then interpolate all ROC curves at this points 
mean_tpr = np.zeros_like(all_fpr) 
for i in range(n_classes): 
    mean_tpr += interp(all_fpr, fpr[i], tpr[i]) 

# Finally average it and compute AUC 
mean_tpr /= n_classes 

fpr["macro"] = all_fpr 
tpr["macro"] = mean_tpr 
roc_auc["macro"] = metrics.auc(fpr["macro"], tpr["macro"]) 

# Plot all ROC curves 
plt.figure() 
plt.plot(fpr["micro"], tpr["micro"], 
     label='micro-average ROC curve (area = {0:0.2f})' 
       ''.format(roc_auc["micro"]), 
     color='deeppink', linestyle=':', linewidth=4) 

plt.plot(fpr["macro"], tpr["macro"], 
     label='macro-average ROC curve (area = {0:0.2f})' 
       ''.format(roc_auc["macro"]), 
     color='navy', linestyle=':', linewidth=4) 

colors = cycle(['aqua', 'darkorange', 'cornflowerblue','red']) 
for i, color in zip(range(n_classes), colors): 
    plt.plot(fpr[i], tpr[i], color=color, lw=2, 
      label='ROC curve of class {0} (area = {1:0.2f})' 
      ''.format(i, roc_auc[i])) 

plt.plot([0, 1], [0, 1], 'k--', lw=2) 
plt.xlim([0.0, 1.0]) 
plt.ylim([0.0, 1.05]) 
plt.xlabel('False Positive Rate') 
plt.ylabel('True Positive Rate') 
plt.title('Some extension of Receiver operating characteristic to multi-class') 
plt.legend(loc="lower right") 

with PdfPages('ROCCurve.pdf') as ROCCurvePdf: 
    plt.savefig(ROCCurvePdf, format='pdf') 

plt.clf() 

對於點2)我並不確切地知道你的意思