【问题标题】:Random Forest Regressor- Python is not reproducible随机森林回归器 - Python 不可重现
【发布时间】:2018-05-10 14:17:33
【问题描述】:

我正在尝试检查受 n_estimators 影响的随机森林回归器性能。

seed = np.random.seed(1962)
rng = np.random.RandomState(1962)

np.random.seed(1962)

estimators = [pow(2,3),10,pow(2,4),pow(2,5),pow(2,6),pow(2,7),pow(2,8),500,pow(2,9),pow(2,10),pow(2,11)]
#oob_train = {}
train_acc = {}
test_acc = {}
for w in range(0,len(estimators),1):
    modelrfe = RandomForestRegressor(n_estimators = estimators[w],random_state=rng, n_jobs = -1)
    model_params = estimators[w]
    modelrfe.fit(train_x1,train_y1)
    train_acc[model_params] = mean_absolute_error(scale_data.inverse_transform(train_y1.reshape(-1,1)),scale_data.inverse_transform(modelrfe.predict(train_x1).reshape(-1,1)))
    test_acc[model_params] = mean_absolute_error(scale_data.inverse_transform(test_y1.reshape(-1,1)),scale_data.inverse_transform(modelrfe.predict(test_x1).reshape(-1,1)))



train_acc = pd.DataFrame(train_acc.items())
train_acc.columns = ['keys','Trainerror']
test_acc = pd.DataFrame(test_acc.items())
test_acc.columns = ['keys','Testerror']
error_df3 = pd.merge(train_acc, test_acc, on='keys')
error_df3 = pd.DataFrame(error_df3)

不可复现 我一开始也定义了 rng。

注意:想象一个 For 循环 1:nrow(dataframe) 为每个 1 它通过多个模型 & 我在 For 循环的开头定义了 rng 和 Seed。

帮帮我! .

2 个样本输出,理想情况下应该是这样。这里的键是指n_estimators

[在此处输入图像描述][模拟 1] [在此处输入图片描述][模拟2]

【问题讨论】:

  • 您忘记附加图片了。另外,最好能提供MCVE
  • 它的代码很长,所以我没有添加任何相关的。我正在询问 Algo 以使其可重现,例如在其中使用 random_state 变量

标签: python scikit-learn regression random-forest reproducible-research


【解决方案1】:

请找出答案:

错误:我使用的是rng = A Randomstate Instance [ np.random.RandomState(1962)]

理想情况下,我应该在 random_state 变量中将种子值作为 int 提及。

rng = 1962 为随机实例播种。

那么我们应该继续使用模型中的 random_state 变量来实现可重复性。

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 2013-05-06
    • 2017-01-06
    • 2023-03-14
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2016-04-24
    相关资源
    最近更新 更多