鱼C论坛

 找回密码
 立即注册
查看: 2113|回复: 12

[技术交流] Python实现ROC和P-R曲线绘制以及AUC计算

[复制链接]
发表于 2020-11-2 13:26:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-11-2 19:05 编辑

参考书籍:《美团机器学习实战》



  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. %matplotlib inline

  6. #分类指标
  7. class cat_plot():
  8.     def __init__(self, data, cat):
  9.         self.data = data
  10.         self.cat = cat
  11.         
  12.     def Precision(self, threshold):
  13.         data["预测分类"] = self.data.iloc[:, 1].apply(lambda x : 1 if x > threshold else 0)
  14.         TP = np.sum((self.data["预测分类"] == 1) & (self.data["二分类"] == 1))
  15.         FP = np.sum((self.data["预测分类"] == 1) & (self.data["二分类"] == 0))
  16.         comput = TP / (TP + FP)
  17.         return comput

  18.     def Recall(self, threshold):
  19.         data["预测分类"] = self.data.iloc[:, 1].apply(lambda x : 1 if x > threshold else 0)
  20.         TP = np.sum((self.data["预测分类"] == 1) & (self.data["二分类"] == 1))
  21.         FN = np.sum((self.data["预测分类"] == 0) & (self.data["二分类"] == 0))
  22.         comput = TP / (TP + FN)
  23.         return comput

  24.     def FPR(self, threshold):
  25.         data["预测分类"] = self.data.iloc[:, 1].apply(lambda x : 1 if x > threshold else 0)
  26.         FP = np.sum((self.data["预测分类"] == 1) & (self.data["二分类"] == 0))
  27.         TN = np.sum((self.data["预测分类"] == 0) & (self.data["二分类"] == 1))
  28.         comput = FP / (FP + TN)
  29.         return comput
  30.    
  31.     def plot(self):
  32.         point_x = []   
  33.         point_y = []
  34.         if self.cat == "P-R":
  35.             for i in range(len(self.data)):
  36.                 threshold = self.data.iloc[i, 1]
  37.                 point_x.append(self.Recall(threshold))
  38.                 point_y.append(self.Precision(threshold))
  39.             ax1 = sns.lineplot(x = point_x, y = point_y)
  40.             ax1.set_title("P-R")
  41.             plt.show()
  42.         elif self.cat == "ROC":
  43.             for i in range(len(self.data)):
  44.                 threshold = self.data.iloc[i, 1]
  45.                 point_x.append(self.FPR(threshold))
  46.                 point_y.append(self.Recall(threshold))
  47.             ax2 = sns.lineplot(x = point_x, y = point_y)
  48.             ax2.set_title("ROC")
  49.             plt.show()
  50.    
  51.     def AUC(self):
  52.         self.data = self.data.sort_values(by= "预测概率",ascending = True)
  53.         self.data["rank"] = self.data["预测概率"].rank(method='average')
  54.         M = np.sum(self.data["二分类"])
  55.         comput = np.sum(self.data.loc[data["二分类"] == 1, "rank"]) - (M+1)*M / 2
  56.         print(comput/(M * (len(self.data) - M)))
  57.             
  58.    
  59. if __name__ == '__main__':
  60.     #数据
  61.     data = {"二分类" :[1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1],
  62.             "预测概率" :[0.99, 0.12, 0.29, 0.45, 0.93, 0.78, 0.89, 0.34, 0.78,
  63.                      0.67, 0.99, 0.17, 0.88, 0.29, 0.72, 0.11, 0.89, 0.49, 0.34, 0.78,
  64.                      0.11, 0.99]}
  65.     data = pd.DataFrame(data)
  66.     temp1 = cat_plot(data, "P-R")
  67.     temp2 = cat_plot(data, "ROC")
  68.     temp1.plot()
  69.     temp2.plot()
  70.     temp1.AUC()
  71.    
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-2 13:27:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-2 15:08:44 | 显示全部楼层
%matplotlib inline
这个是起什么作用, 运行报错, 直接删掉也可以运行.

不过运行后有以下揭示, 是怎么回事?
Warning (from warnings module):
  File "D:/python/Lib/idlelib/0.py", line 17
    comput = TP / (TP + FP)
RuntimeWarning: invalid value encountered in longlong_scalars
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-2 19:03:34 | 显示全部楼层
Nate_2020 发表于 2020-11-2 15:08
%matplotlib inline
这个是起什么作用, 运行报错, 直接删掉也可以运行.

我是本地3.7的python可以运行。
%matplotlib inline是为了让图直接显示,我一般习惯性加上这句。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-2 19:05:19 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 08:50:08 | 显示全部楼层
糖逗 发表于 2020-11-2 19:03
我是本地3.7的python可以运行。
%matplotlib inline是为了让图直接显示,我一般习惯性加上这句。

哦, 我的是3.8的.
知不知道为什么用pyinstaller转成exe文件后不能运行,揭示RuntimeError:Could not find the matplotlib data files.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 09:22:29 | 显示全部楼层
Nate_2020 发表于 2020-11-4 08:50
哦, 我的是3.8的.
知不知道为什么用pyinstaller转成exe文件后不能运行,揭示RuntimeError:Could not find ...

你为啥不查一下百度呢,鱼油?百度能解决99%以上的问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 10:24:04 | 显示全部楼层
糖逗 发表于 2020-11-4 09:22
你为啥不查一下百度呢,鱼油?百度能解决99%以上的问题。

找了,没什么好方法, 都是说要降低版本. 试了一圈, 结果把我的IDLE整坏了, 双击一闪而过, 没有窗口,没有揭示. 修复都没有用. 不知道怎么回事.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 10:47:48 | 显示全部楼层
Nate_2020 发表于 2020-11-4 10:24
找了,没什么好方法, 都是说要降低版本. 试了一圈, 结果把我的IDLE整坏了, 双击一闪而过, 没有窗口,没有揭 ...

摸摸头,没有遇到过这种情况
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 14:09:12 | 显示全部楼层
糖逗 发表于 2020-11-4 10:47
摸摸头,没有遇到过这种情况

刚搞好了, 直接repair不行, 要删掉一部分文件再repair才好了. 谢谢了.
刚安装了pycharm, 试用这个看看.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 14:14:20 | 显示全部楼层
Nate_2020 发表于 2020-11-4 14:09
刚搞好了, 直接repair不行, 要删掉一部分文件再repair才好了. 谢谢了.
刚安装了pycharm, 试用这个看看.

我用的是Anaconda
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 15:07:14 | 显示全部楼层

你都会写那个程序, 应该是高手了, 怎么不用pycharm.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 15:16:26 | 显示全部楼层
Nate_2020 发表于 2020-11-4 15:07
你都会写那个程序, 应该是高手了, 怎么不用pycharm.

不是高手
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-27 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表