循环在逻辑回归的多个数据帧上执行相同的上采样任务

2024-05-16 16:06:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列的数据帧,包含每天的降雨总量(连续数据)和是否发生洪水(二进制数据,即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

非常感谢您的任何建议或意见:)

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


Tags: to数据inwithmaskrandomreplaceclass
1条回答
网友
1楼 · 发布于 2024-05-16 16:06:08

如果您在第二个代码块中使用与第一个代码块相同的mask语法,那么您可能没有任何示例可以传递到一个或多个DFs中的resample

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"

相关问题 更多 >