使用KFold进行训练集和验证集的拆分,使用准确率和召回率来挑选合适的阈值(threshold) 1.KFold(进行交叉验证) 2.np.logical_and(两bool数组都是正即为正) 3.np.logical_not(bool数组为正即为反,为反即为正)
---恢复内容开始---
1. k_fold = KFold(n_split, shuffle) 构造KFold的索引切割器
k_fold.split(indices) 对索引进行切割。
参数说明:n_split表示切割的份数,假设切割的份数为10,那么有9份是训练集有1份是测试集,shuffle是否进行清洗,indices表示需要进行切割的索引值
import numpy as np
from sklearn.model_selection import KFold indices = np.arange(20)
k_fold = KFold(n_splits=10, shuffle=False)
train_test_set = k_fold.split(indices)
for (train_set, test_set) in train_test_set:
print(train_set)
print(test_set)
2.np.logical_and(pred_issame, test_issame) # 如果pred_issame中的元素和test_issame都是True, 返回的也是True,否者返回的是False
参数说明:pred_issame输入的bool数组,test_issame输入的bool数组
import numpy as np
pred_issame = np.array([True, True, False, False])
actual_issame = np.array([False, True, False, False])
print(np.logical_and(pred_issame, actual_issame))
# [False True False False]
3. np.logical_not(pred_issame) # 将输入的True转换为False,False转换为Train
参数说明: pred_issame 表示输入的bool数组
import numpy as np
pred_issame = np.array([True, True, False, False])
print(np.logical_not(pred_issame))
# [False False True True]
第一步:构造indices的索引值,使用KFold对incides进行train_set和test_set的生成
第二步: 使用np.arange(0, 4, 0.4) 构造threshold的列表,循环threshold列表
第三步:
第一步: 使用np.less(dist, threshold) 来获得预测结果
第二步:
tp = np.logical_and(pred_issame, actual_issame) # 正样本被判定为正样本
fp = np.logical_and(pre_issame, np.logical_not(actual_issame)) # 负样本被判断为正样本
tn = np.logical_and(np.logical_not(pre_issame), np.logical_not(actual_issame)) # 负样本判断为负样本
fn = np.logical_and(np.logical_not(pre_issame), actual_issame) # 正样本被判断为负样本
tpr = 0 if tp + fn == 0 else float(tp) / float(tp + fn) # 召回率
fpr = 0 if fp + tn == 0 else float(tn) / float(fp + tn)
accur = (tp + tn) / (tp+fp+fn+tn)
第四步:使用threshold_max = np.argmax(accur) # 获得准确率最大的索引值,即为thresholds最好的索引值
def calculate_roc(thresh, dist, actual_issame):
pre_issame = np.less(dist, thresh)
tp = np.sum(np.logical_and(pre_issame, actual_issame)) # 正样本被预测为正样本
fp = np.sum(np.logical_and(pre_issame, np.logical_not(actual_issame))) # 负样本被预测为正样本
tn = np.sum(np.logical_and(np.logical_not(pre_issame), np.logical_not(actual_issame))) # 负样本被预测为负样本
fn = np.sum(np.logical_and(np.logical_not(pre_issame), actual_issame)) # 正样本被预测为负样本 tpr = 0 if tp + tn == 0 else float(tp) / float(tp + fn)
fpr = 0 if tp + fn == 0 else float(tn) / float(fp + tn)
accur = ((tp + tn) / dist.size)
return tpr, fpr, accur
#
import numpy as np
from sklearn.model_selection import KFold
distance = np.array([0.1, 0.2, 0.3, 0.25, 0.33, 0.20, 0.18, 0.24])
actual_issame = np.array([True, True, False, False, False, True, True, False])
k_fold = KFold(n_splits=4, shuffle=False)
indices = np.arange(len(distance))
for k_num, (train_set, test_set) in enumerate(k_fold.split(indices)):
thresholds = np.arange(0, 1, 0.04)
accuracy = np.zeros(len(thresholds))
for threshold_index, threshold in enumerate(thresholds):
_, _, accuracy[threshold_index] = calculate_roc(threshold, distance[train_set], actual_issame[train_set]) max_threshold = np.argmax(accuracy)
print(thresholds[max_threshold])
---恢复内容结束---
使用KFold进行训练集和验证集的拆分,使用准确率和召回率来挑选合适的阈值(threshold) 1.KFold(进行交叉验证) 2.np.logical_and(两bool数组都是正即为正) 3.np.logical_not(bool数组为正即为反,为反即为正)的更多相关文章
- sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画
from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...
- 机器学习入门06 - 训练集和测试集 (Training and Test Sets)
原文链接:https://developers.google.com/machine-learning/crash-course/training-and-test-sets 测试集是用于评估根据训练 ...
- sklearn学习3----模型选择和评估(1)训练集和测试集的切分
来自链接:https://blog.csdn.net/zahuopuboss/article/details/54948181 1.sklearn.model_selection.train_test ...
- sklearn——train_test_split 随机划分训练集和测试集
sklearn——train_test_split 随机划分训练集和测试集 sklearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http: ...
- Sklearn-train_test_split随机划分训练集和测试集
klearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http://scikit-learn.org/stable/modules/gener ...
- 随机切分csv训练集和测试集
使用numpy切分训练集和测试集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 序言 在机器学习的任务中,时常需要将一个完整的数据集切分为训练集和测试集.此处我们使用numpy完成这个任务. ...
- sklearn中的train_test_split (随机划分训练集和测试集)
官方文档:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html ...
- 将dataframe分割为训练集和测试集两部分
data = pd.read_csv("./dataNN.csv",',',error_bad_lines=False)#我的数据集是两列,一列字符串,一列为0,1的labelda ...
- 用python制作训练集和测试集的图片名列表文本
# -*- coding: utf-8 -*- from pathlib import Path #从pathlib中导入Path import os import fileinput import ...
随机推荐
- 自己实现一个简化版的SpringMVC框架
废话不多说,我们进入今天的正题,在Web应用程序设计中,MVC模式已经被广泛使用.SpringMVC以DispatcherServlet为核心,负责协调和组织不同组件以完成请求处理并返回响应的工作,实 ...
- 关于Linux防火墙的问题以及关闭,试一下这四条命令
关闭防火墙,依次执行以下四条命令 临时服务 service firewalld stop 永久关闭 chkconfig iptables off 列出所有规则 iptables -L 清除所有规则,可 ...
- kubeadm安装k8s1.13
1.环境介绍: centos 7.4.1708 关闭selinux和iptable,环境很重要! 主机 ip地址 cpu核数 内存 swap host解析 k8s-master 10.0.0.11 2 ...
- Bss段的作用及初始化
初始化的全局变量:数据段 局部变量:栈 malloc:堆 未初始化的全局变量:Bss段 arm-linux-readelf -a 应用程序 可查看文件运行架构.大小端.共享库等信息 初始化Bss ...
- 自学Python6.5-内置模块(re、collections )
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- ubuntu16.04安装cuDNN
cudnn的安装非常简单 (1)下载安装文件 按需求下载cudnn的安装文件:https://developer.nvidia.com/rdp/cudnn-archive Tar File的下载如下图 ...
- 牛客小白月赛12 J 月月查华华的手机 (序列自动机模板题)
链接:https://ac.nowcoder.com/acm/contest/392/J 来源:牛客网 题目描述 月月和华华一起去吃饭了.期间华华有事出去了一会儿,没有带手机.月月出于人类最单纯的好奇 ...
- hadoop常见命令
常用命令 启动Hadoop 进入HADOOP_HOME目录. 执行sh bin/start-all.sh 关闭Hadoop 进入HADOOP_HOME目录. 执行sh bin/stop-all.sh ...
- QT的DPI支持
在main函数第一行加入: QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 鼠标不按下也响应移动事件: setMouseTra ...
- Katalon Studio入门学习之三种获取元素方式
Katalon Studio中元素属性定位有三种方式,分别是XPath.Attributes(元素).CSS(样式),KS的界面展示如右图 打开网站,按F12或进入浏览器设置->更多工具-> ...