如何将数据拆分为训练和测试,牢记 pandas 中的 groupby 列?
How to split data into train and test keeping in mind the groupby column in pandas?
我想按照 20:80 的比例将数据集拆分为测试和训练数据集。但是,在拆分时,我不想以 1 S_Id 值在训练中的数据点和测试中的其他数据点很少的方式进行拆分。
我有一个数据集:
S_Id Datetime Item
1 29-06-2018 03:23:00 654
1 29-06-2018 04:01:00 452
1 29-06-2018 04:25:00 101
2 30-06-2018 05:17:00 088
2 30-06-2018 05:43:00 131
3 30-06-2018 10:36:00 013
3 30-06-2018 11:19:00 092
我想像这样整齐地拆分:
火车:
S_Id Datetime Item
1 29-06-2018 03:23:00 654
1 29-06-2018 04:01:00 452
1 29-06-2018 04:25:00 101
2 30-06-2018 05:17:00 088
2 30-06-2018 05:43:00 131
测试:
S_Id Datetime Item
3 30-06-2018 10:36:00 013
3 30-06-2018 11:19:00 092
所有相同的 S_Id 必须在一组中。可以通过简单的'groupby'吗?
感谢您的帮助!
如果 S_Id 是 dataframe 的索引,你可以简单地使用:
df.loc[3]
如果不是这样,可以设置为index,然后设置为loc,如:
df.set_index(S_Id).loc[3]
这应该 return 数据框包含 S_Id 等于 3 的所有行。
我不相信有一个直接的函数可以这样做,所以你可以写一个自定义的函数:
def sample_(we_array, train_size):
"""
we_array : used as the weight of each unique element on your S_id column,
it's normalized to represent a probability
"""
idx = np.arange(we_array.size) #get the index for each element on the array
np.random.shuffle(idx) #shuffle it
cum = we_array[idx].cumsum()
train_idx = idx[cum<train_size]# we take the first elements until we have
# our desired size
test_idx = idx[cum>=train_size]
return train_idx, test_idx
idx = df.S_Id.values
unique, counts = np.unique(idx, return_counts = True) # we access the unique
# elements and their cout
probability = counts/counts.sum()
train_idx, test_idx = sample_(probability, 0.8)
train = df[df.S_Id.isin(unique[train_idx])]
test = df[df.S_Id.isin(unique[test_idx])]
我想按照 20:80 的比例将数据集拆分为测试和训练数据集。但是,在拆分时,我不想以 1 S_Id 值在训练中的数据点和测试中的其他数据点很少的方式进行拆分。
我有一个数据集:
S_Id Datetime Item
1 29-06-2018 03:23:00 654
1 29-06-2018 04:01:00 452
1 29-06-2018 04:25:00 101
2 30-06-2018 05:17:00 088
2 30-06-2018 05:43:00 131
3 30-06-2018 10:36:00 013
3 30-06-2018 11:19:00 092
我想像这样整齐地拆分: 火车:
S_Id Datetime Item
1 29-06-2018 03:23:00 654
1 29-06-2018 04:01:00 452
1 29-06-2018 04:25:00 101
2 30-06-2018 05:17:00 088
2 30-06-2018 05:43:00 131
测试:
S_Id Datetime Item
3 30-06-2018 10:36:00 013
3 30-06-2018 11:19:00 092
所有相同的 S_Id 必须在一组中。可以通过简单的'groupby'吗?
感谢您的帮助!
如果 S_Id 是 dataframe 的索引,你可以简单地使用:
df.loc[3]
如果不是这样,可以设置为index,然后设置为loc,如:
df.set_index(S_Id).loc[3]
这应该 return 数据框包含 S_Id 等于 3 的所有行。
我不相信有一个直接的函数可以这样做,所以你可以写一个自定义的函数:
def sample_(we_array, train_size):
"""
we_array : used as the weight of each unique element on your S_id column,
it's normalized to represent a probability
"""
idx = np.arange(we_array.size) #get the index for each element on the array
np.random.shuffle(idx) #shuffle it
cum = we_array[idx].cumsum()
train_idx = idx[cum<train_size]# we take the first elements until we have
# our desired size
test_idx = idx[cum>=train_size]
return train_idx, test_idx
idx = df.S_Id.values
unique, counts = np.unique(idx, return_counts = True) # we access the unique
# elements and their cout
probability = counts/counts.sum()
train_idx, test_idx = sample_(probability, 0.8)
train = df[df.S_Id.isin(unique[train_idx])]
test = df[df.S_Id.isin(unique[test_idx])]