Python sklearn to draw Roc curves

Source: Internet
Author: User
Tags svm

Preface : Recent bioinformatics has talked about the AUC,Roc , two indicators, is doing project, requires the ROC curve,Sklearn inside has corresponding functions, so learn to learn.

Auc:

ROC:

Specific use of reference Sklearn:

Http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html

http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc_crossval.html# Example-model-selection-plot-roc-crossval-py

http://www.tuicool.com/articles/b22eYz (Bo Friends blog)

#coding: Utf-8print (__doc__) import numpy as Npfrom scipy import interpimport matplotlib.pyplot as Pltfrom sklearn import SV M, datasetsfrom sklearn.metrics import roc_curve, aucfrom sklearn.cross_validation import stratifiedkfold############# ################################################################### data IO and generation, import iris, do data preparation # import Some data to play Withiris = Datasets.load_iris () X = Iris.datay = iris.targetx, y = x[y! = 2], y[y! = 2]n_samples, N_featu res = x.shape# Add Noisy featuresrandom_state = np.random.RandomState (0) X = np.c_[x, Random_state.randn (n_samples, $ * n _features)]################################################################################ Classification and  ROC analysis# Classification, do ROC analysis # Run classifier with cross-validation and plot Roc curves# use 60 percent cross-validation, and draw roc curve CV = stratifiedkfold (y, n_folds=6) classifier = SVM. SVC (kernel= ' linear ', Probability=true, random_state=random_state) MEAN_TPR = 0.0MEAN_FPR = Np.linspace ( 0, 1, ALL_TPR =[]for I, (train, test) in enumerate (CV): #通过训练数据, establish a model using SVM linear kernel and test the test set to find the predicted score Probas_ = Classifier.fit (X[train], Y[trai N]). Predict_proba (X[test]) # Compute ROC curve and area the curve #通过roc_curve () function, find FPR and TPR, and threshold FPR, TPR, Thres Holds = Roc_curve (Y[test], probas_[:, 1]) MEAN_TPR + = Interp (MEAN_FPR, FPR, TPR) #对mean_tpr在mean_fpr处进行插值, call scipy via inte package RP () function mean_tpr[0] = 0.0 #初始处为0 ROC_AUC = AUC (FPR, TPR) #画图, only Plt.plot (FPR,TPR), variable ROC_AUC simply records the value of AUC, can be calculated by the AUC () function To 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个点, interpolated interpolation at each point multiple times average mean_tpr[-1] = 1.0 #坐标最后一个点为 (max) Mean_ AUC = AUC (MEAN_FPR, MEAN_TPR) #计算平均AUC值 # Draw the average ROC curve #print Mean_fpr,len (MEAN_FPR) #print mean_tprplt.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.xla Bel (' False Positive rate ') Plt.ylabel(' True Positive rate ') plt.title (' Receiver operating characteristic example ') plt.legend (loc= "lower Right") plt.show () 


Python sklearn to draw Roc curves

Related Article

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.