How to Plot ROC curve with matplotlib/python -


i want plot roc curve in python matplotlib , want show this:

enter image description here

i cannot find function that.

is there easy way it?

yes, don't think there's direct plot command it. recommend follow scikit-learn recipe it:

import numpy np import matplotlib.pyplot plt sklearn import svm, datasets sklearn.metrics import roc_curve, auc sklearn.cross_validation import train_test_split sklearn.preprocessing import label_binarize sklearn.multiclass import onevsrestclassifier scipy import interp  # import data play iris = datasets.load_iris() x = iris.data y = iris.target  # binarize output y = label_binarize(y, classes=[0, 1, 2]) n_classes = y.shape[1]  # add noisy features make problem harder random_state = np.random.randomstate(0) n_samples, n_features = x.shape x = np.c_[x, random_state.randn(n_samples, 200 * n_features)]  # shuffle , split training , test sets x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.5,                                                     random_state=0)  # learn predict each class against 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)  # compute roc curve , roc area each class fpr = dict() tpr = dict() roc_auc = dict() in range(n_classes):     fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])     roc_auc[i] = auc(fpr[i], tpr[i])  # compute micro-average roc curve , roc area fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel()) roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])   ############################################################################## # plot of roc curve specific class plt.figure() plt.plot(fpr[2], tpr[2], label='roc curve (area = %0.2f)' % roc_auc[2]) plt.plot([0, 1], [0, 1], 'k--') 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()   ############################################################################## # plot roc curves multiclass problem  # compute macro-average roc curve , roc area  # first aggregate false positive rates all_fpr = np.unique(np.concatenate([fpr[i] in range(n_classes)]))  # interpolate roc curves @ points mean_tpr = np.zeros_like(all_fpr) in range(n_classes):     mean_tpr += interp(all_fpr, fpr[i], tpr[i])  # average , compute auc mean_tpr /= n_classes  fpr["macro"] = all_fpr tpr["macro"] = mean_tpr roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])  # plot roc curves plt.figure() plt.plot(fpr["micro"], tpr["micro"],          label='micro-average roc curve (area = {0:0.2f})'                ''.format(roc_auc["micro"]),          linewidth=2)  plt.plot(fpr["macro"], tpr["macro"],          label='macro-average roc curve (area = {0:0.2f})'                ''.format(roc_auc["macro"]),          linewidth=2)  in range(n_classes):     plt.plot(fpr[i], tpr[i], label='roc curve of class {0} (area = {1:0.2f})'                                    ''.format(i, roc_auc[i]))  plt.plot([0, 1], [0, 1], 'k--') 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 multi-class') plt.legend(loc="lower right") plt.show() 

you notice plot's given should this:

scikit-learn roc recipe

this not style requesting should adapt matplotlib code contain this:

import numpy np import matplotlib.pyplot plt  x = [i in range(7)] y = [i**2 in range(7)] in range(1,len(x)):     diffx = (x[i]-x[i-1])*0.15     diffy = (y[i]-y[i-1])*0.15     plt.plot((x[i-1]+diffx,x[i]-diffx),(y[i-1]+diffy,y[i]-diffy),color='black',linewidth=3)     plt.scatter(x[i-1],y[i-1],marker='o',s=30,facecolor='white',edgecolor='black')  plt.plot((min(x),max(x)),(min(y),max(y)),color='red',linewidth=3,linestyle='--')  plt.show() 

which results in this:

styles , features in matplotlib line plot

adapt @ convenience.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -