zoukankan      html  css  js  c++  java
  • 使用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])

      

    ---恢复内容结束---

  • 相关阅读:
    QT5每日一学(五)QT布局管理器
    构造注入链:POP
    Jarvis OJ--PHPINFO
    XCTF(Web_php_unserialize)
    XCTF-反序列化中_wakeup()函数
    session反序列化
    PHP反序列化
    默认安装的phpMyAdmin会存在哪些安全隐患
    如何进行数据库备份
    开启mysql外部访问(root外连)
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/11352581.html
Copyright © 2011-2022 走看看