【问题标题】:Loop to perform same upsampling task over several pandas dataframes for logistic regression循环在多个 pandas 数据帧上执行相同的上采样任务以进行逻辑回归
【发布时间】:2018-11-06 17:23:17
【问题描述】:

我有一系列数据框,其中包含每日降雨总量(连续数据)以及是否发生洪水(二进制数据,即 1 或 0)。每个数据框代表一年(例如 df01、df02、df03 等),如下所示:

date        ppt    fld
01/02/2011  1.5    0
02/02/2011  0.0    0
03/02/2011  2.7    0
04/02/2011  4.6    0
05/02/2011  15.5   1
06/02/2011  1.5    0
...

我希望对每年的数据进行逻辑回归,但由于洪水事件的数量相对于降雨事件的数量非常少,因此数据严重不平衡。因此,我希望只对少数类进行上采样(“fld”中的值为 1)。到目前为止,我知道根据“fld”值将每个数据帧一分为二,对生成的“1”数据帧进行上采样,然后重新合并为一个数据帧。

# So if I apply to one dataframe it looks like this:

# Separate majority and minority classes
mask = df01.fld == 0
fld_0 = df01[mask]
fld_1 = df01[~mask]

# Upsample minority class
fld_1_upsampled = resample(fld_1, 
                                 replace=True,     # sample with replacement
                                 n_samples=247,    # to match majority class
                                 random_state=123) # reproducible results

# Combine majority class with upsampled minority class
df01_upsampled = pd.concat([fld_0, fld_1_upsampled])

由于我有 17 个数据帧,因此逐个数据帧进行处理是低效的。关于如何提高效率有什么想法吗?到目前为止,我已经尝试过这个(很明显我不知道我在用这种循环做什么,我对 python 很陌生):

df_all = [df01, df02, df03, df04,
           df05, df06, df07, df08, 
           df09, df10, df11, df12, 
           df13, df14, df15, df16, df17]
# This is my list of annual data

for i in df_all:
fld_0 = i[mask]
fld_1 = i[~mask]

    fld_1_upsampled = resample(fld_1, 
                               replace=True,     # sample with replacement
                               n_samples=len(fld_0),    # to match majority class
                               random_state=123) # reproducible results
    i_upsampled = pd.concat([fld_0, fld_1_upsampled])
return i_upsampled

返回以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-6fd782d4c469> in <module>()
     11                                replace=True,     # sample with replacement
     12                                n_samples=247,    # to match majority class
---> 13                                random_state=123) # reproducible results
     14     i_upsampled = pd.concat([fld_0, fld_1_upsampled])
     15 return i_upsampled

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/__init__.py in resample(*arrays, **options)
    259 
    260     if replace:
--> 261         indices = random_state.randint(0, n_samples, size=(max_n_samples,))
    262     else:
    263         indices = np.arange(n_samples)

mtrand.pyx in mtrand.RandomState.randint()

ValueError: low >= high

非常感谢任何建议或cmets :)

更新:一个回复表明我的一些数据框可能不包含来自少数类的任何样本。这是正确的,所以我删除了它们,但出现了同样的错误。

【问题讨论】:

    标签: python pandas dataframe scikit-learn


    【解决方案1】:

    让您怀疑您在第二个代码块中使用与第一个代码块中相同的 mask 语法,看起来您可能没有任何示例可以传递给您的 resample或更多您的 DF:

    df=pd.DataFrame({'date':[1,2,3,4,5,6],'ppt':[1.5,0,2.7,4.6,15.5,1.5],'fld':[0,1,0,0,1,1]})
    
    date    ppt     fld
    1       1.5     0
    2       0.0     1
    3       2.7     0
    4       4.6     0
    5       15.5    1
    6       1.5     1
    
    resample(df[df.fld==1], replace=True, n_samples=3, random_state=123)
    
    date    ppt     fld
    6       1.5     1
    5       15.5    1
    6       1.5     1
    
    resample(df[df.fld==2], replace=True, n_samples=3, random_state=123)
    
    "...ValueError: low >= high"
    

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 2021-08-03
      • 2022-10-23
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多