{"id":1198,"date":"2018-10-21T18:00:55","date_gmt":"2018-10-21T18:00:55","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2018\/10\/21\/how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python\/"},"modified":"2018-10-21T18:00:55","modified_gmt":"2018-10-21T18:00:55","slug":"how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2018\/10\/21\/how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python\/","title":{"rendered":"How to Grid Search Triple Exponential Smoothing for Time Series Forecasting in Python"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>Exponential smoothing is a time series forecasting method for univariate data that can be extended to support data with a systematic trend or seasonal component.<\/p>\n<p>It is common practice to use an optimization process to find the model hyperparameters that result in the exponential smoothing model with the best performance for a given time series dataset. This practice applies only to the coefficients used by the model to describe the exponential structure of the level, trend, and seasonality.<\/p>\n<p>It is also possible to automatically optimize other hyperparameters of an exponential smoothing model, such as whether or not to model the trend and seasonal component and if so, whether to model them using an additive or multiplicative method.<\/p>\n<p>In this tutorial, you will discover how to develop a framework for grid searching all of the exponential smoothing model hyperparameters for univariate time series forecasting.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>How to develop a framework for grid searching ETS models from scratch using walk-forward validation.<\/li>\n<li>How to grid search ETS model hyperparameters for daily time series data for female births.<\/li>\n<li>How to grid search ETS model hyperparameters for monthly time series data for shampoo sales, car sales, and temperature.<\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<div id=\"attachment_6354\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6354\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2018\/10\/How-to-Grid-Search-Triple-Exponential-Smoothing-for-Time-Series-Forecasting-in-Python.jpg\" alt=\"How to Grid Search Triple Exponential Smoothing for Time Series Forecasting in Python\" width=\"640\" height=\"429\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/10\/How-to-Grid-Search-Triple-Exponential-Smoothing-for-Time-Series-Forecasting-in-Python.jpg 640w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/10\/How-to-Grid-Search-Triple-Exponential-Smoothing-for-Time-Series-Forecasting-in-Python-300x201.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\"><\/p>\n<p class=\"wp-caption-text\">How to Grid Search Triple Exponential Smoothing for Time Series Forecasting in Python<br \/>Photo by <a href=\"https:\/\/www.flickr.com\/photos\/127130111@N06\/16375806988\/\">john mcsporran<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into six parts; they are:<\/p>\n<ol>\n<li>Exponential Smoothing for Time Series Forecasting<\/li>\n<li>Develop a Grid Search Framework<\/li>\n<li>Case Study 1: No Trend or Seasonality<\/li>\n<li>Case Study 2: Trend<\/li>\n<li>Case Study 3: Seasonality<\/li>\n<li>Case Study 4: Trend and Seasonality<\/li>\n<\/ol>\n<h2>Exponential Smoothing for Time Series Forecasting<\/h2>\n<p>Exponential smoothing is a time series forecasting method for univariate data.<\/p>\n<p>Time series methods like the Box-Jenkins ARIMA family of methods develop a model where the prediction is a weighted linear sum of recent past observations or lags.<\/p>\n<p>Exponential smoothing forecasting methods are similar in that a prediction is a weighted sum of past observations, but the model explicitly uses an exponentially decreasing weight for past observations.<\/p>\n<p>Specifically, past observations are weighted with a geometrically decreasing ratio.<\/p>\n<blockquote>\n<p>Forecasts produced using exponential smoothing methods are weighted averages of past observations, with the weights decaying exponentially as the observations get older. In other words, the more recent the observation, the higher the associated weight.<\/p>\n<\/blockquote>\n<p>\u2014 Page 171, <a href=\"https:\/\/amzn.to\/2xlJsfV\">Forecasting: principles and practice<\/a>, 2013.<\/p>\n<p>Exponential smoothing methods may be considered as peers and an alternative to the popular Box-Jenkins ARIMA class of methods for time series forecasting.<\/p>\n<p>Collectively, the methods are sometimes referred to as ETS models, referring to the explicit modeling of <em>Error<\/em>, <em>Trend<\/em>, and <em>Seasonality<\/em>.<\/p>\n<p>There are three types of exponential smoothing; they are:<\/p>\n<ul>\n<li><strong>Single Exponential Smoothing<\/strong>, or SES, for univariate data without trend or seasonality.<\/li>\n<li><strong>Double Exponential Smoothing<\/strong> for univariate data with support for trends.<\/li>\n<li><strong>Triple Exponential Smoothing<\/strong>, or Holt-Winters Exponential Smoothing, with support for both trends and seasonality.<\/li>\n<\/ul>\n<p>A triple exponential smoothing model subsumes single and double exponential smoothing by the configuration of the nature of the trend (additive, multiplicative, or none) and the nature of the seasonality (additive, multiplicative, or none), as well as any dampening of the trend.<\/p>\n<\/p>\n<div class=\"woo-sc-hr\"><\/div>\n<p><center><\/p>\n<h3>Need help with Deep Learning for Time Series?<\/h3>\n<p>Take my free 7-day email crash course now (with sample code).<\/p>\n<p>Click to sign-up and also get a free PDF Ebook version of the course.<\/p>\n<p><a href=\"https:\/\/machinelearningmastery.lpages.co\/leadbox\/14531ee73f72a2%3A164f8be4f346dc\/5630742793027584\/\" target=\"_blank\" style=\"background: rgb(255, 206, 10); color: rgb(255, 255, 255); text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-weight: bold; font-size: 16px; line-height: 20px; padding: 10px; display: inline-block; max-width: 300px; border-radius: 5px; text-shadow: rgba(0, 0, 0, 0.25) 0px -1px 1px; box-shadow: rgba(255, 255, 255, 0.5) 0px 1px 3px inset, rgba(0, 0, 0, 0.5) 0px 1px 3px;\">Download Your FREE Mini-Course<\/a><script data-leadbox=\"14531ee73f72a2:164f8be4f346dc\" data-url=\"https:\/\/machinelearningmastery.lpages.co\/leadbox\/14531ee73f72a2%3A164f8be4f346dc\/5630742793027584\/\" data-config=\"%7B%7D\" type=\"text\/javascript\" src=\"https:\/\/machinelearningmastery.lpages.co\/leadbox-1534880695.js\"><\/script><\/p>\n<p><\/center><\/p>\n<div class=\"woo-sc-hr\"><\/div>\n<h2>Develop a Grid Search Framework<\/h2>\n<p>In this section, we will develop a framework for grid searching exponential smoothing model hyperparameters for a given univariate time series forecasting problem.<\/p>\n<p>We will use the implementation of <a href=\"http:\/\/www.statsmodels.org\/dev\/generated\/statsmodels.tsa.holtwinters.ExponentialSmoothing.html\">Holt-Winters Exponential Smoothing<\/a> provided by the statsmodels library.<\/p>\n<p>This model has hyperparameters that control the nature of the exponential performed for the series, trend, and seasonality, specifically:<\/p>\n<ul>\n<li><strong>smoothing_level<\/strong> (<em>alpha<\/em>): the smoothing coefficient for the level.<\/li>\n<li><strong>smoothing_slope<\/strong> (<em>beta<\/em>): the smoothing coefficient for the trend.<\/li>\n<li><strong>smoothing_seasonal<\/strong> (<em>gamma<\/em>): the smoothing coefficient for the seasonal component.<\/li>\n<li><strong>damping_slope<\/strong> (<em>phi<\/em>): the coefficient for the damped trend.<\/li>\n<\/ul>\n<p>All four of these hyperparameters can be specified when defining the model. If they are not specified, the library will automatically tune the model and find the optimal values for these hyperparameters (e.g. <em>optimized=True<\/em>).<\/p>\n<p>There are other hyperparameters that the model will not automatically tune that you may want to specify; they are:<\/p>\n<ul>\n<li><strong>trend<\/strong>: The type of trend component, as either \u201c<em>add<\/em>\u201d for additive or \u201c<em>mul<\/em>\u201d for multiplicative. Modeling the trend can be disabled by setting it to None.<\/li>\n<li><strong>damped<\/strong>: Whether or not the trend component should be damped, either True or False.<\/li>\n<li><strong>seasonal<\/strong>: The type of seasonal component, as either \u201c<em>add<\/em>\u201d for additive or \u201c<em>mul<\/em>\u201d for multiplicative. Modeling the seasonal component can be disabled by setting it to None.<\/li>\n<li><strong>seasonal_periods<\/strong>: The number of time steps in a seasonal period, e.g. 12 for 12 months in a yearly seasonal structure.<\/li>\n<li><strong>use_boxcox<\/strong>: Whether or not to perform a power transform of the series (True\/False) or specify the lambda for the transform.<\/li>\n<\/ul>\n<p>If you know enough about your problem to specify one or more of these parameters, then you should specify them. If not, you can try grid searching these parameters.<\/p>\n<p>We can start-off by defining a function that will fit a model with a given configuration and make a one-step forecast.<\/p>\n<p>The <em>exp_smoothing_forecast()<\/em> below implements this behavior.<\/p>\n<p>The function takes an array or list of contiguous prior observations and a list of configuration parameters used to configure the model.<\/p>\n<p>The configuration parameters in order are: the trend type, the dampening type, the seasonality type, the seasonal period, whether or not to use a Box-Cox transform, and whether or not to remove the bias when fitting the model.<\/p>\n<pre class=\"crayon-plain-tag\"># one-step Holt Winter's Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]<\/pre>\n<p>Next, we need to build up some functions for fitting and evaluating a model repeatedly via <a href=\"https:\/\/machinelearningmastery.com\/backtest-machine-learning-models-time-series-forecasting\/\">walk-forward validation<\/a>, including splitting a dataset into train and test sets and evaluating one-step forecasts.<\/p>\n<p>We can split a list or NumPy array of data using a slice given a specified size of the split, e.g. the number of time steps to use from the data in the test set.<\/p>\n<p>The <em>train_test_split()<\/em> function below implements this for a provided dataset and a specified number of time steps to use in the test set.<\/p>\n<pre class=\"crayon-plain-tag\"># split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]<\/pre>\n<p>After forecasts have been made for each step in the test dataset, they need to be compared to the test set in order to calculate an error score.<\/p>\n<p>There are many popular errors scores for time series forecasting. In this case, we will use root mean squared error (RMSE), but you can change this to your preferred measure, e.g. MAPE, MAE, etc.<\/p>\n<p>The <em>measure_rmse()<\/em> function below will calculate the RMSE given a list of actual (the test set) and predicted values.<\/p>\n<pre class=\"crayon-plain-tag\"># root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))<\/pre>\n<p>We can now implement the walk-forward validation scheme. This is a standard approach to evaluating a time series forecasting model that respects the temporal ordering of observations.<\/p>\n<p>First, a provided univariate time series dataset is split into train and test sets using the <em>train_test_split()<\/em> function. Then the number of observations in the test set are enumerated. For each, we fit a model on all of the history and make a one step forecast. The true observation for the time step is then added to the history, and the process is repeated. The <em>exp_smoothing_forecast()<\/em> function is called in order to fit a model and make a prediction. Finally, an error score is calculated by comparing all one-step forecasts to the actual test set by calling the <em>measure_rmse()<\/em> function.<\/p>\n<p>The <em>walk_forward_validation()<\/em> function below implements this, taking a univariate time series, a number of time steps to use in the test set, and an array of model configurations.<\/p>\n<pre class=\"crayon-plain-tag\"># walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error<\/pre>\n<p>If you are interested in making multi-step predictions, you can change the call to <em>predict()<\/em> in the <em>exp_smoothing_forecast()<\/em> function and also change the calculation of error in the <em>measure_rmse()<\/em> function.<\/p>\n<p>We can call <em>walk_forward_validation()<\/em> repeatedly with different lists of model configurations.<\/p>\n<p>One possible issue is that some combinations of model configurations may not be called for the model and will throw an exception, e.g. specifying some but not all aspects of the seasonal structure in the data.<\/p>\n<p>Further, some models may also raise warnings on some data, e.g. from the linear algebra libraries called by the statsmodels library.<\/p>\n<p>We can trap exceptions and ignore warnings during the grid search by wrapping all calls to <em>walk_forward_validation()<\/em> with a try-except and a block to ignore warnings. We can also add debugging support to disable these protections in case we want to see what is really going on. Finally, if an error does occur, we can return a <em>None<\/em> result; otherwise, we can print some information about the skill of each model evaluated. This is helpful when a large number of models are evaluated.<\/p>\n<p>The <em>score_model()<\/em> function below implements this and returns a tuple of (key and result), where the key is a string version of the tested model configuration.<\/p>\n<pre class=\"crayon-plain-tag\"># score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)<\/pre>\n<p>Next, we need a loop to test a list of different model configurations.<\/p>\n<p>This is the main function that drives the grid search process and will call the <em>score_model()<\/em> function for each model configuration.<\/p>\n<p>We can dramatically speed up the grid search process by evaluating model configurations in parallel. One way to do that is to use the <a href=\"https:\/\/pythonhosted.org\/joblib\/\">Joblib library<\/a>.<\/p>\n<p>We can define a <em>Parallel<\/em> object with the number of cores to use and set it to the number of CPU cores detected in your hardware.<\/p>\n<pre class=\"crayon-plain-tag\">executor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')<\/pre>\n<p>We can then create a list of tasks to execute in parallel, which will be one call to the <em>score_model()<\/em> function for each model configuration we have.<\/p>\n<pre class=\"crayon-plain-tag\">tasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)<\/pre>\n<p>Finally, we can use the <em>Parallel<\/em> object to execute the list of tasks in parallel.<\/p>\n<pre class=\"crayon-plain-tag\">scores = executor(tasks)<\/pre>\n<p>That\u2019s it.<\/p>\n<p>We can also provide a non-parallel version of evaluating all model configurations in case we want to debug something.<\/p>\n<pre class=\"crayon-plain-tag\">scores = [score_model(data, n_test, cfg) for cfg in cfg_list]<\/pre>\n<p>The result of evaluating a list of configurations will be a list of tuples, each with a name that summarizes a specific model configuration and the error of the model evaluated with that configuration as either the RMSE or None if there was an error.<\/p>\n<p>We can filter out all scores with a None.<\/p>\n<pre class=\"crayon-plain-tag\">scores = [r for r in scores if r[1] != None]<\/pre>\n<p>We can then sort all tuples in the list by the score in ascending order (best are first), then return this list of scores for review.<\/p>\n<p>The <em>grid_search()<\/em> function below implements this behavior given a univariate time series dataset, a list of model configurations (list of lists), and the number of time steps to use in the test set. An optional parallel argument allows the evaluation of models across all cores to be tuned on or off, and is on by default.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores<\/pre>\n<p>We\u2019re nearly done.<\/p>\n<p>The only thing left to do is to define a list of model configurations to try for a dataset.<\/p>\n<p>We can define this generically. The only parameter we may want to specify is the periodicity of the seasonal component in the series, if one exists. By default, we will assume no seasonal component.<\/p>\n<p>The <em>exp_smoothing_configs()<\/em> function below will create a list of model configurations to evaluate.<\/p>\n<p>An optional list of seasonal periods can be specified, and you could even change the function to specify other elements that you may know about your time series.<\/p>\n<p>In theory, there are 72 possible model configurations to evaluate, but in practice, many will not be valid and will result in an error that we will trap and ignore.<\/p>\n<pre class=\"crayon-plain-tag\"># create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models<\/pre>\n<p>We now have a framework for grid searching triple exponential smoothing model hyperparameters via one-step walk-forward validation.<\/p>\n<p>It is generic and will work for any in-memory univariate time series provided as a list or NumPy array.<\/p>\n<p>We can make sure all the pieces work together by testing it on a contrived 10-step dataset.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search holt winter's exponential smoothing\r\nfrom math import sqrt\r\nfrom multiprocessing import cpu_count\r\nfrom joblib import Parallel\r\nfrom joblib import delayed\r\nfrom warnings import catch_warnings\r\nfrom warnings import filterwarnings\r\nfrom statsmodels.tsa.holtwinters import ExponentialSmoothing\r\nfrom sklearn.metrics import mean_squared_error\r\n\r\n# one-step Holt Winter\u2019s Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]\r\n\r\n# root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))\r\n\r\n# split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]\r\n\r\n# walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error\r\n\r\n# score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)\r\n\r\n# grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores\r\n\r\n# create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models\r\n\r\nif __name__ == '__main__':\r\n\t# define dataset\r\n\tdata = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0]\r\n\tprint(data)\r\n\t# data split\r\n\tn_test = 4\r\n\t# model configs\r\n\tcfg_list = exp_smoothing_configs()\r\n\t# grid search\r\n\tscores = grid_search(data, cfg_list, n_test)\r\n\tprint('done')\r\n\t# list top 3 configs\r\n\tfor cfg, error in scores[:3]:\r\n\t\tprint(cfg, error)<\/pre>\n<p>Running the example first prints the contrived time series dataset.<\/p>\n<p>Next, the model configurations and their errors are reported as they are evaluated.<\/p>\n<p>Finally, the configurations and the error for the top three configurations are reported.<\/p>\n<pre class=\"crayon-plain-tag\">[10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0]\r\n\r\n > Model[[None, False, None, None, True, True]] 1.380\r\n > Model[[None, False, None, None, True, False]] 10.000\r\n > Model[[None, False, None, None, False, True]] 2.563\r\n > Model[[None, False, None, None, False, False]] 10.000\r\ndone\r\n\r\n[None, False, None, None, True, True] 1.379824445857423\r\n[None, False, None, None, False, True] 2.5628662672606612\r\n[None, False, None, None, False, False] 10.0<\/pre>\n<p>We do not report the model parameters optimized by the model itself. It is assumed that you can achieve the same result again by specifying the broader hyperparameters and allow the library to find the same internal parameters.<\/p>\n<p>You can access these internal parameters by refitting a standalone model with the same configuration and printing the contents of the \u2018<em>params<\/em>\u2018 attribute on the model fit; for example:<\/p>\n<pre class=\"crayon-plain-tag\">print(model_fit.params)<\/pre>\n<p>Now that we have a robust framework for grid searching ETS model hyperparameters, let\u2019s test it out on a suite of standard univariate time series datasets.<\/p>\n<p>The datasets were chosen for demonstration purposes; I am not suggesting that an ETS model is the best approach for each dataset, and perhaps an SARIMA or something else would be more appropriate in some cases.<\/p>\n<h2>Case Study 1: No Trend or Seasonality<\/h2>\n<p>The \u2018daily female births\u2019 dataset summarizes the daily total female births in California, USA in 1959.<\/p>\n<p>The dataset has no obvious trend or seasonal component.<\/p>\n<div id=\"attachment_6350\" style=\"width: 1450px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6350\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Daily-Female-Births-Dataset.png\" alt=\"Line Plot of the Daily Female Births Dataset\" width=\"1440\" height=\"780\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Daily-Female-Births-Dataset.png 1440w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Daily-Female-Births-Dataset-300x163.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Daily-Female-Births-Dataset-768x416.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Daily-Female-Births-Dataset-1024x555.png 1024w\" sizes=\"(max-width: 1440px) 100vw, 1440px\"><\/p>\n<p class=\"wp-caption-text\">Line Plot of the Daily Female Births Dataset<\/p>\n<\/div>\n<p>You can learn more about the dataset from <a href=\"https:\/\/datamarket.com\/data\/set\/235k\/daily-total-female-births-in-california-1959#!ds=235k&#038;display=line\">DataMarket<\/a>.<\/p>\n<p>Download the dataset directly from here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/daily-total-female-births.csv\">daily-total-female-births.csv<\/a><\/li>\n<\/ul>\n<p>Save the file with the filename \u2018<em>daily-total-female-births.csv<\/em>\u2018 in your current working directory.<\/p>\n<p>We can load this dataset as a Pandas series using the function <em>read_csv()<\/em>.<\/p>\n<pre class=\"crayon-plain-tag\">series = read_csv('daily-total-female-births.csv', header=0, index_col=0)<\/pre>\n<p>The dataset has one year, or 365 observations. We will use the first 200 for training and the remaining 165 as the test set.<\/p>\n<p>The complete example grid searching the daily female univariate time series forecasting problem is listed below.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search ets models for daily female births\r\nfrom math import sqrt\r\nfrom multiprocessing import cpu_count\r\nfrom joblib import Parallel\r\nfrom joblib import delayed\r\nfrom warnings import catch_warnings\r\nfrom warnings import filterwarnings\r\nfrom statsmodels.tsa.holtwinters import ExponentialSmoothing\r\nfrom sklearn.metrics import mean_squared_error\r\nfrom pandas import read_csv\r\n\r\n# one-step Holt Winter\u2019s Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]\r\n\r\n# root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))\r\n\r\n# split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]\r\n\r\n# walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error\r\n\r\n# score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)\r\n\r\n# grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores\r\n\r\n# create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models\r\n\r\nif __name__ == '__main__':\r\n\t# load dataset\r\n\tseries = read_csv('daily-total-female-births.csv', header=0, index_col=0)\r\n\tdata = series.values\r\n\tprint(data.shape)\r\n\t# data split\r\n\tn_test = 165\r\n\t# model configs\r\n\tcfg_list = exp_smoothing_configs()\r\n\t# grid search\r\n\tscores = grid_search(data, cfg_list, n_test)\r\n\tprint('done')\r\n\t# list top 3 configs\r\n\tfor cfg, error in scores[:3]:\r\n\t\tprint(cfg, error)<\/pre>\n<p>Running the example may take a few minutes as fitting each ETS model can take about a minute on modern hardware.<\/p>\n<p>Model configurations and the RMSE are printed as the models are evaluated The top three model configurations and their error are reported at the end of the run.<\/p>\n<p>We can see that the best result was an RMSE of about 7.08 births with the following configuration:<\/p>\n<ul>\n<li><strong>Trend<\/strong>: Additive<\/li>\n<li><strong>Damped<\/strong>: False<\/li>\n<li><strong>Seasonal<\/strong>: None<\/li>\n<li><strong>Seasonal Periods<\/strong>: None<\/li>\n<li><strong>Box-Cox Transform<\/strong>: True<\/li>\n<li><strong>Remove Bias<\/strong>: True<\/li>\n<\/ul>\n<p>What is surprising is that a model that assumed an additive trend performed better than one that didn\u2019t.<\/p>\n<p>We would not know that this is the case unless we threw out assumptions and grid searched models.<\/p>\n<pre class=\"crayon-plain-tag\">(365, 1)\r\n > Model[['add', False, None, None, True, True]] 7.081\r\n > Model[['add', False, None, None, True, False]] 7.113\r\n > Model[['add', False, None, None, False, True]] 7.112\r\n > Model[['add', False, None, None, False, False]] 7.115\r\n > Model[[None, False, None, None, True, True]] 7.169\r\n > Model[[None, False, None, None, True, False]] 7.212\r\n > Model[[None, False, None, None, False, True]] 7.117\r\n > Model[[None, False, None, None, False, False]] 7.126\r\n > Model[['add', True, None, None, True, False]] 7.170\r\n > Model[['add', True, None, None, True, True]] 7.118\r\n > Model[['add', True, None, None, False, True]] 7.113\r\n > Model[['add', True, None, None, False, False]] 7.126\r\ndone\r\n\r\n['add', False, None, None, True, True] 7.081359856193836\r\n['add', False, None, None, False, True] 7.111893396203345\r\n['add', True, None, None, False, True] 7.112743603181863<\/pre>\n<\/p>\n<h2>Case Study 2: Trend<\/h2>\n<p>The \u2018shampoo\u2019 dataset summarizes the monthly sales of shampoo over a three-year period.<\/p>\n<p>The dataset contains an obvious trend but no obvious seasonal component.<\/p>\n<div id=\"attachment_6351\" style=\"width: 1448px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6351\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Shampoo-Sales-Dataset.png\" alt=\"Line Plot of the Monthly Shampoo Sales Dataset\" width=\"1438\" height=\"776\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Shampoo-Sales-Dataset.png 1438w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Shampoo-Sales-Dataset-300x162.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Shampoo-Sales-Dataset-768x414.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Shampoo-Sales-Dataset-1024x553.png 1024w\" sizes=\"(max-width: 1438px) 100vw, 1438px\"><\/p>\n<p class=\"wp-caption-text\">Line Plot of the Monthly Shampoo Sales Dataset<\/p>\n<\/div>\n<p>You can learn more about the dataset from <a href=\"https:\/\/datamarket.com\/data\/set\/22r0\/sales-of-shampoo-over-a-three-year-period#!ds=22r0&#038;display=line\">DataMarket<\/a>.<\/p>\n<p>Download the dataset directly from here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/shampoo.csv\">shampoo.csv<\/a><\/li>\n<\/ul>\n<p>Save the file with the filename \u2018shampoo.csv\u2019 in your current working directory.<\/p>\n<p>We can load this dataset as a Pandas series using the function <em>read_csv()<\/em>.<\/p>\n<pre class=\"crayon-plain-tag\"># parse dates\r\ndef custom_parser(x):\r\n\treturn datetime.strptime('195'+x, '%Y-%m')\r\n\r\n# load dataset\r\nseries = read_csv('shampoo.csv', header=0, index_col=0, date_parser=custom_parser)<\/pre>\n<p>The dataset has three years, or 36 observations. We will use the first 24 for training and the remaining 12 as the test set.<\/p>\n<p>The complete example grid searching the shampoo sales univariate time series forecasting problem is listed below.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search ets models for monthly shampoo sales\r\nfrom math import sqrt\r\nfrom multiprocessing import cpu_count\r\nfrom joblib import Parallel\r\nfrom joblib import delayed\r\nfrom warnings import catch_warnings\r\nfrom warnings import filterwarnings\r\nfrom statsmodels.tsa.holtwinters import ExponentialSmoothing\r\nfrom sklearn.metrics import mean_squared_error\r\nfrom pandas import read_csv\r\nfrom pandas import datetime\r\n\r\n# one-step Holt Winter\u2019s Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]\r\n\r\n# root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))\r\n\r\n# split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]\r\n\r\n# walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error\r\n\r\n# score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)\r\n\r\n# grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores\r\n\r\n# create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models\r\n\r\n# parse dates\r\ndef custom_parser(x):\r\n\treturn datetime.strptime('195'+x, '%Y-%m')\r\n\r\nif __name__ == '__main__':\r\n\t# load dataset\r\n\tseries = read_csv('shampoo.csv', header=0, index_col=0, date_parser=custom_parser)\r\n\tdata = series.values\r\n\tprint(data.shape)\r\n\t# data split\r\n\tn_test = 12\r\n\t# model configs\r\n\tcfg_list = exp_smoothing_configs()\r\n\t# grid search\r\n\tscores = grid_search(data, cfg_list, n_test)\r\n\tprint('done')\r\n\t# list top 3 configs\r\n\tfor cfg, error in scores[:3]:\r\n\t\tprint(cfg, error)<\/pre>\n<p>Running the example is fast given there are a small number of observations.<\/p>\n<p>Model configurations and the RMSE are printed as the models are evaluated. The top three model configurations and their error are reported at the end of the run.<\/p>\n<p>We can see that the best result was an RMSE of about 97.91 sales with the following configuration:<\/p>\n<ul>\n<li><strong>Trend<\/strong>: Additive<\/li>\n<li><strong>Damped<\/strong>: False<\/li>\n<li><strong>Seasonal<\/strong>: None<\/li>\n<li><strong>Seasonal Periods<\/strong>: None<\/li>\n<li><strong>Box-Cox Transform<\/strong>: False<\/li>\n<li><strong>Remove Bias<\/strong>: True<\/li>\n<\/ul>\n<pre class=\"crayon-plain-tag\">(36, 1)\r\n > Model[['add', False, None, None, False, True]] 106.431\r\n > Model[['add', False, None, None, False, False]] 104.874\r\n > Model[[None, False, None, None, False, True]] 99.416\r\n > Model[[None, False, None, None, False, False]] 108.031\r\n > Model[['add', True, None, None, False, True]] 97.918\r\n > Model[['add', True, None, None, False, False]] 103.069\r\ndone\r\n\r\n['add', True, None, None, False, True] 97.91815887268478\r\n[None, False, None, None, False, True] 99.41551161747742\r\n['add', True, None, None, False, False] 103.06878140174923<\/pre>\n<\/p>\n<h2>Case Study 3: Seasonality<\/h2>\n<p>The \u2018monthly mean temperatures\u2019 dataset summarizes the monthly average air temperatures in Nottingham Castle, England from 1920 to 1939 in degrees Fahrenheit.<\/p>\n<p>The dataset has an obvious seasonal component and no obvious trend.<\/p>\n<div id=\"attachment_6352\" style=\"width: 1464px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6352\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Mean-Temperatures-Dataset.png\" alt=\"Line Plot of the Monthly Mean Temperatures Dataset\" width=\"1454\" height=\"766\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Mean-Temperatures-Dataset.png 1454w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Mean-Temperatures-Dataset-300x158.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Mean-Temperatures-Dataset-768x405.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Mean-Temperatures-Dataset-1024x539.png 1024w\" sizes=\"(max-width: 1454px) 100vw, 1454px\"><\/p>\n<p class=\"wp-caption-text\">Line Plot of the Monthly Mean Temperatures Dataset<\/p>\n<\/div>\n<p>You can learn more about the dataset from <a href=\"https:\/\/datamarket.com\/data\/set\/22li\/mean-monthly-air-temperature-deg-f-nottingham-castle-1920-1939#!ds=22li&#038;display=line\">DataMarket<\/a>.<\/p>\n<p>Download the dataset directly from here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-mean-temp.csv\">monthly-mean-temp.csv<\/a><\/li>\n<\/ul>\n<p>Save the file with the filename \u2018monthly-mean-temp.csv\u2019 in your current working directory.<\/p>\n<p>We can load this dataset as a Pandas series using the function <em>read_csv()<\/em>.<\/p>\n<pre class=\"crayon-plain-tag\">series = read_csv('monthly-mean-temp.csv', header=0, index_col=0)<\/pre>\n<p>The dataset has 20 years, or 240 observations.<\/p>\n<p>We will trim the dataset to the last five years of data (60 observations) in order to speed up the model evaluation process and use the last year, or 12 observations, for the test set.<\/p>\n<pre class=\"crayon-plain-tag\"># trim dataset to 5 years\r\ndata = data[-(5*12):]<\/pre>\n<p>The period of the seasonal component is about one year, or 12 observations.<\/p>\n<p>We will use this as the seasonal period in the call to the <em>exp_smoothing_configs()<\/em> function when preparing the model configurations.<\/p>\n<pre class=\"crayon-plain-tag\"># model configs\r\ncfg_list = exp_smoothing_configs(seasonal=[0, 12])<\/pre>\n<p>The complete example grid searching the monthly mean temperature time series forecasting problem is listed below.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search ets hyperparameters for monthly mean temp dataset\r\nfrom math import sqrt\r\nfrom multiprocessing import cpu_count\r\nfrom joblib import Parallel\r\nfrom joblib import delayed\r\nfrom warnings import catch_warnings\r\nfrom warnings import filterwarnings\r\nfrom statsmodels.tsa.holtwinters import ExponentialSmoothing\r\nfrom sklearn.metrics import mean_squared_error\r\nfrom pandas import read_csv\r\n\r\n# one-step Holt Winter\u2019s Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]\r\n\r\n# root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))\r\n\r\n# split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]\r\n\r\n# walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error\r\n\r\n# score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)\r\n\r\n# grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores\r\n\r\n# create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models\r\n\r\nif __name__ == '__main__':\r\n\t# load dataset\r\n\tseries = read_csv('monthly-mean-temp.csv', header=0, index_col=0)\r\n\tdata = series.values\r\n\t# trim dataset to 5 years\r\n\tdata = data[-(5*12):]\r\n\tprint(data.shape)\r\n\t# data split\r\n\tn_test = 12\r\n\t# model configs\r\n\tcfg_list = exp_smoothing_configs(seasonal=[12])\r\n\t# grid search\r\n\tscores = grid_search(data, cfg_list, n_test)\r\n\tprint('done')\r\n\t# list top 3 configs\r\n\tfor cfg, error in scores[:3]:\r\n\t\tprint(cfg, error)<\/pre>\n<p>Running the example is relatively slow given the large amount of data.<\/p>\n<p>Model configurations and the RMSE are printed as the models are evaluated. The top three model configurations and their error are reported at the end of the run.<\/p>\n<p>We can see that the best result was an RMSE of about 1.50 degrees with the following configuration:<\/p>\n<ul>\n<li><strong>Trend<\/strong>: None<\/li>\n<li><strong>Damped<\/strong>: False<\/li>\n<li><strong>Seasonal<\/strong>: Additive<\/li>\n<li><strong>Seasonal Periods<\/strong>: 12<\/li>\n<li><strong>Box-Cox Transform<\/strong>: False<\/li>\n<li><strong>Remove Bias<\/strong>: False<\/li>\n<\/ul>\n<pre class=\"crayon-plain-tag\">(60, 1)\r\n> Model[['add', True, None, 12, True, True]] 4.654\r\n> Model[['add', True, None, 12, True, False]] 4.597\r\n> Model[['add', True, None, 12, False, True]] 4.800\r\n> Model[['add', True, None, 12, False, False]] 4.760\r\n> Model[['add', False, 'add', 12, True, False]] 1.826\r\n> Model[['add', False, 'add', 12, True, True]] 1.860\r\n> Model[['add', False, None, 12, True, True]] 4.980\r\n> Model[['add', False, 'add', 12, False, True]] 1.707\r\n> Model[['add', False, 'add', 12, False, False]] 1.683\r\n> Model[['add', False, None, 12, True, False]] 4.900\r\n> Model[['add', False, None, 12, False, True]] 5.203\r\n> Model[['add', False, None, 12, False, False]] 5.151\r\n> Model[[None, False, 'add', 12, True, True]] 1.508\r\n> Model[[None, False, 'add', 12, True, False]] 1.507\r\n> Model[[None, False, 'add', 12, False, True]] 1.502\r\n> Model[[None, False, 'add', 12, False, False]] 1.502\r\n> Model[[None, False, None, 12, True, True]] 5.188\r\n> Model[[None, False, None, 12, True, False]] 5.143\r\n> Model[[None, False, None, 12, False, True]] 5.187\r\n> Model[[None, False, None, 12, False, False]] 5.143\r\n> Model[['add', True, 'add', 12, True, False]] 1.638\r\n> Model[['add', True, 'add', 12, False, True]] 1.568\r\n> Model[['add', True, 'add', 12, False, False]] 1.555\r\n> Model[['add', True, 'add', 12, True, True]] 1.646\r\ndone\r\n\r\n[None, False, 'add', 12, False, False] 1.5015471290238562\r\n[None, False, 'add', 12, False, True] 1.5015526638829775\r\n[None, False, 'add', 12, True, False] 1.5072252073652794<\/pre>\n<\/p>\n<h2>Case Study 4: Trend and Seasonality<\/h2>\n<p>The \u2018monthly car sales\u2019 dataset summarizes the monthly car sales in Quebec, Canada between 1960 and 1968.<\/p>\n<p>The dataset has an obvious trend and seasonal component.<\/p>\n<div id=\"attachment_6353\" style=\"width: 1472px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6353\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Car-Sales-Dataset.png\" alt=\"Line Plot of the Monthly Car Sales Dataset\" width=\"1462\" height=\"768\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Car-Sales-Dataset.png 1462w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Car-Sales-Dataset-300x158.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Car-Sales-Dataset-768x403.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2018\/07\/Line-Plot-of-the-Monthly-Car-Sales-Dataset-1024x538.png 1024w\" sizes=\"(max-width: 1462px) 100vw, 1462px\"><\/p>\n<p class=\"wp-caption-text\">Line Plot of the Monthly Car Sales Dataset<\/p>\n<\/div>\n<p>You can learn more about the dataset from <a href=\"https:\/\/datamarket.com\/data\/set\/22n4\/monthly-car-sales-in-quebec-1960-1968#!ds=22n4&#038;display=line\">DataMarket<\/a>.<\/p>\n<p>Download the dataset directly from here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv\">monthly-car-sales.csv<\/a><\/li>\n<\/ul>\n<p>Save the file with the filename \u2018monthly-car-sales.csv\u2019 in your current working directory.<\/p>\n<p>We can load this dataset as a Pandas series using the function <em>read_csv()<\/em>.<\/p>\n<pre class=\"crayon-plain-tag\">series = read_csv('monthly-car-sales.csv', header=0, index_col=0)<\/pre>\n<p>The dataset has nine years, or 108 observations. We will use the last year, or 12 observations, as the test set.<\/p>\n<p>The period of the seasonal component could be six months or 12 months. We will try both as the seasonal period in the call to the <em>exp_smoothing_configs()<\/em> function when preparing the model configurations.<\/p>\n<pre class=\"crayon-plain-tag\"># model configs\r\ncfg_list = exp_smoothing_configs(seasonal=[6,12])<\/pre>\n<p>The complete example grid searching the monthly car sales time series forecasting problem is listed below.<\/p>\n<pre class=\"crayon-plain-tag\"># grid search ets models for monthly car sales\r\nfrom math import sqrt\r\nfrom multiprocessing import cpu_count\r\nfrom joblib import Parallel\r\nfrom joblib import delayed\r\nfrom warnings import catch_warnings\r\nfrom warnings import filterwarnings\r\nfrom statsmodels.tsa.holtwinters import ExponentialSmoothing\r\nfrom sklearn.metrics import mean_squared_error\r\nfrom pandas import read_csv\r\nfrom pandas import datetime\r\n\r\n# one-step Holt Winter\u2019s Exponential Smoothing forecast\r\ndef exp_smoothing_forecast(history, config):\r\n\tt,d,s,p,b,r = config\r\n\t# define model\r\n\tmodel = ExponentialSmoothing(history, trend=t, damped=d, seasonal=s, seasonal_periods=p)\r\n\t# fit model\r\n\tmodel_fit = model.fit(optimized=True, use_boxcox=b, remove_bias=r)\r\n\t# make one step forecast\r\n\tyhat = model_fit.predict(len(history), len(history))\r\n\treturn yhat[0]\r\n\r\n# root mean squared error or rmse\r\ndef measure_rmse(actual, predicted):\r\n\treturn sqrt(mean_squared_error(actual, predicted))\r\n\r\n# split a univariate dataset into train\/test sets\r\ndef train_test_split(data, n_test):\r\n\treturn data[:-n_test], data[-n_test:]\r\n\r\n# walk-forward validation for univariate data\r\ndef walk_forward_validation(data, n_test, cfg):\r\n\tpredictions = list()\r\n\t# split dataset\r\n\ttrain, test = train_test_split(data, n_test)\r\n\t# seed history with training dataset\r\n\thistory = [x for x in train]\r\n\t# step over each time-step in the test set\r\n\tfor i in range(len(test)):\r\n\t\t# fit model and make forecast for history\r\n\t\tyhat = exp_smoothing_forecast(history, cfg)\r\n\t\t# store forecast in list of predictions\r\n\t\tpredictions.append(yhat)\r\n\t\t# add actual observation to history for the next loop\r\n\t\thistory.append(test[i])\r\n\t# estimate prediction error\r\n\terror = measure_rmse(test, predictions)\r\n\treturn error\r\n\r\n# score a model, return None on failure\r\ndef score_model(data, n_test, cfg, debug=False):\r\n\tresult = None\r\n\t# convert config to a key\r\n\tkey = str(cfg)\r\n\t# show all warnings and fail on exception if debugging\r\n\tif debug:\r\n\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\telse:\r\n\t\t# one failure during model validation suggests an unstable config\r\n\t\ttry:\r\n\t\t\t# never show warnings when grid searching, too noisy\r\n\t\t\twith catch_warnings():\r\n\t\t\t\tfilterwarnings(\"ignore\")\r\n\t\t\t\tresult = walk_forward_validation(data, n_test, cfg)\r\n\t\texcept:\r\n\t\t\terror = None\r\n\t# check for an interesting result\r\n\tif result is not None:\r\n\t\tprint(' > Model[%s] %.3f' % (key, result))\r\n\treturn (key, result)\r\n\r\n# grid search configs\r\ndef grid_search(data, cfg_list, n_test, parallel=True):\r\n\tscores = None\r\n\tif parallel:\r\n\t\t# execute configs in parallel\r\n\t\texecutor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')\r\n\t\ttasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)\r\n\t\tscores = executor(tasks)\r\n\telse:\r\n\t\tscores = [score_model(data, n_test, cfg) for cfg in cfg_list]\r\n\t# remove empty results\r\n\tscores = [r for r in scores if r[1] != None]\r\n\t# sort configs by error, asc\r\n\tscores.sort(key=lambda tup: tup[1])\r\n\treturn scores\r\n\r\n# create a set of exponential smoothing configs to try\r\ndef exp_smoothing_configs(seasonal=[None]):\r\n\tmodels = list()\r\n\t# define config lists\r\n\tt_params = ['add', 'mul', None]\r\n\td_params = [True, False]\r\n\ts_params = ['add', 'mul', None]\r\n\tp_params = seasonal\r\n\tb_params = [True, False]\r\n\tr_params = [True, False]\r\n\t# create config instances\r\n\tfor t in t_params:\r\n\t\tfor d in d_params:\r\n\t\t\tfor s in s_params:\r\n\t\t\t\tfor p in p_params:\r\n\t\t\t\t\tfor b in b_params:\r\n\t\t\t\t\t\tfor r in r_params:\r\n\t\t\t\t\t\t\tcfg = [t,d,s,p,b,r]\r\n\t\t\t\t\t\t\tmodels.append(cfg)\r\n\treturn models\r\n\r\nif __name__ == '__main__':\r\n\t# load dataset\r\n\tseries = read_csv('monthly-car-sales.csv', header=0, index_col=0)\r\n\tdata = series.values\r\n\tprint(data.shape)\r\n\t# data split\r\n\tn_test = 12\r\n\t# model configs\r\n\tcfg_list = exp_smoothing_configs(seasonal=[6,12])\r\n\t# grid search\r\n\tscores = grid_search(data, cfg_list, n_test)\r\n\tprint('done')\r\n\t# list top 3 configs\r\n\tfor cfg, error in scores[:3]:\r\n\t\tprint(cfg, error)<\/pre>\n<p>Running the example is slow given the large amount of data.<\/p>\n<p>Model configurations and the RMSE are printed as the models are evaluated. The top three model configurations and their error are reported at the end of the run.<\/p>\n<p>We can see that the best result was an RMSE of about 1,658 sales with the following configuration:<\/p>\n<ul>\n<li><strong>Trend<\/strong>: Additive<\/li>\n<li><strong>Damped<\/strong>: True<\/li>\n<li><strong>Seasonal<\/strong>: Additive<\/li>\n<li><strong>Seasonal Periods<\/strong>: 12<\/li>\n<li><strong>Box-Cox Transform<\/strong>: False<\/li>\n<li><strong>Remove Bias<\/strong>: True<\/li>\n<\/ul>\n<p>This is a little surprising as I would have guessed that a six-month seasonal model would be the preferred approach.<\/p>\n<pre class=\"crayon-plain-tag\">(108, 1)\r\n> Model[['add', True, 'add', 6, False, True]] 3240.433\r\n> Model[['add', True, 'add', 6, False, False]] 3226.384\r\n> Model[['add', True, 'add', 6, True, False]] 2823.588\r\n> Model[['add', True, 'add', 6, True, True]] 2810.103\r\n> Model[['add', True, 'add', 12, False, False]] 1684.424\r\n> Model[['add', True, 'add', 12, False, True]] 1658.925\r\n> Model[['add', True, None, 6, True, True]] 3933.443\r\n> Model[['add', True, None, 6, True, False]] 3915.510\r\n> Model[['add', True, None, 6, False, True]] 3924.489\r\n> Model[['add', True, None, 6, False, False]] 3905.487\r\n> Model[['add', True, None, 12, True, True]] 3935.659\r\n> Model[['add', True, None, 12, True, False]] 3915.499\r\n> Model[['add', True, 'add', 12, True, False]] 2067.595\r\n> Model[['add', True, 'add', 12, True, True]] 2095.083\r\n> Model[['add', False, 'add', 6, True, True]] 3220.532\r\n> Model[['add', False, 'add', 6, True, False]] 3199.766\r\n> Model[['add', True, None, 12, False, True]] 3934.066\r\n> Model[['add', True, None, 12, False, False]] 3905.367\r\n> Model[['add', False, None, 6, True, True]] 3815.765\r\n> Model[['add', False, None, 6, True, False]] 3813.234\r\n> Model[['add', False, None, 6, False, True]] 3805.651\r\n> Model[['add', False, 'add', 6, False, True]] 3243.478\r\n> Model[['add', False, None, 6, False, False]] 3813.920\r\n> Model[['add', False, None, 12, True, True]] 3815.765\r\n> Model[['add', False, 'add', 6, False, False]] 3226.955\r\n> Model[['add', False, None, 12, True, False]] 3813.234\r\n> Model[['add', False, None, 12, False, True]] 3805.700\r\n> Model[['add', False, None, 12, False, False]] 3809.819\r\n> Model[['add', False, 'add', 12, False, True]] 1675.977\r\n> Model[[None, False, 'add', 6, True, True]] 3204.875\r\n> Model[['add', False, 'add', 12, False, False]] 1733.999\r\n> Model[['add', False, 'add', 12, True, True]] 1833.418\r\n> Model[['add', False, 'add', 12, True, False]] 1833.608\r\n> Model[[None, False, 'add', 6, True, False]] 3190.972\r\n> Model[[None, False, 'add', 6, False, True]] 3147.644\r\n> Model[[None, False, 'add', 6, False, False]] 3049.932\r\n> Model[[None, False, 'add', 12, True, True]] 1834.905\r\n> Model[[None, False, 'add', 12, True, False]] 1872.182\r\n> Model[[None, False, 'add', 12, False, True]] 1751.538\r\n> Model[[None, False, 'add', 12, False, False]] 1800.867\r\n> Model[[None, False, None, 6, True, True]] 3801.741\r\n> Model[[None, False, None, 6, True, False]] 3783.966\r\n> Model[[None, False, None, 6, False, True]] 3801.560\r\n> Model[[None, False, None, 6, False, False]] 3783.966\r\n> Model[[None, False, None, 12, True, True]] 3801.741\r\n> Model[[None, False, None, 12, True, False]] 3783.966\r\n> Model[[None, False, None, 12, False, True]] 3801.560\r\n> Model[[None, False, None, 12, False, False]] 3783.966\r\ndone\r\n\r\n['add', True, 'add', 12, False, True] 1658.9253551827699\r\n['add', False, 'add', 12, False, True] 1675.9766454275066\r\n['add', True, 'add', 12, False, False] 1684.4244861443897<\/pre>\n<\/p>\n<h2>Extensions<\/h2>\n<p>This section lists some ideas for extending the tutorial that you may wish to explore.<\/p>\n<ul>\n<li><strong>Data Transforms<\/strong>. Update the framework to support configurable data transforms such as normalization and standardization.<\/li>\n<li><strong>Plot Forecast<\/strong>. Update the framework to re-fit a model with the best configuration and forecast the entire test dataset, then plot the forecast compared to the actual observations in the test set.<\/li>\n<li><strong>Tune Amount of History<\/strong>. Update the framework to tune the amount of historical data used to fit the model (e.g. in the case of the 10 years of max temperature data).<\/li>\n<\/ul>\n<p>If you explore any of these extensions, I\u2019d love to know.<\/p>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<h3>Books<\/h3>\n<ul>\n<li>Chapter 7 Exponential smoothing, <a href=\"https:\/\/amzn.to\/2xlJsfV\">Forecasting: principles and practice<\/a>, 2013.<\/li>\n<li>Section 6.4. Introduction to Time Series Analysis, <a href=\"https:\/\/www.itl.nist.gov\/div898\/handbook\/\">Engineering Statistics Handbook<\/a>, 2012.<\/li>\n<li><a href=\"https:\/\/amzn.to\/2LGKzKm\">Practical Time Series Forecasting with R<\/a>, 2016.<\/li>\n<\/ul>\n<h3>APIs<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.statsmodels.org\/dev\/generated\/statsmodels.tsa.holtwinters.ExponentialSmoothing.html\">statsmodels.tsa.holtwinters.ExponentialSmoothing API<\/a><\/li>\n<li><a href=\"http:\/\/www.statsmodels.org\/dev\/generated\/statsmodels.tsa.holtwinters.HoltWintersResults.html\">statsmodels.tsa.holtwinters.HoltWintersResults API<\/a><\/li>\n<li><a href=\"https:\/\/pythonhosted.org\/joblib\/\">Joblib: running Python functions as pipeline jobs<\/a><\/li>\n<\/ul>\n<h3>Articles<\/h3>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Exponential_smoothing\">Exponential smoothing on Wikipedia<\/a><\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to develop a framework for grid searching all of the exponential smoothing model hyperparameters for univariate time series forecasting.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>How to develop a framework for grid searching ETS models from scratch using walk-forward validation.<\/li>\n<li>How to grid search ETS model hyperparameters for daily time series data for births.<\/li>\n<li>How to grid search ETS model hyperparameters for monthly time series data for shampoo sales, car sales and temperature.<\/li>\n<\/ul>\n<p>Do you have any questions?<br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python\/\">How to Grid Search Triple Exponential Smoothing for Time Series Forecasting in Python<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee Exponential smoothing is a time series forecasting method for univariate data that can be extended to support data with a systematic trend [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2018\/10\/21\/how-to-grid-search-triple-exponential-smoothing-for-time-series-forecasting-in-python\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1199,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1198"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=1198"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1198\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/1199"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}