簡體   English   中英

有條件地將數據拆分為訓練和測試(Pandas)

[英]Conditional splitting the data into training and testing (Pandas)

我有一個使用 Python 執行預測任務的代碼。 任務是預測一家公司從 2015 年到 2019 年不同年份的銷售額。

我想將數據拆分為訓練集和測試集。

但問題是,我想用2015年到2018年的數據訓練模型,在2019年的數據上測試模型。

我怎樣才能使用 train_test_split、ShuffleSplit、有條件地拆分數據?

X_train = df.iloc[train_index]
X_test = df.iloc[test_index]
y_train = X_train.Sales
y_test = X_test.Sales

由於您在一開始就遇到了條件,因此您將失去使用機器學習預處理中使用的改組方法的好處。 因此,我建議不要在這種情況下執行訓練測試拆分(我假設結果有偏差)。 不過,如果您需要這樣做,請嘗試:

train = your_data[your_data['year_column'] < 2019]
test = your_data[your_data['year_column'] == 2019]

X_train = train.loc[:, train.columns != 'column_of_interest']
y_train = train['column_of_interest']
X_test = test.loc[:, test.columns != 'column_of_interest']
y_train = test['column_of_interest']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM