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

1. k_fold = KFold(n_split, shuffle) 构造KFold的索引切割器

k_fold.split(indices) 对索引进行切割。

参数说明:n_split表示切割的份数,假设切割的份数为10,那么有9份是训练集有1份是测试集,shuffle是否进行清洗,indices表示需要进行切割的索引值

  1. import numpy as np
  2. from sklearn.model_selection import KFold
  3.  
  4. indices = np.arange(20)
  5. k_fold = KFold(n_splits=10, shuffle=False)
  6. train_test_set = k_fold.split(indices)
  7. for (train_set, test_set) in train_test_set:
  8. print(train_set)
  9. print(test_set)

2.np.logical_and(pred_issame, test_issame) # 如果pred_issame中的元素和test_issame都是True, 返回的也是True,否者返回的是False

参数说明:pred_issame输入的bool数组,test_issame输入的bool数组

  1. import numpy as np
  2. pred_issame = np.array([True, True, False, False])
  3. actual_issame = np.array([False, True, False, False])
  4. 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数组

  1. import numpy as np
  2. pred_issame = np.array([True, True, False, False])
  3. print(np.logical_not(pred_issame))
  4. # [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最好的索引值

  1. def calculate_roc(thresh, dist, actual_issame):
  2. pre_issame = np.less(dist, thresh)
  3. tp = np.sum(np.logical_and(pre_issame, actual_issame)) # 正样本被预测为正样本
  4. fp = np.sum(np.logical_and(pre_issame, np.logical_not(actual_issame))) # 负样本被预测为正样本
  5. tn = np.sum(np.logical_and(np.logical_not(pre_issame), np.logical_not(actual_issame))) # 负样本被预测为负样本
  6. fn = np.sum(np.logical_and(np.logical_not(pre_issame), actual_issame)) # 正样本被预测为负样本
  7.  
  8. tpr = 0 if tp + tn == 0 else float(tp) / float(tp + fn)
  9. fpr = 0 if tp + fn == 0 else float(tn) / float(fp + tn)
  10. accur = ((tp + tn) / dist.size)
  11. return tpr, fpr, accur
  12. #
  13. import numpy as np
  14. from sklearn.model_selection import KFold
  15. distance = np.array([0.1, 0.2, 0.3, 0.25, 0.33, 0.20, 0.18, 0.24])
  16. actual_issame = np.array([True, True, False, False, False, True, True, False])
  17. k_fold = KFold(n_splits=4, shuffle=False)
  18. indices = np.arange(len(distance))
  19. for k_num, (train_set, test_set) in enumerate(k_fold.split(indices)):
  20. thresholds = np.arange(0, 1, 0.04)
  21. accuracy = np.zeros(len(thresholds))
  22. for threshold_index, threshold in enumerate(thresholds):
  23. _, _, accuracy[threshold_index] = calculate_roc(threshold, distance[train_set], actual_issame[train_set])
  24.  
  25. max_threshold = np.argmax(accuracy)
  26. 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元类

    类也是对象 在理解元类之前,你需要先掌握Python中的类.Python 中的类概念借鉴 Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.当然在 P ...

  2. microsoft office powerpoibt automation 二次开发

    背景 首先office的产品powerpoint是支持二次开发的,这里的二次开发并不是指在powerpoint产品中嵌入一些自己的控件,而是一些简单的automation的控制(进入放映状态,上一页, ...

  3. 2019.10.9php进阶

    <?php header("Content-type:text/html;charset:utf-8"); if ($_FILES["file"][&qu ...

  4. svn 权限设置

    /***********************************************************/ //SVNSubversion 用户权限管理 //资料来源:网络.总结 // ...

  5. Java HashMap的工作原理(转载)

    原文地址:http://www.importnew.com/10620.html 面试的时候经常会遇见诸如:"java中的HashMap是怎么工作的","HashMap的 ...

  6. grunt的安装及使用

    windows下安装grunt需要先安装ruby和nodejsruby -v 测试ruby是否安装成功node -v 测试nodejs是否安装成功npm -v 测试npm是否安装成功(npm是node ...

  7. JQ其他

    关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom定 ...

  8. qt5---滑动条QSlider

    需要    #include <QSlider> #include "win.h" #include <QDebug> #include <QPush ...

  9. springboot整合admin管理平台

    server 端 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  10. 24. ClustrixDB 持久性配置文件

    这些持久性选项不适用于内存中的表.有关更多信息,请参见内存表. ClustrixDB提供了一个选项,通过指定如何提交事务并使其持久,从而提高性能.当提交成功通知应用程序时,用户可以在提交过程中指定提交 ...