Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Last active February 11, 2020 06:12
Show Gist options
  • Save Eligijus112/cf2785ff80a835dd3522976c2e0712f3 to your computer and use it in GitHub Desktop.
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
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