python-3.x 基于列值的训练测试拆分-按顺序

xsuvu9jc  于 2023-03-04  发布在  Python
关注(0)|答案(3)|浏览(114)

我有一个 Dataframe 如下

df = pd.DataFrame({"Col1": ['A','B','B','A','B','B','A','B','A', 'A'],
                   "Col2" : [-2.21,-9.59,0.16,1.29,-31.92,-24.48,15.23,34.58,24.33,-3.32],
                   "Col3" : [-0.27,-0.57,0.072,-0.15,-0.21,-2.54,-1.06,1.94,1.83,0.72],
                   "y" : [-1,1,-1,-1,-1,1,1,1,1,-1]})
    
       Col1 Col2    Col3    y
    0   A   -2.21   -0.270  -1
    1   B   -9.59   -0.570   1
    2   B    0.16    0.072  -1
    3   A    1.29   -0.150  -1
    4   B   -31.92  -0.210  -1
    5   B   -24.48  -2.540   1
    6   A    15.23  -1.060   1
    7   B    34.58   1.940   1
    8   A    24.33   1.830   1
    9   A   -3.32    0.720  -1

是否有办法分割 Dataframe (60:40分割),以便Col1的前60%的值每组将被训练,最后40%将被测试。
列车:

Col1 Col2    Col3    y
0   A   -2.21   -0.270  -1
1   B   -9.59   -0.570   1
2   B    0.16    0.072  -1
3   A    1.29   -0.150  -1
4   B   -31.92  -0.210  -1
6   A    15.23  -1.060   1

试验:

Col1 Col2    Col3    y
5   B   -24.48  -2.540   1
7   B    34.58   1.940   1
8   A    24.33   1.830   1
9   A   -3.32    0.720  -1
i7uaboj4

i7uaboj41#

我觉得你需要一个

s=df.groupby('Col1').Col1.cumcount()#get the count for each group
s=s//(df.groupby('Col1').Col1.transform('count')*0.6).astype(int)# get the top 60% of each group 
Train=df.loc[s==0].copy()
Test=df.drop(Train.index)
Train
Out[118]: 
  Col1   Col2   Col3  y
0    A  -2.21 -0.270 -1
1    B  -9.59 -0.570  1
2    B   0.16  0.072 -1
3    A   1.29 -0.150 -1
4    B -31.92 -0.210 -1
6    A  15.23 -1.060  1
Test
Out[119]: 
  Col1   Col2  Col3  y
5    B -24.48 -2.54  1
7    B  34.58  1.94  1
8    A  24.33  1.83  1
9    A  -3.32  0.72 -1
mrfwxfqh

mrfwxfqh2#

如果需要拆分而不分组:

thresh = int(len(df) * 0.6)
train = df.iloc[:thresh]
test = df.iloc[thresh:]
print(train)
  Col1   Col2   Col3  y
0    A  -2.21 -0.270 -1
1    B  -9.59 -0.570  1
2    B   0.16  0.072 -1
3    A   1.29 -0.150 -1
4    B -31.92 -0.210 -1
5    B -24.48 -2.540  1

print(test)
  Col1   Col2  Col3  y
6    A  15.23 -1.06  1
7    B  34.58  1.94  1
8    A  24.33  1.83  1
9    A  -3.32  0.72 -1

编辑:如果需要按组拆分,则使用GroupBy.cumcount和过滤创建阈值:

thresh = int(len(df) * 0.6 / df['Col1'].nunique())
print (thresh)
3

mask  = df.groupby('Col1')['Col1'].cumcount() < thresh 

train = df[mask]
test = df[~mask]
print(train)
  Col1   Col2   Col3  y
0    A  -2.21 -0.270 -1
1    B  -9.59 -0.570  1
2    B   0.16  0.072 -1
3    A   1.29 -0.150 -1
4    B -31.92 -0.210 -1
6    A  15.23 -1.060  1
print(test)
  Col1   Col2  Col3  y
5    B -24.48 -2.54  1
7    B  34.58  1.94  1
8    A  24.33  1.83  1
9    A  -3.32  0.72 -1
8wtpewkr

8wtpewkr3#

IIUC,您可以使用numpy.split

import numpy as np

train, test = np.split(df, [int(len(df) * 0.6)])

print(train)

  Col1   Col2   Col3  y
0    A  -2.21 -0.270 -1
1    B  -9.59 -0.570  1
2    B   0.16  0.072 -1
3    A   1.29 -0.150 -1
4    B -31.92 -0.210 -1
5    B -24.48 -2.540  1

print(test)

  Col1   Col2  Col3  y
6    A  15.23 -1.06  1
7    B  34.58  1.94  1
8    A  24.33  1.83  1
9    A  -3.32  0.72 -1

相关问题