Data preparation glue

We've done a lot of transformation in this example. Before moving on to training, I think it might be a good idea to see how this all fits together. We will use one more function, as shown here, to tie all these steps together:

def prep_data(df_train, df_test, lags):
df_train = diff_data(df_train)
scaler, df_train = scale_data(df_train)
df_test = diff_data(df_test)
scaler, df_test = scale_data(df_test, scaler)
df_train = lag_dataframe(df_train, lags=lags)
df_test = lag_dataframe(df_test, lags=lags)

X_train = df_train.drop("y", axis=1)
y_train = df_train.y
X_test = df_test.drop("y", axis=1)
y_test = df_test.y

X_train = np.reshape(X_train.values, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test.values, (X_test.shape[0], X_test.shape[1], 1))

return X_train, X_test, y_train, y_test

This function takes our train and test dataframes and applies differencing, scaling, and lagging code. It then realigns those dataframes into our familiar X and y tensors for both train and test.

We can now get from loading the data to being ready to train and test with just a few lines of code that glue these transformations together:

LAGS=10
df = read_data()
df_train = select_dates(df, start="2017-01-01", end="2017-05-31")
df_test = select_dates(df, start="2017-06-01", end="2017-06-30")
X_train, X_test, y_train, y_test = prep_data(df_train, df_test, lags=LAGS)

And with that we're ready to train.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.7