【问题标题】:RandomizedSearchCV and GridsearchCV results are not reproducibleRandomizedSearchCV 和 GridsearchCV 结果不可重现
【发布时间】:2018-01-31 21:43:10
【问题描述】:

我正在 python 中执行 RandomForest 和 AdaBoost 回归

我的结果不可重现 [我的预测每次运行都使用相同的数据和代码]

seed = np.random.seed(22)
rng = np.random.RandomState(1)      

param_grid = {'n_estimators': [10, 100, 1000]}
model_rfr = GridSearchCV(RandomForestRegressor(random_state = rng), param_grid, cv=3, n_jobs=-1, verbose=1)
model_rfr.fit(train_x1,train_y1)
test_rfr = model_rfr.predict(test_y1)
param_grid = {"n_estimators":[100,500],"learning_rate":list(np.linspace(0.01,1,10)),"loss":["linear", "square", "exponential"]}
model_adr = RandomizedSearchCV(AdaBoostRegressor(DecisionTreeRegressor()), param_grid,n_jobs=-1,n_iter=10,cv=3,random_state = rng)
model_adr.fit(train_x1,train_y1)
test_adr = model_adr.fit(test_y1)

这里 test_adr 和 test_rfr 的值会发生变化,每一次,我都会运行我的代码。

请使用任何样本数据进行回归。但请建议如何使我的结果可重现。

【问题讨论】:

  • ++ 现在 Gridsearch 结果可重现,建议使用 RandomizedCV
  • 另外,predict() 方法应该使用test_x1 发送,而您发送的是test_y1,并且代码的最后一行应该是model_adr.predict(),而不是model_adr.fit()
  • AdaBoostRegressor 和 DecisionTreeRegressor 也有 random_state 参数。有了这些,我可以复制结果
  • 感谢您的帮助,我知道了。但是有了一些 random_state 值,我得到了更好的结果,我可以优化它吗?并使其可重现?
  • 不,random_state 不是为了优化。认为这是幸运的。这可能并非每次都如此。只相信多重交叉验证的平均输出。

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


【解决方案1】:

感谢您的贡献。请找到可重现的结果代码。

seed = np.random.seed(22)
rng = np.random.RandomState(1)
param_grid = {'n_estimators': [10, 100, 1000]}
model_rfr = GridSearchCV(RandomForestRegressor(random_state = rng), param_grid, cv=3, n_jobs=-1, verbose=1)
model_rfr.fit(train_x1,train_y1)
test_rfr = model_rfr.predict(test_y1)
param_grid = {"n_estimators":[100,500],"learning_rate":list(np.linspace(0.01,1,10)),"loss":["linear", "square", "exponential"]}
model_adr = RandomizedSearchCV(AdaBoostRegressor(DecisionTreeRegressor(random_state = rng)), param_grid,n_jobs=-1,n_iter=10,cv=3,random_state = rng)
model_adr.fit(train_x1,train_y1)
test_adr = model_adr.predict(test_x1)

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 2018-06-18
    • 2019-11-13
    • 2019-09-10
    • 2019-11-28
    • 2016-07-16
    • 2017-05-29
    • 2020-11-01
    • 2020-03-12
    相关资源
    最近更新 更多