gpt4 book ai didi

python - 使用 scikit-learn 并行生成随机森林

转载 作者:太空狗 更新时间:2023-10-29 20:20:05 25 4
gpt4 key购买 nike

主要问题:如何在 python 和 scikit-learn 中组合不同的随机森林?

我目前正在使用 R 中的 randomForest 包来使用弹性映射减少生成随机森林对象。这是为了解决分类问题。

由于我的输入数据太大,一台机器的内存无法容纳,我将数据采样成较小的数据集,并生成包含较小树集的随机森林对象。然后,我使用修改后的组合函数将不同的树组合在一起,以创建一个新的随机森林对象。这个随机森林对象包含特征重要性和最终的树集。这不包括树的 oob 错误或投票。

虽然这在 R 中运行良好,但我想使用 scikit-learn 在 Python 中做同样的事情。我可以创建不同的随机森林对象,但我没有办法将它们组合在一起形成一个新对象。谁能指出一个可以结合森林的功能?这可能使用 scikit-learn 吗?

这里是关于如何在 R 中执行此过程的问题的链接:Combining random forests built with different training sets in R .

编辑:生成的随机森林对象应包含可用于预测的树以及特征重要性。

如有任何帮助,我们将不胜感激。

最佳答案

当然,只需聚合所有树,例如查看来自 pyrallel 的片段:

def combine(all_ensembles):
"""Combine the sub-estimators of a group of ensembles

>>> from sklearn.datasets import load_iris
>>> from sklearn.ensemble import ExtraTreesClassifier
>>> iris = load_iris()
>>> X, y = iris.data, iris.target

>>> all_ensembles = [ExtraTreesClassifier(n_estimators=4).fit(X, y)
... for i in range(3)]
>>> big = combine(all_ensembles)
>>> len(big.estimators_)
12
>>> big.n_estimators
12
>>> big.score(X, y)
1.0

"""
final_ensemble = copy(all_ensembles[0])
final_ensemble.estimators_ = []

for ensemble in all_ensembles:
final_ensemble.estimators_ += ensemble.estimators_

# Required in old versions of sklearn
final_ensemble.n_estimators = len(final_ensemble.estimators_)

return final_ensemble

关于python - 使用 scikit-learn 并行生成随机森林,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com