

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn import model_selection
# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
X, y = X[y != 2], y[y != 2]
# Add noisy features to make the 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 and split training and test sets
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=.3, random_state=0)
# Learn to predict each class against the other
svm = svm.SVC(kernel='linear', probability=True, random_state=random_state)
y_score = svm.fit(X_train, y_train).decision_function(X_test)
# Compute ROC curve and ROC area for each class
fpr, tpr, threshold = roc_curve(y_test, y_score) ###计算真正率和假正率
# tpr = [0,0.24,0.24,0.285,0.3,0.38,0.38,0.4,0.4,0.48,0.48,0.55,0.55,0.615,0.615,0.75,0.85,0.85,0.9,0.977528,0.980952,1]
# fpr = [0,0.001,0.005,0.005,0.02,0.02,0.03,0.035,0.04,0.04,0.06,0.06,0.07,0.072,0.074,0.078,0.078,0.085,0.085,0.09127,0.122449,1]
tpr = [0,0.477612,0.507463,0.507463,0.537313,0.567164,0.686567,0.970149,1,1]
fpr = [0,0.148789,0.16955,0.197232,0.207612,0.214533,0.32526,0.515571,0.705882,1]
roc_auc = auc(fpr, tpr) ###计算auc的值
plt.figure()
lw = 2
plt.figure(figsize=(10, 10))
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Various Type ROC')
plt.legend(loc="lower right")
plt.show()