目前我正在查看这样的数据集:
date income account flag day month year
0 2018-04-13 470.57 1000 0002 8 13 4 2018
1 2018-04-14 375.54 1000 0002 8 14 4 2018
2 2018-05-15 375.54 1000 0002 8 15 5 2018
3 2018-05-16 229.04 1000 0002 7 16 5 2018
4 2018-06-17 216.62 1000 0002 7 17 6 2018
5 2018-06-18 161.61 1000 0002 6 18 6 2018
6 2018-04-19 131.87 0000 0001 6 19 4 2018
7 2018-04-20 100.57 0000 0001 6 20 4 2018
8 2018-08-21 100.57 0000 0001 6 21 8 2018
9 2018-08-22 50.57 0000 0001 5 22 8 2018 我正在研究一个DecisionTree回归模型,将RandomForest与ExtraTrees进行比较,并调整它的一些超参数。我当前尝试做的是拆分dataset,以便将每个唯一值的列month的最大值的行保留为account (如果这样做更容易,也可以设置为索引),而将其他行设置为train_set。基本上,这意味着将使用所有可用的历史数据进行回归,但属于最后一个可用月份的数据将用于验证mse。
我知道如何根据静态条件(如df[df['month'] < 12] )过滤数据帧,但在这种情况下,我需要为每个不同的account值保留属于最大月份的所有行。
从前面的数据集中,我应该能够获得如下内容:df_test =
date income account flag day month year
4 2018-06-17 216.62 1000 0002 7 17 6 2018
5 2018-06-18 161.61 1000 0002 6 18 6 2018
8 2018-08-21 100.57 0000 0001 6 21 8 2018
9 2018-08-22 50.57 0000 0001 5 22 8 2018 和df_train =
date income account flag day month year
0 2018-04-13 470.57 1000 0002 8 13 4 2018
1 2018-04-14 375.54 1000 0002 8 14 4 2018
2 2018-05-15 375.54 1000 0002 8 15 5 2018
3 2018-05-16 229.04 1000 0002 7 16 5 2018
6 2018-04-19 131.87 0000 0001 6 19 4 2018
7 2018-04-20 100.57 0000 0001 6 20 4 2018 例如,对于df['account'] = 1000 0002,我可以使用4个月和5个月进行预测,使用6个月进行验证。谢谢!
发布于 2019-08-26 22:39:55
您可以使用transform
test=df[df.month==df.groupby('account').month.transform('max')].copy()
train=df.drop(test.index)
test
Out[643]:
date income account flag day month year
4 2018-06-17 216.62 10000002 7 17 6 2018
5 2018-06-18 161.61 10000002 6 18 6 2018
8 2018-08-21 100.57 1 6 21 8 2018
9 2018-08-22 50.57 1 5 22 8 2018https://stackoverflow.com/questions/57659966
复制相似问题