尽管定义了 random_state,为什么 ML 模型会产生不同的结果?以及如何为 sklearn 设置全局随机种子

数据挖掘 机器学习 深度学习 统计数据 特征选择 表现
2021-09-15 08:30:13

对于类比例为 33:67 的二元分类问题,我一直在对同一组数据运行几个 ML 模型。

在昨天和今天的运行中,我有相同的算法和相同的超参数集。

请注意,我random_state在每个估计器函数中也有参数,如下所示

np.random.seed(42)
svm=SVC()  # i replace the estimator here for diff algos
svm_cv=GridSearchCV(svm,op_param_grid,cv=10,scoring='f1')
svm_cv.fit(X_train_std,y_train)

q1) 为什么即使我已经random_state配置了也会发生这种变化?

q2) 每次跑步时我还应该做些什么来重现相同的结果?

请在下面找到不同的结果?这里auc-Y表示昨天的运行

在此处输入图像描述

1个回答

不是每颗种子都是一样的

这是设置所有种子的确定功能,您可以期待完全的可重复性:

def seed_everything(seed=42):
    """"
    Seed everything.
    """   
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True

你必须导入torch、numpy等。

更新:如何为 sklearn 模型设置全局随机种子:

鉴于 sklearn 没有自己的全局随机种子,但使用 numpy 随机种子,我们可以使用上面的方法全局设置它:

np.random.seed(seed)

这是 scipy 库的一个小实验,类似的是 sklearn(生成随机数 - 通常是权重):

import numpy as np
from scipy.stats import norm
print('Without seed')
print(norm.rvs(100, size = 5))
print(norm.rvs(100, size = 5))

print('With the same seed')
np.random.seed(42) 
print(norm.rvs(100, size = 5))
np.random.seed(42) # reset the random seed back to 42
print(norm.rvs(100, size = 5))

print('Without seed')
np.random.seed(None)
print(norm.rvs(100, size = 5))
print(norm.rvs(100, size = 5))

输出和确认

Without seed
[100.27042599 100.9258397  100.20903163  99.88255017  99.29165699]
[100.53127275 100.17750482  98.38604284 100.74109598 101.54287085]
With the same seed
**[101.36242188 101.13410818 102.36307449  99.74043318  98.83044407]**
**[101.36242188 101.13410818 102.36307449  99.74043318  98.83044407]**
Without seed
[101.2933838  100.52176902 101.38602156 100.72865231  99.02271004]
[100.19080241  99.11010957  99.51578106 101.56403284 100.37350788]