Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Last active February 3, 2022 12:37
Show Gist options
  • Save Eligijus112/b28fb1dadf422139035c481017e7a71a to your computer and use it in GitHub Desktop.
Save Eligijus112/b28fb1dadf422139035c481017e7a71a to your computer and use it in GitHub Desktop.
A function to create X and Y training matrices for sequence modeling
def create_X_Y(ts: np.array, lag=1, n_ahead=1, target_index=0) -> tuple:
"""
A method to create X and Y matrix from a time series array for the training of
deep learning models
"""
# Extracting the number of features that are passed from the array
n_features = ts.shape[1]
# Creating placeholder lists
X, Y = [], []
if len(ts) - lag <= 0:
X.append(ts)
else:
for i in range(len(ts) - lag - n_ahead):
Y.append(ts[(i + lag):(i + lag + n_ahead), target_index])
X.append(ts[i:(i + lag)])
X, Y = np.array(X), np.array(Y)
# Reshaping the X array to an RNN input shape
X = np.reshape(X, (X.shape[0], lag, n_features))
return X, Y
@oysteinloken
Copy link

oysteinloken commented Feb 3, 2022

if len(ts) - lag <= 0: X.append(ts)
Could you explain what you do here? This will create a feature frame but no target variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment