--- title: Experiment Utils keywords: fastai sidebar: home_sidebar summary: "This notebook contains a set of functions to easily perform experiments on time series datasets. In this notebook you can see functions for:" description: "This notebook contains a set of functions to easily perform experiments on time series datasets. In this notebook you can see functions for:" nb_path: "nbs/experiments__utils.ipynb" ---
In the next two scetion you can see enviroment variables and imports that are used for set of functions shown in this notebook.
import torch as t
from neuralforecast.losses.numpy import mae, rmse
from neuralforecast.auto import nhits_space, nbeats_space
from neuralforecast.data.datasets.epf import EPF
dataset = ['NP']
Y_df, X_df, S_df = EPF.load_groups(directory='data', groups=dataset)
X_df = X_df[['unique_id', 'ds', 'week_day']]
space = nhits_space(n_time_out=24) #, n_series=1, n_x=1, n_s=0, frequency='H')
space['max_steps'] = hp.choice('max_steps', [1]) # Override max_steps for faster example
# The suggested spaces are partial, here we complete them with data specific information
space['n_series'] = hp.choice('n_series', [ Y_df['unique_id'].nunique() ])
space['n_x'] = hp.choice('n_x', [ 0 if X_df is None else (X_df.shape[1]-2) ])
space['n_s'] = hp.choice('n_s', [ 0 if S_df is None else (S_df.shape[1]-1) ])
space['n_x_hidden'] = hp.choice('n_x_hidden', [ 0 if X_df is None else (X_df.shape[1]-2) ])
space['n_s_hidden'] = hp.choice('n_s_hidden', [ 0 if S_df is None else (S_df.shape[1]-1) ])
# Infers freq with first time series
freq = pd.infer_freq(Y_df[Y_df['unique_id']==Y_df.unique_id.unique()[0]]['ds'])
space['frequency'] = hp.choice('frequency', [ freq ])
model, trials = hyperopt_tunning(space=space, hyperopt_max_evals=2, loss_function_val=mae,
loss_functions_test={'mae': mae, 'rmse': rmse},
S_df=S_df, Y_df=Y_df, X_df=X_df, f_cols=[],
ds_in_val=7*24, ds_in_test=7*24,
return_forecasts=True, return_model=True, save_trials=True,
results_dir='./results/example', loss_kwargs={}, verbose=False)
model
space = nbeats_space(n_time_out=24)
space['max_steps'] = hp.choice('max_steps', [1]) # Override max_steps for faster example
# The suggested spaces are partial, here we complete them with data specific information
space['n_series'] = hp.choice('n_series', [ Y_df['unique_id'].nunique() ])
space['n_x'] = hp.choice('n_x', [ 0 if X_df is None else (X_df.shape[1]-2) ])
space['n_s'] = hp.choice('n_s', [ 0 if S_df is None else (S_df.shape[1]-1) ])
space['n_x_hidden'] = hp.choice('n_x_hidden', [ 0 if X_df is None else (X_df.shape[1]-2) ])
space['n_s_hidden'] = hp.choice('n_s_hidden', [ 0 if S_df is None else (S_df.shape[1]-1) ])
# Infers freq with first time series
freq = pd.infer_freq(Y_df[Y_df['unique_id']==Y_df.unique_id.unique()[0]]['ds'])
space['frequency'] = hp.choice('frequency', [ freq ])
trials = hyperopt_tunning(space=space, hyperopt_max_evals=2, loss_function_val=mae,
loss_functions_test={'mae': mae, 'rmse': rmse},
S_df=S_df, Y_df=Y_df, X_df=X_df, f_cols=[],
ds_in_val=7*24, ds_in_test=7*24,
return_forecasts=True, return_model=False, save_trials=False,
results_dir=None, loss_kwargs={}, verbose=False)