港服(Server.HK)Python教程:python如何计算auc

1、安装scikit-learn 1.1 Scikit-learn 依赖 ·Python (>= 2.6 or &…


1、安装scikit-learn

1.1 Scikit-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学习网,免费的在线学习python平台,欢迎关注!

为您推荐

港服(Server.HK)Python教程:如何实现对Python中列表的排序?

对List进行排序,Python提供了两个方法 方法1.用List的内建函数list.sort进行排序 list.sor...

港服(Server.HK)Python教程:python迭代器中的函数整理

1、可以连接迭代器的函数 chain:按顺序将多个迭代器连接成一个迭代器。 Cycle:重复迭代器的所有元素。 Tee:...

港服(Server.HK)Python教程:用Python举例实现逆波兰表达式

逆波兰表达式是编译原理中的一种基本表达式,利用Python语言也可以实现逆波兰表达式的输出,这里举例实践说明: 什么是逆...

Python 程序:检查给定字符串是否为回文

港服(Server.HK)Python教程: 用一个实例写一个 Python 程序来检查给定的字符串是不是回文。在 Py...

港服(Server.HK)Python教程:python3判断字典中key是否存在

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实...
返回顶部