【问题标题】:Getting different results each time I run RandomForestRegressor每次运行 RandomForestRegressor 时都会得到不同的结果
【发布时间】:2018-12-24 08:36:21
【问题描述】:

我使用这段代码希望实现确定性:

from sklearn.ensemble import RandomForestRegressor
np.random.seed(0)
import random
random.seed(0)

rf = RandomForestRegressor(n_estimators=1000, criterion='mse', min_samples_leaf=4)

但我的结果不是确定性的。为什么会这样,我该如何解决?

【问题讨论】:

  • 您需要将random_state 添加到您的回归模型中
  • 您可能希望在 RFRegressor 调用中使用 random_state 参数。
  • 在导入 RandomForestRegressor 之前,您应该在脚本顶部添加 np.random.seed()
  • @Baron 看到我的回答并告诉我们

标签: python numpy scikit-learn random-forest non-deterministic


【解决方案1】:

RandomForestRegressor 中使用random_state 参数:

from sklearn.ensemble import RandomForestRegressor

rf = RandomForestRegressor(n_estimators=1000, criterion='mse', min_samples_leaf=4, 
                           random_state= 0)

这应该每次都返回相同的结果。


Scikit-learn 不使用自己的全局随机状态;每当一个 RandomState 实例或整数随机种子不作为 参数,它依赖于 numpy 全局随机状态,可以设置 使用 numpy.random.seed


话虽如此,添加 np.random.seed() 之前 导入 RandomForestRegressor 也应该可以解决问题。

来源:http://scikit-learn.org/stable/faq.html#how-do-i-set-a-random-state-for-an-entire-execution

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2015-04-24
    • 2016-07-31
    • 2020-04-16
    • 2012-05-02
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多