第10章 评价分类结果 学习笔记下
目录
精准度-召回率曲线
Precision-Recall 曲线
ROC曲线
TPR
FPR
TPR和FPR的关系
scikit-learn中的ROC
ROC AUC
多分类问题中的混淆矩阵
精准度-召回率曲线
from sklearn import datasetsdigits = datasets.load_digits()
X = digits.data
y = digits.target.copy()y[digits.target==9] = 1
y[digits.target!=9] = 0from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)from sklearn.linear_model import LogisticRegressionlog_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
decision_scores = log_reg.decision_function(X_test)from sklearn.metrics import precision_score
from sklearn.metrics import recall_scoreprecisions = []
recalls = []
thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)
for threshold in thresholds:y_predict = np.array(decision_scores >= threshold, dtype='int')precisions.append(precision_score(y_test, y_predict))recalls.append(recall_score(y_test, y_predict))
Precision-Recall 曲线
precision_recall_curve 步长会自动计算
相关的详细参数参考官方文档
没有像我们的从最小值开始
横轴是threads
横轴是p,纵轴是R
PR曲线靠外或xy轴的面积大则对应的模型好
ROC曲线
TPR
FPR
TPR和FPR的关系
程序接着上面的
metrics.py
import numpy as np
from math import sqrtdef accuracy_score(y_true, y_predict):"""计算y_true和y_predict之间的准确率"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum(y_true == y_predict) / len(y_true)def mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的MSE"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum((y_true - y_predict)**2) / len(y_true)def root_mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的RMSE"""return sqrt(mean_squared_error(y_true, y_predict))def mean_absolute_error(y_true, y_predict):"""计算y_true和y_predict之间的MAE"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum(np.absolute(y_true - y_predict)) / len(y_true)def r2_score(y_true, y_predict):"""计算y_true和y_predict之间的R Square"""return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)def TN(y_true, y_predict):assert len(y_true) == len(y_predict)return np.sum((y_true == 0) & (y_predict == 0))def FP(y_true, y_predict):assert len(y_true) == len(y_predict)return np.sum((y_true == 0) & (y_predict == 1))def FN(y_true, y_predict):assert len(y_true) == len(y_predict)return np.sum((y_true == 1) & (y_predict == 0))def TP(y_true, y_predict):assert len(y_true) == len(y_predict)return np.sum((y_true == 1) & (y_predict == 1))def confusion_matrix(y_true, y_predict):return np.array([[TN(y_true, y_predict), FP(y_true, y_predict)],[FN(y_true, y_predict), TP(y_true, y_predict)]])def precision_score(y_true, y_predict):assert len(y_true) == len(y_predict)tp = TP(y_true, y_predict)fp = FP(y_true, y_predict)try:return tp / (tp + fp)except:return 0.0def recall_score(y_true, y_predict):assert len(y_true) == len(y_predict)tp = TP(y_true, y_predict)fn = FN(y_true, y_predict)try:return tp / (tp + fn)except:return 0.0def f1_score(y_true, y_predict):precision = precision_score(y_true, y_predict)recall = recall_score(y_true, y_predict)try:return 2. * precision * recall / (precision + recall)except:return 0.def TPR(y_true, y_predict):tp = TP(y_true, y_predict)fn = FN(y_true, y_predict)try:return tp / (tp + fn)except:return 0.def FPR(y_true, y_predict):fp = FP(y_true, y_predict)tn = TN(y_true, y_predict)try:return fp / (fp + tn)except:return 0.
scikit-learn中的ROC
ROC AUC
auc aera under of curve
两个模型或一个模型对应的两组超参数对应的模型
多分类问题中的混淆矩阵
import numpy as np
import matplotlib.pyplot as pltfrom sklearn import datasetsdigits = datasets.load_digits()
X = digits.data
y = digits.targetfrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.8, random_state=666)from sklearn.linear_model import LogisticRegressionlog_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
log_reg.score(X_test, y_test)y_predict = log_reg.predict(X_test)from sklearn.metrics import precision_scoreprecision_score(y_test, y_predict)
cfm = confusion_matrix(y_test, y_predict)
plt.matshow(cfm, cmap=plt.cm.gray)
plt.show()
cmap color map
row_sums = np.sum(cfm, axis=1)
err_matrix = cfm / row_sums
np.fill_diagonal(err_matrix, 0)plt.matshow(err_matrix, cmap=plt.cm.gray)
plt.show()
第10章 评价分类结果 学习笔记下相关推荐
- 第10章 评价分类结果
分类算法的评价 分类准确度的问题 , , 精准率和召回率 , , , , , , Precision和Recall的平衡 , , , , , , , , , ROC,AUC用来比较两个模型 ...
- 《第8章-图分类》学习笔记
第8章-图分类 与之前的节点分类不同,图分类任务需要学习到整张图的标签和类别.为了解决这类问题,一般使用层次化池化(Pooling)的操作. 8.1 基于全局池化的图分类 全局池化的主要思路是设计一个 ...
- 《机器学习》周志华第10章降维与度量学习 思维导图+笔记+习题
K-Means与LVQ都试图以类簇中心作为原型指导聚类,其中K-Means通过EM算法不断迭代直至收敛,LVQ使用真实类标辅助聚类:高斯混合聚类采用高斯分布来描述类簇原型:密度聚类则是将一个核心对象所 ...
- kafka学习_Kafka学习笔记下
4 Kafka API实战 4.1 环境准备 1)启动zk和kafka集群,在kafka集群中打开一个消费者 [atguigu@hadoop102 kafka]$ bin/kafka-console- ...
- HTML5新特性基础学习笔记下
6.Web储存 客户端储存数据 1.两种方式 1):localStorage - 没有时间限制的数据存储 2):sessionStorage - 针对一个session的存储数据 2. ...
- 深度学习 第3章线性分类 实验四 pytorch实现 Logistic回归 上篇
目录: 第3章 线性分类 3.1 基于Logistic回归的二分类任务 3.1.1 数据集构建 3.1.2 模型构建 1. Logistic函数 2. Logistic回归算子 3.1.3 损失函数 ...
- 深度学习 第3章线性分类 实验四 pytorch实现 Softmax回归 鸢尾花分类任务 下篇
目录: 第3章 线性分类 3.3 实践:基于Softmax回归完成鸢尾花分类任务 3.3.1 数据处理 3.3.1.1 数据集介绍 3.3.1.2 数据清洗 1. 缺失值分析 2. 异常值处理 3.3 ...
- SAS学习笔记(四)第8/9/10章
一.可视化数据 1. ODS图形概述 在统计过程中使用图形: ods图形选项默认是开启的,若你发现是关闭的,则在将要运行的程序之前用以下语句开启:ods graphics on:则支持ods图形的统计 ...
- c语言程序设计第四版乌云高娃,C语言程序设计教学课件作者第3版乌云高娃学习手册C语言程序设计教学课件作者第3版乌云高娃学习手册学习手册第10章文件及其应用课件.docx...
C语言程序设计教学课件作者第3版乌云高娃学习手册C语言程序设计教学课件作者第3版乌云高娃学习手册学习手册第10章文件及其应用课件.docx 学习手册(1):文本文件的操作学习内容文本文件的操作学习目标 ...
- 第10章 项目:多类花朵分类
第10章 项目:多类花朵分类 本章我们使用Keras为多类分类开发并验证一个神经网络.本章包括: 将CSV导入Keras 为Keras预处理数据 使用scikit-learn验证Keras模型 我们开 ...
最新文章
- 在vue中使用Element-UI
- android 自定义图形,Android自定义View之图形图像(模仿360的刷新球自定
- spring源码分析之spring-core-io
- C#方法重载(overload)方法重写(override)隐藏(new)
- Python循环完成剪刀石头布游戏
- leetcode691:Stickers to Spell Word
- fanuc机器人与示教器配对_看FANUC机器人在重力浇铸行业,都是又累又重的活儿啊!...
- 突破变态限制快捷方式提权法
- php脚本是什么,PHP脚本的编写
- 惠普打印机墨盒更换教程_惠普打印机换墨盒图解 惠普打印机怎么换墨盒
- 做直流逆变中用到的全桥逆变电路测试mos管好坏的方法
- web前端程序员两年学习经验与总结
- 使用FFmpeg合并多个MP4视频
- java制作小鱼吃大鱼_大鱼吃小鱼游戏(Java编写)
- python自动发送微信文件_Python脚本定期发送微信文件,定时
- Jumpserver界面设置及界面功能
- 程序员在互联网创业公司工作五年是一种怎样的体验?
- halcon学习实战系列—如何更便捷,更高效的计算同心度
- 希尔伯特-黄变换(HHT)的前世今生——一个从瞬时频率讲起的故事
- LCD1602液晶显示屏使用方法
热门文章
- 出现画面抖动_手机拍照时模糊,抖动?赶紧来拯救你的拍摄技术吧!
- CPaintDC 、CWindowDC、 CClientDC、cMemDC、 CDC
- oracle如何储存超长汉子_厦门到惠州整车运输超长超宽超重运输
- python运算符_零基础学习 Python 之运算符
- 计算机与临床医学的应用,论临床医学教育中计算机系统的应用.pdf
- css居中的几种方法_css两种常用的不定宽高的水平垂直居中方法,记住它,不再为样式发愁...
- ThinkPHP/---微信支付PC流程
- 雷达发现 |最新教育行业数据报告
- MySQL基础操作命令
- 【题解】Inspection UVa 1440 LA 4597 NEERC 2009