【问题标题】:What is stratified bootstrap?什么是分层引导?
【发布时间】:2016-05-21 13:43:38
【问题描述】:

我已经学会了引导和分层。但是什么是分层引导?它是如何工作的?

假设我们有一个包含 n 个实例(观察)的数据集,m 是类的数量。我应该如何划分数据集,训练和测试的百分比是多少?

【问题讨论】:

    标签: algorithm machine-learning data-mining


    【解决方案1】:

    您按类别拆分数据集。之后,您独立地从每个子群体中取样。您从一个子群体中抽样的实例数量应该与其比例相关。

     data
     d(i) <- { x in data | class(x) =i }
     for each class
        for j = 0..samplesize*(size(d(i))/size(data))
           sample(i) <- draw element from d(i)
     sample <- U sample(i)
    

    如果您从具有 {'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b'} 类的数据集中抽取四个元素,则此过程可确保分层样本中至少包含一个 b 类元素。

    【讨论】:

    • samplesize 是否等于数据集中的类或数据集中的实例?
    【解决方案2】:

    只需要在 python 中实现它,我将在这里发布我当前的方法,以防其他人对此感兴趣。

    为原始数据框创建索引以创建分层引导样本的功能

    我选择遍历原始 Dataframe 中的所有相关层簇,检索每个层中相关行的索引,并随机(有替换)从该层所包含的层中抽取相同数量的样本。

    反过来,随机绘制的索引可以组合成一个列表(最终应该与原始数据帧具有相同的长度)。

    import pandas as pd
    from random import choices
    
    def provide_stratified_bootstap_sample_indices(bs_sample):
    
        strata = bs_sample.loc[:, "STRATIFICATION_VARIABLE"].value_counts()
        bs_index_list_stratified = []
    
        for idx_stratum_var, n_stratum_var in strata.iteritems():
    
            data_index_stratum = list(bs_sample[bs_sample["STRATIFICATION_VARIABLE"] == idx_stratum_var[0]].index)
            bs_index_list_stratified.extend(choices(data_index_stratum , k = len(data_index_stratum )))
    
        return bs_index_list_stratified
    

    然后是实际的引导循环

    (比如 10k 次):

    k=10000
    
    for i in range(k):
        bs_sample = DATA_original.copy()
    
        bs_index_list_stratified = provide_stratified_bootstap_sample_indices(bs_sample)
        bs_sample = bs_sample.loc[bs_index_list_stratified , :]
    
        # process data with some statistical operation as required and save results as required for each iteration
        RESULTS = FUNCTION_X(bs_sample)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多