Auto Time Series ~ A Powerful Genetic Algorithm Approach for Time Series

 

Hi everyone, welcome back to another interesting topic of the Genetic Based Approach for time series / sequential data.

We have already seen in our previous discussion how the Genetic Based Approach works for the machine learning algorithms, designed for higher accuracy throughput,

Here’s the link in case of couldn't able to catch on what I m saying.

Today we will look into the same approach for Time Series data using a package called AutoTS

Let’s get started

#AutoTS
from autots import AutoTS
from autots.models.model_list import model_lists
print(model_lists)
autoTS model list
model list

Cool? we have tons of and types of Time Series models including ensemble which I will show you later.

Now let's load the data.

#default data
from autots.datasets import load_monthly
#data was in long format
df_long = load_monthly(long=True)
data frame (long format)

#Initialize the AutoTS with different paramters

model = AutoTS(
forecast_length=3,
frequency='infer',
ensemble='simple',
max_generations=5,
num_validations=2,
)
model = model.fit(df_long, date_col='datetime', value_col='value', id_col='series_id')
#help(AutoTS) 

Be patient it takes time as it has to go through many iterations of algorithms.

# Print the name of the best model
print(model)
Best Model
Best Model

We can also view the model accuracy scores

#accuracy score
model.results()
#aggregated from cross validation
validation_results = model.results("validation")
validation results
validation results

From the list of model accuracy scores, you can also see the highlighted above from column Model ‘Ensemble’ and its low accuracy validates one of the theories that it's not true that ensembling always performs better.

Since we got the best model as well the other model accuracy scores. now its time to get the forecast values.

prediction = model.predict()
forecasts_df = prediction.forecast
upper_forecasts_df = prediction.upper_forecast
lower_forecasts_df = prediction.lower_forecast
#or
forecasts_df1 = prediction.long_form_results()
upper_forecasts_df1 = prediction.long_form_results()
lower_forecasts_df1 = prediction.long_form_results()
orecast results in wide and long format respectively
forecast results in wide and long format respectively

Let's take another use-case to meet our customization

What if we want to perform on a few model lists and having more or less weight for a certain feature.

from autots import AutoTS
from autots.datasets import load_hourly
df_wide = load_hourly(long=False)
# here we care most about traffic volume, all other series assumed to be weight of 1
weights_hourly = {'traffic_volume': 20}
Data Frame in Wide Format

Now we will define the model list

model_list = [
'LastValueNaive',
'GLS',
'ETS',
'AverageValueNaive',
]
model = AutoTS(
forecast_length=49,
frequency='infer',
prediction_interval=0.95,
ensemble=['simple', 'horizontal-min'],
max_generations=5,
num_validations=2,
validation_method='seasonal 168',
model_list=model_list,
transformer_list='all',
models_to_validate=0.2,
drop_most_recent=1,
n_jobs='auto',
)
model list
model list

After this, we now have to define the weight while fitting the model with our data.

model = model.fit(
df_wide,
weights=weights_hourly,
)
prediction = model.predict()
forecasts_df = prediction.forecast
autoTS forecasted Results
Forecasted Results

Exporting the model is quite easy too.

#EXPORTING############################################
# after fitting an AutoTS model
example_filename = "example_export.csv" # .csv/.json
model.export_template(example_filename, models='best',
n=15, max_per_model_class=3)
forecast_length = 3
# on new training
model = AutoTS(forecast_length=forecast_length,
frequency='infer', max_generations=0,
num_validations=0, verbose=0)
model = model.import_template(example_filename, method='only') # method='add on'
print("Overwrite template is: {}".format(str(model.initial_template)))

Again what if we want to run one model, here is the quick catch

#Running just one model ################################################################
from autots import load_daily, model_forecast
df = load_daily(long=False)  # long or non-numeric data won't work with this function
df_forecast = model_forecast(
model_name="AverageValueNaive",
model_param_dict={'method': 'Mean'},
model_transform_dict={
'fillna': 'mean',
'transformations': {'0': 'DifferencedTransformer'},
'transformation_params': {'0': {}}
},
df_train=df,
forecast_length=12,
frequency='infer',
prediction_interval=0.9,
no_negatives=False,
# future_regressor_train=future_regressor_train2d,
# future_regressor_forecast=future_regressor_forecast2d,
random_seed=321,
verbose=0,
n_jobs="auto",
)
df_forecast.forecast.head(5)

That's it! Here is the repo of the package for detail exploration

https://winedarksea.github.io/AutoTS/build/html/source/intro.html#autots

Did you enjoyed it? if so let me know…….do browse my other articles including the Genetic Based Algorithm with Tpot

I guarantee you will like them too. See you soon with another interesting topic.

Some of my alternative internet presences are Facebook, Instagram, Udemy, Blogger, Issuu, and more.

Also available on Quora @ https://www.quora.com/profile/Rupak-Bob-Roy

Have a good day.


Comments

Popular Posts