---恢复内容开始---

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数组为正即为反,为反即为正)的更多相关文章

  1. sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画

    from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...

  2. 机器学习入门06 - 训练集和测试集 (Training and Test Sets)

    原文链接:https://developers.google.com/machine-learning/crash-course/training-and-test-sets 测试集是用于评估根据训练 ...

  3. sklearn学习3----模型选择和评估(1)训练集和测试集的切分

    来自链接:https://blog.csdn.net/zahuopuboss/article/details/54948181 1.sklearn.model_selection.train_test ...

  4. sklearn——train_test_split 随机划分训练集和测试集

    sklearn——train_test_split 随机划分训练集和测试集 sklearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http: ...

  5. Sklearn-train_test_split随机划分训练集和测试集

    klearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http://scikit-learn.org/stable/modules/gener ...

  6. 随机切分csv训练集和测试集

    使用numpy切分训练集和测试集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 序言 在机器学习的任务中,时常需要将一个完整的数据集切分为训练集和测试集.此处我们使用numpy完成这个任务. ...

  7. sklearn中的train_test_split (随机划分训练集和测试集)

    官方文档:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html ...

  8. 将dataframe分割为训练集和测试集两部分

    data = pd.read_csv("./dataNN.csv",',',error_bad_lines=False)#我的数据集是两列,一列字符串,一列为0,1的labelda ...

  9. 用python制作训练集和测试集的图片名列表文本

    # -*- coding: utf-8 -*- from pathlib import Path #从pathlib中导入Path import os import fileinput import ...

随机推荐

  1. Python 第一个程序

    第一个程序 打开pycharm,新建一个工程,新建一个文件(后缀为.py) 书写最简单的代码:print(人生苦短,我用python!) 执行python代码 使用pycharm的运行按钮 终端下输入 ...

  2. python如何判断字符串是否以某个字母或者数字结尾

    1.如果是对某个确定的字符或者数字进行判断,可以直接使用endswith()方法 # 判断str_a是否以‘A’结尾 str_a = '20190813A' print(str_a.endswith( ...

  3. 免费安装正版Xshell6+Xftp6

    先进入链接获取最新的下载地址 下载地址 填写一下姓名+邮箱,勾选需要哪个就行了 它会发给你指定邮箱一个地址,点击即可下载 安装就不用说了...

  4. mysql5.7主从复制及相关注意点!

    首先在两台不同IP的服务器安装相同版本的mysql(也可以docker 用 network模式).例如 主数据库(master) 178.18.0.2 my.cnf的设置(一般在/etc/mysql/ ...

  5. 13、yum

    1.yum yum是管理rpm包的工具 2.yum源(yum仓库) 要使用yum前,需要准备一个yum源(我们也称为yum仓库), 这个可以是一个互联网上的仓库,也可以是本地自己搭建的仓库. 仓库里面 ...

  6. Ceph实战入门之安部署篇

    最近Ceph官方发布了luminous长久支持版,新版本增加了很多有意思的功能,但是入门还是先从部署安装开始. 环境说明 在Win10下安装VMware® Workstation 12 Pro软件,用 ...

  7. web规范文档说明三

    网站头部:    head/header(头部) top(顶部)导航:   nanv 导航具体区分:topnav(顶部导航).mainnav(主导航).mininav(迷你导航).textnav(导航 ...

  8. Windows KMS激活脚本

    @echo off&setlocal EnableDelayedExpansion&color 3etitle Windows KMS激活脚本 --by David :: 如果激活失败 ...

  9. 【CF1181D】Irrigation

    题目大意:给定 M 个城市,每年会选出一个城市举办比赛,现给出前 N 年城市举办比赛的情况.在接下来的年份中,每年会在举办比赛次数最小的城市举办比赛,如果有很多城市举办次数均为最小值,则在编号最小的城 ...

  10. 【leetcode】1260. Shift 2D Grid

    题目如下: Given a 2D grid of size n * m and an integer k. You need to shift the grid k times. In one shi ...