Sklearn Painting ROC Curve __sklearn

Source: Internet
Author: User
Tags svm
#coding: Utf-8
Print (__doc__)

Import NumPy as NP
From scipy import Interp
Import Matplotlib.pyplot as Plt

From Sklearn import SVM, datasets
From sklearn.metrics import Roc_curve, AUC
From sklearn.cross_validation import Stratifiedkfold

###############################################################################
# data IO and generation, import iris data and prepare

# import some data to play with
Iris = Datasets.load_iris ()
X = Iris.data
y = Iris.target
X, y = x[y!= 2], y[y!= 2]
N_samples, n_features = X.shape

# ADD Noisy Features
Random_state = np.random.RandomState (0)
X = Np.c_[x, Random_state.randn (n_samples, * n_features)]

###############################################################################
# Classification and ROC analysis
#分类, do ROC analysis

# Run classifier with cross-validation and plot ROC curves
#使用6折交叉验证, and draw Roc curves
CV = Stratifiedkfold (y, n_folds=6)
classifier = SVM. SVC (kernel= ' linear ', probability=true,
Random_state=random_state)

MEAN_TPR = 0.0
MEAN_FPR = Np.linspace (0, 1, 100)
ALL_TPR = []

For I, (train, test) in enumerate (CV):
Print test
#通过训练数据, the SVM linear kernel is used to build the model, and the test set is tested to find out the prediction score.
Probas_ = Classifier.fit (X[train], Y[train]). Predict_proba (X[test)
# Compute ROC curve and area the curve
#通过roc_curve () function, find FPR and TPR, and thresholds
FPR, TPR, thresholds = Roc_curve (y[test), probas_[:, 1])
MEAN_TPR + = Interp (MEAN_FPR, FPR, TPR) #对mean_tpr在mean_fpr处进行插值, calling SciPy () function via INTERP package
Mean_tpr[0] = 0.0 #初始处为0
ROC_AUC = AUC (FPR, TPR)
#画图, only need Plt.plot (FPR,TPR), variable ROC_AUC just record AUC value, through the AUC () function can be calculated
Plt.plot (FPR, TPR, lw=1, label= ' ROC fold%d (area =%0.2f) '% (I, ROC_AUC))

#画对角线
Plt.plot ([0, 1], [0, 1], '--', color= (0.6, 0.6, 0.6), label= ' Luck ')

MEAN_TPR/= Len (CV) #在mean_fpr100个点, interpolation interpolation at each point for multiple averaging
MEAN_TPR[-1] = 1.0 #坐标最后一个点为 (1,1)
MEAN_AUC = AUC (MEAN_FPR, MEAN_TPR) #计算平均AUC值
#画平均ROC曲线
#print Mean_fpr,len (MEAN_FPR)
#print MEAN_TPR
Plt.plot (MEAN_FPR, MEAN_TPR, ' k--',
Label= ' Mean ROC (area =%0.2f) '% Mean_auc, lw=2)

Plt.xlim ([-0.05, 1.05])
Plt.ylim ([-0.05, 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.