This file contains 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
import pandas as pd | |
import numpy as np | |
from timeit import Timer | |
# create a dataframe with N rows | |
def create_df(N): | |
df = pd.DataFrame({ | |
'A' : ['red', 'blue', 'green', 'white'] * int(N/4), | |
'B' : np.random.randint(0, 100, N), |
This file contains 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
# 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)); |
This file contains 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
# 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) | |
This file contains 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
# 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()) |
This file contains 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
# 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) |
This file contains 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
# 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)"); |
This file contains 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
# 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) |
This file contains 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
# testing: generate predictions | |
ts_tpred = model.predict( n=len(ts_ttest), | |
num_samples=N_SAMPLES, | |
n_jobs=N_JOBS, | |
verbose=True) |
This file contains 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
# 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) |
This file contains 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
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), |