
机器学习
|
373
从图中可以看出,如果使用
100
棵随机决策树,就可以得到一个非常接近我们直觉的“关
于数据空间应该如何分割”的整体模型。
5.8.3
随机森林回归
前面介绍了随机森林分类的内容。其实随机森林也可以用作回归(处理连续变量,而不是
离散变量)。随机森林回归的评估器是 RandomForestRegressor,其语法与我们之前看到的
非常类似。
下面的数据通过快慢振荡组合而成(如图
5-76
所示):
In[10]: rng = np.random.RandomState(42)
x = 10 * rng.rand(200)
def model(x, sigma=0.3):
fast_oscillation = np.sin(5 * x)
slow_oscillation = np.sin(0.5 * x)
noise = sigma * rng.randn(len(x))
return slow_oscillation + fast_oscillation + noise
y = model(x)
plt.errorbar(x, y, 0.3, fmt='o');
图 5-76:随机森林回归数据
通过随机森林回归器,可以获得下面的最佳拟合曲线(如图
5-77
所示):
In[11]: from sklearn.ensemble import RandomForestRegressor
forest = RandomForestRegressor(200)
forest.fit(x[:, ...