Guest User

Untitled

a guest
Jul 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from sklearn import svm, datasets
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import label_binarize
  5. from sklearn.metrics import roc_curve, auc
  6. from sklearn.multiclass import OneVsRestClassifier
  7.  
  8. iris = datasets.load_iris()
  9. X = iris.data
  10. y = iris.target
  11.  
  12. # Binarize the output
  13. y = label_binarize(y, classes=[0, 1, 2])
  14. n_classes = y.shape[1]
  15.  
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0)
  17.  
  18. classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
  19. random_state=0))
  20. y_score = classifier.fit(X_train, y_train).decision_function(X_test)
  21.  
  22. fpr = dict()
  23. tpr = dict()
  24. roc_auc = dict()
  25. for i in range(n_classes):
  26. fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
  27. roc_auc[i] = auc(fpr[i], tpr[i])
  28. colors = cycle(['blue', 'red', 'green'])
  29. for i, color in zip(range(n_classes), colors):
  30. plt.plot(fpr[i], tpr[i], color=color, lw=lw,
  31. label='ROC curve of class {0} (area = {1:0.2f})'
  32. ''.format(i, roc_auc[i]))
  33. plt.plot([0, 1], [0, 1], 'k--', lw=lw)
  34. plt.xlim([-0.05, 1.0])
  35. plt.ylim([0.0, 1.05])
  36. plt.xlabel('False Positive Rate')
  37. plt.ylabel('True Positive Rate')
  38. plt.title('Receiver operating characteristic for multi-class data')
  39. plt.legend(loc="lower right")
  40. plt.show()
Add Comment
Please, Sign In to add comment