当前位置: 首页 > news >正文

sklearn包中对于分类问题,如何计算accuracy和roc_auc_score?

1. 基础条件

import numpy as np
from sklearn import metricsy_true = np.array([1, 7, 4, 6, 3])
y_prediction = np.array([3, 7, 4, 6, 3])

2. accuracy_score计算

acc = metrics.accuracy_score(y_true, y_prediction)

这个没问题

3. roc_auc_score计算

The binary and multiclass cases expect labels with shape (n_samples,) while the multilabel case expects binary label indicators with shape (n_samples, n_classes).

因此metrics.roc_auc_score对于multiclasses类的roc_auc_score计算,需要一个二维array,每一列是表示分的每一类,每一行是表示是否为此类。

from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(sparse=False)
enc.fit(y_true.reshape(-1, 1))
y_true_onehot = enc.transform(y_true.reshape(-1, 1))
y_predictions_onehot = \enc.transform(y_prediction.reshape(-1, 1))
In [201]: y_true_onehot
Out[201]: 
array([[1., 0., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 1., 0., 0.],[0., 0., 0., 1., 0.],[0., 1., 0., 0., 0.]])In [202]: y_predictions_onehot
Out[202]: 
array([[0., 1., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 1., 0., 0.],[0., 0., 0., 1., 0.],[0., 1., 0., 0., 0.]])
In [204]: enc.categories_
Out[204]: [array([1, 3, 4, 6, 7])]

所以结合enc.categories_y_true_onehoty_truey_true_onehot的对应关系如下:

Class13467
true value: 11
true value: 71
true value: 41
true value: 61
true value: 31

因此,对于y_predictiony_prediction_onehot的对应关系就是如下:

Class13467
Prediction value: 31
Prediction value: 71
Prediction value: 41
Prediction value: 61
Prediction value: 31

这就解释了上述y_true_onehoty_prediction_onehot的返回结果。

ensemble_auc = metrics.roc_auc_score(y_true_onehot,y_predictions_onehot)
In [200]: ensemble_auc
Out[200]: 0.875
http://www.lryc.cn/news/172103.html

相关文章:

  • python温度转换程序
  • Vue2中10种组件通信方式和实践技巧
  • Flutter flutter.minSdkVersion的实际文件位置
  • python生成PDF报告
  • 在visual studio里安装Python并创建python工程
  • AIGC(生成式AI)试用 6 -- 从简单到复杂
  • 竞赛 基于深度学习的人脸识别系统
  • uniapp:APP开发,后台保活
  • vue2 项目中嵌入视频
  • 第二章 进程与线程 十二、进程同步与进程互斥
  • Linux内核链表(list)移植到任意平台
  • 【操作系统】聊聊什么是CPU上下文切换
  • CMake教程-第 2 步 添加一个库
  • DS 顺序表--类实现(C++数据结构题)
  • 0.UML
  • PostgreSQL设置主键为自增
  • input修改checkbox复选框默认选中样式
  • 高云FPGA系列教程(10):letter-shell移植
  • 【C语言学习笔记---指针进阶02】
  • 低功耗蓝牙物联网:未来连接的无限可能
  • 安装社区版本OB
  • JSON 串和 Java 对象的相互转换
  • 爬虫 — App 爬虫(一)
  • 如何使用正则表达式实现Java日志信息的抓取与收集
  • C/C++算法入门 | 简单模拟
  • stm32学习-芯片系列/选型/开发方式
  • mnist数据集
  • Java之IO概述以及
  • Spring WebFlux—Reactive 核心
  • 由于找不到d3dx9_43.dll,无法继续执行代码要怎么解决