首页 文章

如何在Scikit-Learn中的随机森林分类器中设置子样本大小?特别是对于不 balancer 的数据

提问于
浏览
3

目前,我正在Sklearn中为我的不 balancer 数据实现RandomForestClassifier . 我不太清楚RF在Sklearn中的工作方式 . 以下是我的担忧如下:

  • 根据文件,似乎没有办法为每个树木学习者设置子样本大小(即小于原始数据大小) . 但实际上,在随机森林算法中,我们需要获得每个树的样本子集和特征子集 . 我不确定我们能通过Sklearn实现这一目标吗?如果有,怎么样?

Follwoing是Sklearn中RandomForestClassifier的描述 .

“随机森林是一种元估计器,它可以在数据集的各个子样本上拟合多个决策树分类器,并使用平均值来提高预测精度和控制过度拟合 . The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if bootstrap=True (default) . ”

在这里我发现了类似的问题 . 但这个问题的答案并不多 .

How can SciKit-Learn Random Forest sub sample size may be equal to original training data size?

  • 对于不 balancer 数据,如果我们可以通过Sklearn进行子样本提取(即解决上面的问题#1),我们可以做 balancer 随机森林吗?即,对于每个树木学习者,它将从人口较少的类中获取子集,并且还从更多人类中选取相同数量的样本以构成具有两个类的相等分布的整个训练集 . 然后重复该过程一段时间(即树木数量) .

谢谢!程

1 回答

  • 2

    没有明显的方法,但你可以在 sklearn.ensemble.forest 中进入采样方法 .

    通过使用 set_rf_samples(n) ,可以强制树对n行进行子采样,并调用 reset_rf_samples() 对整个数据集进行采样 .

    from sklearn.ensemble import forest
    
    def set_rf_samples(n):
        """ Changes Scikit learn's random forests to give each tree a random sample of
        n random rows.
        """
        forest._generate_sample_indices = (lambda rs, n_samples:
            forest.check_random_state(rs).randint(0, n_samples, n))
    
    def reset_rf_samples():
        """ Undoes the changes produced by set_rf_samples.
        """
        forest._generate_sample_indices = (lambda rs, n_samples:
            forest.check_random_state(rs).randint(0, n_samples, n_samples))
    

    参考:fast.ai

相关问题