python skylearnはROC曲線を描きます。


preface:最近「生物情報学」は何度もAUC、ROCという二つの指標に言及しています。今やっているプロジェクトはROC曲線を描くことを要求しています。slearnの中には相応の関数がありますので、勉強します。
AUC:
ROC:
具体的にはスカイツリーを参照してください。
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curvel.
http://scikit-learn.org/stable/auto_examples/model_セレクションroc_crossval.co-plot-roc-crossval-py
http://www.tuicool.com/articles/b22eYz(ブロ友ブログ)
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 08:57:13 2015

@author: shifeng
"""
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,  iris  ,     

# import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
X, y = X[y != 2], y[y != 2]#   label 2,label    ,   。
n_samples, n_features = X.shape

# Add noisy features
random_state = np.random.RandomState(0)
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

###############################################################################
# Classification and ROC analysis
#  , ROC  

# Run classifier with cross-validation and plot ROC curves
#  6     ,   ROC  
cv = StratifiedKFold(y, n_folds=6)
classifier = svm.SVC(kernel='linear', probability=True,
                     random_state=random_state)#    ,probability=True,  ,            。  rbf      。

mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
all_tpr = []

for i, (train, test) in enumerate(cv):
	#      ,  svm       ,         ,      
    probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test])
#    print set(y[train])                     #set([0,1])  label     
#    print len(X[train]),len(X[test])        #    84 ,    16 
#    print "++",probas_                      #predict_proba()          lael        ,
#    #           ,     
    # Compute ROC curve and area the curve
    #  roc_curve()  ,  fpr tpr,    
    fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
    mean_tpr += interp(mean_fpr, fpr, tpr)			# mean_tpr mean_fpr     ,  scipy   interp()  
    mean_tpr[0] = 0.0 								#    0
    roc_auc = auc(fpr, tpr)
    #  ,   plt.plot(fpr,tpr),  roc_auc    auc  ,  auc()       
    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  ,             
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()