Bar*_*ich 1 python numpy non-deterministic random-forest scikit-learn
我使用这段代码希望实现确定性:
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)
Run Code Online (Sandbox Code Playgroud)
但我的结果并不是确定性的。这是为什么?我该如何解决这个问题?
使用random_state中的参数RandomForestRegressor:
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators=1000, criterion='mse', min_samples_leaf=4,
random_state= 0)
Run Code Online (Sandbox Code Playgroud)
这应该每次都会返回相同的结果。
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