Last active
February 3, 2022 12:37
-
-
Save Eligijus112/b28fb1dadf422139035c481017e7a71a to your computer and use it in GitHub Desktop.
A function to create X and Y training matrices for sequence modeling
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?