Python draw ROC plot

In the present case comes sklearn iris data set example, the main method used to plot the ROC plot sklearn.metric library
method sklearn with a function specific parameters can Tell me what network document:
https://scikit-learn.org /stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve


Environment: python3.7

1. Import related packages

from sklearn import metrics
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import roc_curve,auc
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

2. Create a decision tree classifier

clf = DecisionTreeClassifier(random_state=0)

3. introducing iris data set comes sklearn

iris=load_iris()
x=iris.data
y=iris.target

4. Data preprocessing
because the original data set classification iris has three kinds of labels, and in doing ROC model evaluation only binary determination, so here we need to first remove the third kind, the Categories 1 and 2 singled out

x,y=x[y!=2],y[y!=2]

5. Next we split the data set into a training set and test set, the original volume of very large data sets, where we can set the size of the data set be defined test_size used herein set to 0.2

X_train, X_test, y_train, y_test = train_test_split(x,y,stratify=y,test_size=0.2)
y_score=clf.fit(X_train,y_train).predict_proba(X_test)

Obtained FPR, TPR (negative sample rate and a positive sample rate) by roc_curve method
first parameter roc_curve the test sample, the second parameter is the probability of a positive sample, the third argument is positive sample values
such as: the test sample dataset is = [0,1,1,1,0,0], probability = [0,0.5,0.3,0.2,0.1], n sample values pos_label = 1

fpr,tpr,thre = roc_curve(y_test,y_score[:,1],pos_label=1)
roc_auc = metrics.auc(fpr,tpr)
#平均ROC曲线的两个参数
mean_tpr=0
mean_fpr=np.linspace(0,1,100)

Figure Drawing ROC

plt.figure(figsize=(10,10))
plt.plot(fpr,tpr,color='black',
         label='ROC curve(area = %0.2f)'% roc_auc)
plt.plot([0,1],[0,1],
         color='red',
         linestyle='--')
plt.show()

Renderings:

Here Insert Picture Description
Surprised to find is actually a triangle!
In fact, would not it ha ha ha here this effect occurs because sklearn comes with iris data set is too flat, and generally in practice to the effect that it will add some noise, but I am here as a case of drawing ROC plot I do not write. Normally, the graphics should come out like this:
Here Insert Picture Description

Released three original articles · won praise 1 · views 471

Guess you like

Origin blog.csdn.net/qq_40838896/article/details/105186441
Recommended