我所读到的关于随机森林的所有资料都表明,它们不需要缩放输入,而且缩放不应该影响模型的构建。下面引用另一个SE问题(https://stats.stackexchange.com/questions/255765/does-random-forest-need-input-variables-to-be-scaled-or-centered):
随机森林是基于树划分算法的。因此,在一般回归策略中获得的系数没有任何相似之处,这取决于自变量的单位。相反,我们可以获得一组分区规则,基本上是一个给定阈值的决策,而且这种情况不应该随着缩放而改变。换句话说,树木只看到特征中的等级。基本上,数据的任何单调转换都不应该改变林(在最常见的实现中)。
这是我目前正在使用的。如果去掉权重乘数,就会得到一个不同的模型(即,与model.score
不同的值和不同的树深),尽管在这两种情况下都设置了random_state=0
。
model = RandomForestRegressor(n_estimators=10, criterion='mse', random_state=0)
weights = np.arange(1,self.x_train.shape[1]+1)[None,:]
# weights = [[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]]
model.fit(self.x_train * weights, self.y_train)
相比之下,我注意到如果我使用XGBRegressor
而不是RandomForestRegressor
,缩放不会改变模型。
我是否犯了一个明显的错误,或者上面的解释不正确?
发布于 2020-05-16 05:14:59
在尝试用随机数重新创建这个问题(一开始失败)之后,我发现问题来自这样一个事实:我使用的x_train
数据包含的列具有非常小的、接近于零的值。
要重新创建,第一部分只运行一次:
scale = 0.0001 # making this larger eliminates the issue
x_train = np.random.uniform(0,scale,size=(1000,10))
y_train = np.random.uniform(0,1,size=(x_train.shape[0]))
然后对x_train
和y_train
使用相同的值,运行下面的小节,但是use_weights
设置为True
,然后是False
。
use_weights = True
model = RandomForestRegressor(n_estimators=10, random_state=0)
if use_weights:
weights = np.arange(1,x_train.shape[1]+1)[None,:]
model.fit(x_train * weights, y_train)
prediction = model.predict(x_train * weights)
else:
model.fit(x_train, y_train)
prediction = model.predict(x_train)
print(prediction[0]) # changes based on use_weights value assuming scale is very small
另外,实际数据集的y_train
值也非常小,我必须将它们乘以100或更多才能使模型运行。也就是说,如果不扩展y_train
值的规模(通过在每棵树上运行get_depth
方法来确认),它根本不会创建任何叶子。
我想知道,这是一个纯粹的数值不精确的问题,还是它是一个独特的随机森林计算,正在发生的罩下?
https://datascience.stackexchange.com/questions/74258
复制相似问题