[英]how to split data into train and test based on a column values and shuffle the combinations?
我有一個數據集,我想根據列值拆分數據集。 在每次迭代中,訓練集將包括除屬於2個值的所有數據以外的所有數據,這些數據將保留給測試集。
例如,我們有x
列,其值為a
, b
, c
, d
, e
和f
。
目前,我正在進行手動選擇,但由於我想嘗試每種可能的組合,因此我不確定如何做到最好。
train = df.loc[~df['x'].isin(['a','b'])]
test = df.loc[df['x'].isin(['a','b'])]
如何更改此代碼以考慮所有可能的組合?
我還希望能夠打印這些組合,以查看用於訓練和測試集的組合。
未經測試,但是如何使用itertools.combinations
像:
for holdouts in itertools.combinations(df['x'].unique(), 2):
print(holdouts)
train = df[~df['x'].isin(holdouts)]
test = df[df['x'].isin(holdouts)]
您可以通過mask = df['x'].isin(holdouts)
保存評估
請注意,對於布爾值進行索引時,不需要.loc
iteratetools.combinations應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.