Python中将数据帧随机分层拆分为多个样本

2024-04-19 15:19:45 发布

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

有没有办法将熊猫数据帧分割成多个相互排斥的样本(长度不同),并按变量分层

我目前的方法是对每个样本使用sci kit learn中的train_test_split多次,但感觉效率非常低

cell_to_split, cell_1 = train_test_split(data, test_size=50, stratify=strat_variable)
cell_to_split, cell_2 = train_test_split(cell_to_split, test_size=60, stratify=strat_variable)
cell_to_split, cell_3 = train_test_split(cell_to_split, test_size=40, stratify=strat_variable)

# strat_variable here is a string variable in data or cell_to_split i'm using for random stratified sampling

这使我可以从数据集中获得3个样本,每个样本都具有指定的大小(行数),平衡了strat_变量的代表性,但效率不太高,理想情况下,我希望样本数(此处设置为3)是动态的


Tags: to数据testdatasize分层celltrain