Last active
February 11, 2020 06:12
-
-
Save Eligijus112/cf2785ff80a835dd3522976c2e0712f3 to your computer and use it in GitHub Desktop.
A function to convert a time series to X and Y matrices for deep learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| def create_X_Y(ts: list, lag: int) -> tuple: | |
| """ | |
| A method to create X and Y matrix from a time series list for the training of | |
| deep learning models | |
| """ | |
| X, Y = [], [] | |
| if len(ts) - lag <= 0: | |
| X.append(ts) | |
| else: | |
| for i in range(len(ts) - lag): | |
| Y.append(ts[i + lag]) | |
| X.append(ts[i:(i + lag)]) | |
| X, Y = np.array(X), np.array(Y) | |
| # Reshaping the X array to an LSTM input shape | |
| X = np.reshape(X, (X.shape[0], X.shape[1], 1)) | |
| return X, Y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment