python调用库roc_curve()_python中实现ROC curve

以下是使用scikit learn预测、做出决策边界并画出ROC曲线的一个示例,以鸢尾花数据集为例。

1. 导入鸢尾花的数据

import numpy as np

import matplotlib.pyplot as plt

import warnings

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn import metrics

from sklearn.preprocessing import StandardScaler

from sklearn.svm import LinearSVC

warnings.filterwarnings('ignore')

iris = datasets.load_iris()

X = iris.data

y = iris.target

X = X[y<2,:2]

y = y[y<2] # 方便可视化

2. 标准化数据并使用SVM预测

standardScaler = StandardScaler()

standardScaler.fit(X)

X_standard = standardScaler.transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_standard, y, test_size=0.75, random_state=1)

svc2 = LinearSVC(C=0.001)

svc2.fit(X_train, y_train)

3. 做出决策边界

# 决策边界函数

def plot_boundary(model, X, y):

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5

y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

h = .02 # step size in the mesh

xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot

Z = Z.reshape(xx.shape)

plt.figure(1, figsize=(4, 3))

plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Set3)

# Plot also the training points

plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Greens)

plt.show()

plot_boundary(svc2, X_train, y_train)

4. ROC曲线

y_pred_proba = poly_kernel_svc.predict_proba(X_test)[::,1]

fpr, tpr, _ = metrics.roc_curve(y_test, y_pred_proba)

auc = metrics.roc_auc_score(y_test, y_pred_proba)

plt.plot(fpr,tpr,label='SVM model AUC %0.2f' % auc, color='blue', lw = 2)

plt.plot([0, 1], [0, 1], color='black', lw=2, linestyle='--')

plt.xlim([0.0, 1.0])

plt.ylim([0.0, 1.05])

plt.xlabel('False Positive Rate')

plt.ylabel('True Positive Rate')

plt.title('Receiver operating Curve')

plt.legend(loc="lower right")

plt.show()

示例数据集比较简单,所以效果非常好,一般的数据集画出的效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值