“max_features”如何限制sklearn集成模型中的特征数量?

2024-10-08 16:40:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我仍然不完全理解sklearn分类器中的max_features。这个documentation给解释留下了一点空间。出于这个问题的目的,假设我正在使用一个基于树的分类器,例如决策树、随机林、梯度提升等等

例如,如果我要设置max_features=10,这是否意味着每个估计器将从我的数据集中随机抽取10个特征来构建整个树,还是意味着每次分割节点时,每个估计器都会随机抽取10个特征,并选择熵降低最大的特征

我的理解是,在整个过程中,单个估计员将自己限制为10个变量。然而,似乎在每个节点上重置了最大数量的功能。也就是说,对于任何给定节点,估计器随机选择10个特征,选择最佳特征,分割节点,并对所有后续节点重复该过程

from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(max_features=10, n_estimators=150)

Tags: 数据目的决策树节点分类器过程documentation空间
2条回答

However, it seems that the maximum number of features resets at each node. That is, for any given node, the estimator randomly selects 10 features, chooses the best one, splits the node and repeats the process for all subsequent nodes.

你在这里的理解是正确的。梯度提升和随机森林的工作方式是,在每个树的每个分割处,他们将随机选择max_features(在文献中,该参数称为mtry)进行评估。这是一种机制,通过该机制,模型通过在每次分割时不评估每个特征而在模型之间引入随机性

作为TG61591答案的补充,如果您深入研究code,您可以找到一条附加注释,该注释添加了一些关于max_features超参数在模型中如何工作的有用信息:

Notes - The features are always randomly permuted at each split. Therefore, the best found split may vary, even with the same training data and max_features=n_features, if the improvement of the criterion is identical for several splits enumerated during the search of the best split. To obtain a deterministic behaviour during fitting, random_state has to be fixed.

此外,关键概念是随机森林需要增加随机性,以减少模型的方差(尽管这可能会导致更高的偏差),正如本最后说明中所警告的:

Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.

相关问题 更多 >

    热门问题