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

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. Elasticsearch 7.4.0官方文档操作

    官方文档地址 https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 1.0.0 设置Elasticsea ...

  2. MySQL面试题(二)

    ● 请你说一说mysql的四种隔离状态 参考回答: Mysql主要包含四种隔离状态: 事务隔离级别 脏读 不可重复读 幻读 读未提交(read-uncommitted) 是 是 是 不可重复读(rea ...

  3. json字符串与json对象的转换

    JSON(JavaScript Object Notation)格式是开发中较为常见的数据格式,优点是轻量,便于理解和解析生成.JSON对象是一个无序的键值对集合,以 { } 为开头和结尾,键与键之间 ...

  4. optparse:让你轻松地与命令行打交道

    介绍 一个专门用于命令行参数解析的模块 使用 import optparse op = optparse.OptionParser() # 添加选项 op.add_option("--s&q ...

  5. select,poll.epoll区别于联系

    select,poll,epoll都是IO多路复用中的模型.再介绍他们特点时,先来看看多路复用的 模型. 同其他IO的不同的是,IO多路复用一次可以等多个文件描述符.大大提高了等待数据准备好的时间的效 ...

  6. 自动化运维——MySQL备份脚本(二)

    使用if语句编写MySQL备份脚本 代码: #!/bin/bash #auro backup mysql db #by steve yu #define backup path BAK_DIR=/da ...

  7. Python with open 使用技巧

    在使用Python处理文件的是,对于文件的处理,都会经过三个步骤:打开文件->操作文件->关闭文件.但在有些时候,我们会忘记把文件关闭,这就无法释放文件的打开句柄.这可能觉得有些麻烦,每次 ...

  8. python 单元测试_unittest(七)

    一.unittest中各阶段的核心概念:TestCase, TestSuite, 断言函数, TextTestRunner,TestFixture TestCase:所用用例的基类,软件测试中基本的测 ...

  9. jvm的几篇文章

    https://www.cnblogs.com/xrq730/category/731395.html

  10. Mac 安装RN android开发环境

    前言 前面介绍了MAC 安装,再来讲讲mac 安装 安卓的开发环境 首先貌似很多Mac自带安卓JDK ,你可以在终端上输入java -version 看是否已经有java开发环境. 如果没有java开 ...