是否有一种方法可以强制Random Forest Regressor不适合拦截器?

问题描述 投票:0回答:1

我想知道是否有一种适合sklearn随机森林回归器的方法,使得全0输入将为我提供0预测。对于线性模型,我知道我可以在初始化时简单地传入fit_intercept=False参数,并且我想为随机森林复制此参数。

基于树的模型实现我想要做的事情有意义吗?如果是这样,我该如何实施?

python machine-learning scikit-learn random-forest decision-tree
1个回答
0
投票

简短回答:


长回答:

基于树的模型与线性模型完全不同;树中甚至不存在拦截的概念。

为了了解为什么会这样,我们从documentation(一个具有单个输入功能的决策树)中修改简单的示例:

import numpy as np
from sklearn.tree import DecisionTreeRegressor, plot_tree
import matplotlib.pyplot as plt

# Create a random dataset
rng = np.random.RandomState(1)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - rng.rand(16))

# Fit regression model
regr = DecisionTreeRegressor(max_depth=2)
regr.fit(X, y)

# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
y_pred = regr.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_pred, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.legend()
plt.show()

这里是输出:

enter image description here

粗略地说,决策树试图对数据进行[[局部近似,因此在其Universe中不存在任何全局尝试(例如拦截线)。

回归树实际作为输出返回的是训练样本的因变量y

均值

,该样本在拟合期间最终出现在各个终端节点(叶)中。要看到这一点,让我们绘制上面刚刚装配的树:plt.figure() plot_tree(regr, filled=True) plt.show()
enter image description here

在这个非常简单的玩具示例中遍历树,您应该能够使自己相信X=0的预测为0.052。让我们验证一下:

regr.predict(np.array([0]).reshape(1,-1)) # array([0.05236068])

我通过一个非常简单的决策树说明了上面的内容,以使您了解为何此处不存在拦截的概念;结论是,任何实际上基于决策树并由决策树组成的模型(例如“随机森林”)也应如此。
© www.soinside.com 2019 - 2024. All rights reserved.