【问题标题】:Optimal threshold for imbalanced binar classification problem不平衡二元分类问题的最佳阈值
【发布时间】:2020-05-29 14:45:04
【问题描述】:

我无法优化二元分类的阈值。我正在使用 3 个模型:逻辑回归、Catboost 和 Sklearn RandomForestClassifier。

对于每个模型,我都在执行以下步骤:

1) 拟合模型

2) 第一类(属于数据集的 5%)的召回率为 0.0,零类的召回率为 1.0。 (这不能用 gridsearch 和 class_weight='balanced' 参数来解决。)>:(

3) 找到最佳阈值

fpr, tpr, thresholds = roc_curve(y_train, model.predict_proba(X_train)[:, 1])
optimal_threshold = thresholds[np.argmax(tpr - fpr)]

4) 两个课程的召回率都达到 70 左右。

5) 预测测试数据集的概率,并使用我上面计算的最佳阈值来获取类。

问题来了: 当我一次又一次地启动代码时,如果我不修复 random_state,最佳阈值会发生变化,并且会急剧变化。这导致基于测试样本的准确度指标发生巨大变化。

我是否需要计算一些平均阈值并将其用作恒定的硬值?或者也许我必须到处修复 random_state ?或者可能是找到最佳阈值的方法不正确?

【问题讨论】:

    标签: python scikit-learn classification roc


    【解决方案1】:

    如果您不将random_state 设置为固定值,则每次运行的结果都会有所不同。要获得可重复的结果集 random_state 在任何需要固定值的地方,或者使用固定的 numpy 随机种子 numpy.random.seed

    https://scikit-learn.org/stable/faq.html#how-do-i-set-a-random-state-for-an-entire-execution

    Scikit FAQ 提到最好在需要的地方使用random_state 而不是全局随机状态。

    全局随机状态示例:

    import numpy as np
    np.random.seed(42)
    

    一些本地设置random_state的例子:

    X_train, X_test, y_train, y_test = train_test_split(sample.data, sample.target, test_size=0.3, random_state=0)
    
    skf =  StratifiedKFold(n_splits=10, random_state=0, shuffle=True)
    
    classifierAlgorithm = LGBMClassifier(objective='binary', random_state=0)
    

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2023-03-17
      • 2021-09-27
      • 1970-01-01
      • 2020-07-29
      • 2013-06-07
      • 1970-01-01
      • 2020-06-23
      • 2015-11-14
      相关资源
      最近更新 更多