Skip to content

Instantly share code, notes, and snippets.

View tft_oos5
# plot the forecast
plt.figure(100, figsize=(20, 7))
sns.set(font_scale=1.3)
p = sns.lineplot(x = "time", y = "Q50", data = dfY, palette="coolwarm")
sns.lineplot(x = "time", y = "Actual", data = dfY, palette="coolwarm")
plt.legend(labels=["forecast median price Q50", "actual price"])
p.set_ylabel("price")
p.set_xlabel("")
end = ts_tpred.end_time()
p.set_title("energy price until {} (test set + {} hours out-of-sample)".format(end, k));
View tft_oos4
# retrieve forecast series for chosen quantiles,
# inverse-transform each series,
# insert them as columns in a new dataframe dfY
q50_RMSE = np.inf
q50_MAPE = np.inf
ts_q50 = None
pd.options.display.float_format = '{:,.2f}'.format
dfY = pd.DataFrame()
#dfY["Actual"] = TimeSeries.pd_series(ts_test)
View tft_oos2
# forecast from end of training set until k hours beyond end of test set
ts_tpred = model.predict( n=n_FC,
future_covariates=cov_t_fut,
num_samples=N_SAMPLES,
verbose=True,
n_jobs=N_JOBS)
print("start:", ts_tpred.start_time(), "; end:",ts_tpred.end_time())
View tft_oos1
# choose forecast horizon: k hours beyond end of test set
k = 12
n_FC = k + len(ts_ttest) # length of test set + k hours
print("forecast beyond end of training set:", n_FC,
"hours beyond", ts_ttrain.end_time())
# last 24 hours of feature covariates available => copy them to future 24 hours:
covF_t_fut = covF_t.concatenate( other=covF_t.tail(size=24),
ignore_time_axes=True)
View tft_plot1
# plot the forecast
plt.figure(100, figsize=(20, 7))
sns.set(font_scale=1.3)
p = sns.lineplot(x = "time", y = "Q50", data = dfY, palette="coolwarm")
sns.lineplot(x = "time", y = "Actual", data = dfY, palette="coolwarm")
plt.legend(labels=["forecast median price Q50", "actual price"])
p.set_ylabel("price")
p.set_xlabel("")
p.set_title("energy price (test set)");
View tft_qfc
# retrieve forecast series for chosen quantiles,
# inverse-transform each series,
# insert them as columns in a new dataframe dfY
q50_RMSE = np.inf
q50_MAPE = np.inf
ts_q50 = None
pd.options.display.float_format = '{:,.2f}'.format
dfY = pd.DataFrame()
dfY["Actual"] = TimeSeries.pd_series(ts_test)
View tft_test1
# testing: generate predictions
ts_tpred = model.predict( n=len(ts_ttest),
num_samples=N_SAMPLES,
n_jobs=N_JOBS,
verbose=True)
View tft_train1
# training: load a saved model or (re)train
if LOAD:
print("have loaded a previously saved model from disk:" + mpath)
model = TFTModel.load_model(mpath) # load previously model from disk
else:
model.fit( series=ts_ttrain,
future_covariates=cov_t,
val_series=ts_ttest,
val_future_covariates=cov_t,
verbose=True)
View tft_train
model = TFTModel( input_chunk_length=INLEN,
output_chunk_length=N_FC,
hidden_size=HIDDEN,
lstm_layers=LSTMLAYERS,
num_attention_heads=ATTH,
dropout=DROPOUT,
batch_size=BATCH,
n_epochs=EPOCHS,
nr_epochs_val_period=VALWAIT,
likelihood=QuantileRegression(QUANTILES),
View tft_cov3
# combine feature and time covariates along component dimension: axis=1
ts_cov = ts_covF.concatenate( covT.slice_intersect(ts_covF), axis=1 ) # unscaled F+T
cov_t = covF_t.concatenate( covT_t.slice_intersect(covF_t), axis=1 ) # scaled F+T
cov_ttrain = covF_ttrain.concatenate( covT_ttrain.slice_intersect(covF_ttrain), axis=1 ) # scaled F+T training set
cov_ttest = covF_ttest.concatenate( covT_ttest.slice_intersect(covF_ttest), axis=1 ) # scaled F+T test set
pd.options.display.float_format = '{:.2f}'.format
print("first and last row of unscaled covariates:")
ts_cov.pd_dataframe().iloc[[0,-1]]