- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用 RandomForestRegressor 时,我注意到一些奇怪的事情。为了说明问题,这里有一个小例子。我在测试数据集上应用了 RandomForestRegressor 并绘制了森林中第一棵树的图。这给了我以下输出:
Root_node:
mse=8.64
samples=2
value=20.4
Left_leaf:
mse=0
samples=1
value=24
Right_leaf:
mse=0
samples=1
value=18
首先,我希望根节点的值为
(24+18)/2=21
.但不知何故,它是 20.4。
1/2[(24-20.4)^2+(18-20.4)^2]=9.36
(假设根值20.4是正确的)
1/2[(24-21)^2+(18-21)^2]=9
.如果我只使用 DecisionTreeRegressor,这也是我得到的结果。
import pandas as pd
from sklearn import tree
from sklearn.ensemble import RandomForestRegressor
import graphviz
# create example dataset
data = {'AGE': [91, 42, 29, 94, 85], 'TAX': [384, 223, 280, 666, 384], 'Y': [19, 21, 24, 13, 18]}
df = pd.DataFrame(data=data)
x = df[['AGE','TAX']]
y = df[['Y']]
rf_reg = RandomForestRegressor(max_depth=2, random_state=1)
rf_reg.fit(x,y)
# plot a single tree of forest
dot_data = tree.export_graphviz(rf_reg.estimators_[0], out_file=None, feature_names=x.columns)
graph = graphviz.Source(dot_data)
graph
和输出图:
最佳答案
tl;博士
这是由于 引导抽样 .
详细 :
使用默认设置 bootstrap=True
, RF 将在构建单个树时使用 bootstrap 采样;引自交叉验证线程 Number of Samples per-Tree in a Random Forest :
If
bootstrap=True
, then for each tree, N samples are drawn randomly with replacement from the training set and the tree is built on this new version of the training data. This introduces randomness in the training procedure since trees will each be trained on slightly different training sets. In expectation, drawing N samples with replacement from a dataset of size N will select ~2/3 unique samples from the original set.
samples=2
,这应该理解为唯一样本的数量;共有
5 个( bootstrap )样本 在根节点中:样本的 2 个副本,带有
y=24
和 3 份带有
y=18
的副本(回想一下,根据引导抽样过程的定义,这里的根节点必须包含 5 个样本,不多也不少)。
# value:
(2*24 + 3*18)/5
# 20.4
# mse:
(2*(24-20.4)**2 + 3*(18-20.4)**2)/5
# 8.64
显然似乎有一些设计选择,无论是在 Graphviz 可视化中还是在底层
DecisionTreeRegressor
中。 ,所以只有
的数量唯一样本被存储/显示,这可能(也可能不是)是打开 Github 问题的一个原因,但这就是目前的情况(说实话,我不确定我自己是否想要实际总数此处显示的样本,包括由于引导抽样而产生的重复项)。
关于python - sklearn RandomForestRegressor 显示的树值中的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63920980/
我知道有几个类似的问题被问到,但我的问题仍然没有得到解答。 问题来了。我使用命令 python3 -m pip3 install -U scikit-learn 来安装 sklearn、numpy 和
_train_weather.values : [[ 0.61818182 0.81645199 0.6679803 ..., 0. 0. 1.
如果我有一个数据集X及其标签Y,那么我将其分为训练集和测试集,scle为0.2,并使用随机种子进行洗牌: 11 >>>X.shape (10000, 50,50) train_data, test_d
首先我查看了所有相关问题。给出了非常相似的问题。 所以我遵循了链接中的建议,但没有一个对我有用。 Data Conversion Error while applying a function to
这里有两种标准化方法: 1:这个在数据预处理中使用:sklearn.preprocessing.normalize(X,norm='l2') 2:分类方法中使用另一种方法:sklearn.svm.Li
所以刚看了一个教程,作者不需要import sklearn使用时 predict anaconda 环境中pickled 模型的功能(安装了sklearn)。 我试图在 Google Colab 中重
我想评估我的机器学习模型。我使用 roc_auc_score() 计算了 ROC 曲线下的面积,并使用 sklearn 的 plot_roc_curve() 函数绘制了 ROC 曲线。在第二个函数中,
我一直在寻找此信息,但在任何地方都找不到,所以这是我的镜头。 我是Python 2.7的初学者,我学习了一个模型,感谢cPickle我保存了它,但现在我想知道是否可以从另一个设备(没有sklearn库
>>> import sklearn.model_selection.train_test_split Traceback (most recent call last): File "", li
在阅读有关使用 python 的 LinearDiscriminantAnalysis 的过程中,我有两种不同的方法来实现它,可在此处获得, http://scikit-learn.org/stabl
我正在使用 sklearn,我注意到 sklearn.metrics.plot_confusion_matrix 的参数和 sklearn.metrics.confusion_matrix不一致。 p
我正在构建一个多标签文本分类程序,我正在尝试使用 OneVsRestClassifier+XGBClassifier 对文本进行分类。最初,我使用 Sklearn 的 Tf-Idf 矢量化来矢量化文本
我想看看模型是否收敛于我的交叉验证。我如何增加或减少 sklearn.svm.SVC 中的时代? 目前: SVM_Model = SVC(gamma='auto') SVM_Model.fit(X_t
有人可以帮助我吗?我很难知道它们之间的区别 from sklearn.model_selection import train_test_split from sklearn.cross_valida
我需要提取在 sklearn.ensemble.BaggingClassifier 中训练的每个模型的概率。这样做的原因是为了估计 XGBoostClassifier 模型的不确定性。 为此,我创建了
无法使用 scikit-learn 0.19.1 导入 sklearn.qda 和 sklearn.lda 我得到: 导入错误:没有名为“sklearn.qda”的模块 导入错误:没有名为“sklea
我正在尝试在 google cloud ai 平台上创建一个版本,但找不到 impute 模块 No module named 'sklearn.impute._base; 'sklearn.impu
我在 PyQt5 中编写了一个 GUI,其中包括以下行 from sklearn.ensemble import RandomForestClassifier 。 遵循this answer中的建议,
我正在做一个 Kaggle 比赛,需要输入一些缺失的数据。我安装了最新的Anaconda(4.5.4)具有所有相关依赖项(即 scikit-learn (0.19.1) )。 当我尝试导入模块时,出现
在安装了所需的模块后,我正在尝试将imblearn导入到我的Python笔记本中。但是,我收到以下错误:。。附加信息:我使用的是一个用Visual Studio代码编写的虚拟环境。。我已经确定venv
我是一名优秀的程序员,十分优秀!