随机森林回归 - Python不可重现

时间:2017-11-27 10:58:18

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

我正在尝试检查由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(数据帧)为每个1经过多个模型&我已经定义了rng&种子在For循环的开头。

帮帮我吧!

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

[在此处输入图像说明] [模拟1] [在此输入图像说明] [模拟2]

1 个答案:

答案 0 :(得分:0)

请找到答案:

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

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

rng = 1962播种随机实例。

然后我们应该继续使用模型中的random_state vairable进行再现。

相关问题