我很难优化binar分类的阈值。我使用了3个模型: Logistic回归模型,Catboost模型和Sklearn模型。
对于每个模型,我将执行以下步骤:
1)拟合模型
2)第一类(占数据集5% )的召回率为0.0,零类的召回为1.0。(这不能用gridsearch和class_can=‘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)对测试数据集的概率进行预测,并利用上面计算的optimal_threshold来获取类。
这里有一个问题:当我一次又一次地启动代码时,如果我不修复random_state,那么最优的treshold是变体的,并且会显着地移动。这将导致基于测试样本的准确性度量发生巨大变化。
我是否需要计算一些平均阈值并将其用作一个恒定的硬值?或者我要到处修理random_state?或者也许找到optimal_threshold的方法不正确?
发布于 2020-02-14 13:04:23
如果不将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)https://stackoverflow.com/questions/60215509
复制相似问题