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

Posted

技术标签:

【中文标题】使用 scikit-learn 并行生成随机森林【英文标题】:parallel generation of random forests using scikit-learn 【发布时间】:2014-11-12 21:24:00 【问题描述】:

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

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

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

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

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

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

任何帮助将不胜感激。

【问题讨论】:

如果目标是预测,则没有必要组合不同的模型。您可以通过单独的模型进行预测,然后仅组合结果。 同意@DrDom,有很多方法可以集成模型。详细说明您想如何做到这一点非常重要。 @DrDom 我同意,如果这只是预测,那么我可以结合结果。但是,我不仅对预测感兴趣,而且对特征的可变重要性感兴趣。 @reddy,变量重要性是变量被打乱时预测误差的平均变化。因此,不同模型的平均重要性应该大约等于随机森林集合的变量重要性值。如果变量重要性值之前没有以其他方式缩放或修改,则这是有效的。无论如何,变量重要性不是一个固定数字,因为它的值取决于随机数。 UPD:如果每个模型中的树数不同,那么在计算平均重要性时需要考虑这一点。 【参考方案1】:

当然,只是聚合所有的树,例如看看这个来自pyrallel的sn-p:

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

【讨论】:

感谢您的信息。我会试试这个。顺便说一句,final_ensemble 是一个随机森林对象(在这种情况下),以及在这个过程中如何处理 feature_importances_ 属性。 这是一个 Python 属性:访问时会自动重新计算:github.com/scikit-learn/scikit-learn/blob/master/sklearn/… 太棒了。谢谢,这正是我想要的。 谢谢,但当其中一个随机森林接受的标签比另一个多时,此方法不起作用。【参考方案2】:

根据您的编辑,听起来您只是在询问如何提取特征重要性并查看随机森林中使用的单个树。如果是这样,这两个都是您的随机森林模型的属性,分别命名为“feature_importances_”和“estimators_”。可以在下面找到说明这一点的示例:

>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.datasets import make_blobs
>>> X, y = make_blobs(n_samples=10000, n_features=10, centers=100,random_state=0)
>>> clf = RandomForestClassifier(n_estimators=5, max_depth=None, min_samples_split=1, random_state=0)
>>> clf.fit(X,y)
RandomForestClassifier(bootstrap=True, compute_importances=None,
            criterion='gini', max_depth=None, max_features='auto',
            min_density=None, min_samples_leaf=1, min_samples_split=1,
            n_estimators=5, n_jobs=1, oob_score=False, random_state=0,
            verbose=0)
>>> clf.feature_importances_
array([ 0.09396245,  0.07052027,  0.09951226,  0.09095071,  0.08926362,
        0.112209  ,  0.09137607,  0.11771107,  0.11297425,  0.1215203 ])
>>> clf.estimators_
[DecisionTreeClassifier(compute_importances=None, criterion='gini',
            max_depth=None, max_features='auto', min_density=None,
            min_samples_leaf=1, min_samples_split=1,
            random_state=<mtrand.RandomState object at 0x2b6f62d9b408>,
            splitter='best'), DecisionTreeClassifier(compute_importances=None, criterion='gini',
            max_depth=None, max_features='auto', min_density=None,
            min_samples_leaf=1, min_samples_split=1,
            random_state=<mtrand.RandomState object at 0x2b6f62d9b3f0>,
            splitter='best'), DecisionTreeClassifier(compute_importances=None, criterion='gini',
            max_depth=None, max_features='auto', min_density=None,
            min_samples_leaf=1, min_samples_split=1,
            random_state=<mtrand.RandomState object at 0x2b6f62d9b420>,
            splitter='best'), DecisionTreeClassifier(compute_importances=None, criterion='gini',
            max_depth=None, max_features='auto', min_density=None,
            min_samples_leaf=1, min_samples_split=1,
            random_state=<mtrand.RandomState object at 0x2b6f62d9b438>,
            splitter='best'), DecisionTreeClassifier(compute_importances=None, criterion='gini',
            max_depth=None, max_features='auto', min_density=None,
            min_samples_leaf=1, min_samples_split=1,
            random_state=<mtrand.RandomState object at 0x2b6f62d9b450>,
            splitter='best')]

【讨论】:

以上是关于使用 scikit-learn 并行生成随机森林的主要内容,如果未能解决你的问题,请参考以下文章

使用 scikit-learn 和 Flask 对随机森林回归器建模

scikit-learn 随机森林过多的内存使用

在 scikit-learn 中平均多个随机森林模型

scikit-learn 中的随机森林解释

并行拟合 scikit-learn 模型?

使用 Scikit-Learn 在 Python 中绘制随机森林的树