最近看sklearn的源码比较多,好记性不如烂笔头啊,还是记一下吧。
整体:
)实现的代码非常好,模块化、多继承等写的很清楚。
)predict功能通常在该模型的直接类中实现,fit通常在继承的类中实现,方便不同的子类共同引用。
随机森林 和 GBDT
)RandomForest的bootstrap是又放回的;GBDT则是无放回的。
)实现的代码非常好,比如GBDT提供了一些小白不常用的函数【staged_decision_function,staged_predict】之类,对于调试观察每个DT的输出非常有帮助。
)大多数模型的predict都依赖于predict_proba返回的proba,但GBDT的predict依赖于decision_function返回的score,但本质一样,仅记录一下。
)还没观察adaboost如何实现,但GBDT给人的感觉是,这种串行训练模型一般在fit中调用_fit_stages,所以看源码知道重点了吧。GBDT在https://github.com/scikit-learn/scikit-learn/blob/51a765a/sklearn/ensemble/gradient_boosting.py#L747的_fit_stage才是真正的训练函数、L763中给出了训练时使用的base tree是【tree= DecisionTreeRegressor(...)】
)In random forests (see RandomForestClassifier and RandomForestRegressor classes), each tree in the ensemble is built from a sample drawn with replacement (i.e., a bootstrap sample) from the training set. In addition, when splitting a node during the construction of the tree, the split that is chosen is no longer the best split among all features. Instead, the split that is picked is the best split among a random subset of the features. ===》 训练树之前,bootstrap出样本,训练每个节点时,才sample特征。。。。。
)In extremely randomized trees (see ExtraTreesClassifier and

本文详细解析了sklearn的ensemble模型,包括随机森林和GBDT。讨论了模型的实现细节,如RandomForest的bootstrap采样、GBDT的训练过程,以及特征重要性的计算。强调了bootstrap、特征采样、最大深度和学习率的交互作用,并指出在GBDT中,特征重要性基于节点的样本数量和减少的不纯度来计算。同时,对比了两种树构建策略——深度优先和最佳优先,分析了它们对模型的影响。
最低0.47元/天 解锁文章
1283





