python计算auc指标实例

yipeiwu_com6年前Python基础

1、安装scikit-learn

1.1Scikit-learn 依赖

Python (>= 2.6 or >= 3.3),
NumPy (>= 1.6.1),
SciPy (>= 0.9).

分别查看上述三个依赖的版本,

python -V 结果:Python 2.7.3
python -c 'import scipy; print scipy.version.version' scipy版本结果:0.9.0
python -c "import numpy; print numpy.version.version" numpy结果:1.10.2

1.2 Scikit-learn安装

如果你已经安装了NumPy、SciPy和python并且均满足1.1中所需的条件,那么可以直接运行sudo pip install -U scikit-learn 执行安装。

2、计算auc指标

 import numpy as np
 from sklearn.metrics import roc_auc_score
 y_true = np.array([0, 0, 1, 1])
 y_scores = np.array([0.1, 0.4, 0.35, 0.8])
 roc_auc_score(y_true, y_scores)

输出:0.75

3、计算roc曲线

 import numpy as np
 from sklearn import metrics
 y = np.array([1, 1, 2, 2])  #实际值
 scores = np.array([0.1, 0.4, 0.35, 0.8]) #预测值
 fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2) #pos_label=2,表示值为2的实际值为正样本
 print fpr
 print tpr
 print thresholds

输出:
array([ 0. , 0.5, 0.5, 1. ])
array([ 0.5, 0.5, 1. , 1. ])
array([ 0.8 , 0.4 , 0.35, 0.1 ])

以上这篇python计算auc指标实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 处理图片像素点的实例

Python 处理图片像素点的实例

###在做爬虫的时候有时需要识别验证码,但是验证码一般都有干扰物,这时需要对验证码进行预处理,效果如下: from PIL import Image import itertool...

Python 做曲线拟合和求积分的方法

这是一个由加油站油罐传感器测量的油罐高度数据和出油体积,根据体积和高度的倒数,用截面积来描述油罐形状,求出拟合曲线,再用标准数据,求积分来验证拟合曲线效果和误差的一个小项目。 主要的就是...

python奇偶行分开存储实现代码

python奇偶行分开存储实现代码

例子: 1:www.jb51.net 2:www.jb51.net 3:www.jb51.net 4:www.jb51.net 5:www.jb51.net 6:www.jb51.ne...

python返回数组的索引实例

使用python里的index nums = [1, 2, 3, 4, 5, 6, 1, 9] print nums.index(max(nums)) print nums.inde...

Pycharm学习教程(4) Python解释器的相关配置

Pycharm学习教程(4) Python解释器的相关配置

Python解释器的相关配置,供大家参考,具体内容如下 1、准备工作   (1)Pycharm版本为3.4或者更高。   (2)电脑上至少已经安装了一个Python解释器。   (3)如...