[英]Train-Test split for Time Series Data to be used for LSTM
values = df.values
train, test = train_test_split(values)
#Split into train and test
X_train, y_train = train[:, :-1], train[:, -1]
X_test, y_test = test[:, :-1], test[:, -1]
執行上述代碼將時間序列數據集拆分為訓練 75% 和測試 25%。 我想將訓練測試拆分控制為 80-20 或 90-10。 有人可以幫助我了解如何將數據集拆分為我想要的任何比例嗎?
這個概念是從https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/借來的。
注意:我不能為訓練和測試隨機拆分數據集,最近的值必須用於測試。 我已經包含了我的數據集的屏幕截圖。
基本上,你會想要做類似train_test_split(values,test_size=.2,shuffle=False)
test_size=.2
告訴函數使測試大小為輸入數據的 20%(您可以類似地使用train_size=n
指定train_size=n
大小,但在沒有此規范的情況下,函數將使用1-test_size
,即測試集)。
shuffle=False
告訴函數不要隨機打亂順序。
首先,您應該使用切片或 sklearn 的train_test_split (記住對時間序列數據使用shuffle=False
)將數據分成訓練和測試。
#divide data into train and test
train_ind = int(len(df)*0.8)
train = df[:train_ind]
test = df[train_ind:]
然后,您想使用Keras 的 TimeseriesGenerator為 LSTM 生成用作輸入的序列。 這個博客很好地解釋了它的用法。
from keras.preprocessing.sequence import TimeseriesGenerator
n_input = 2 #length of output
generator = TimeseriesGenerator(train, targets=train, length=n_input)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.