【发布时间】:2020-01-16 05:15:06
【问题描述】:
在 sklearn 的 RF 拟合函数(或大多数 fit() 函数)中,可以传入“sample_weight”参数来衡量不同的点。默认情况下,所有点的权重相等,如果我将 1 的数组作为 sample_weight 传递,它确实匹配没有参数的原始模型。
但是,如果我将 0.1s 或 1/len(array) 的数组作为 sample_weight 传递,它会改变模型(现在的预测不同),尽管点仍然是同等权重。这是令人不安的,因为体重缩放似乎很重要。那么缩放的正确方法是什么,以便我有一个独特的解决方案?
下面的例子:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
boston = datasets.load_boston()
X = boston.data
y = boston.target
reg = RandomForestRegressor(random_state=1, n_estimators=10)
reg.fit(X, y)
reg_eq = RandomForestRegressor(random_state=1, n_estimators=10)
reg_eq.fit(X, y, sample_weight=np.full(len(y),1))
reg_eq_bad = RandomForestRegressor(random_state=1, n_estimators=10)
reg_eq_bad.fit(X, y, sample_weight=np.full(len(y),0.1))
xt = X[:20]
print(reg.predict(xt))
print(reg_eq.predict(xt))
print(reg_eq_bad.predict(xt))
np.testing.assert_array_almost_equal(reg.predict(xt),reg_eq.predict(xt))
np.testing.assert_array_almost_equal(reg.predict(xt),reg_eq_bad.predict(xt)) # 75% mismatch
【问题讨论】:
-
这个问题真的可以用minimal reproducible example 来解决,通过改编文档中的一些例子应该很简单。
标签: scikit-learn random-forest