How to find optimal paramters for ARIMA model?

How to find optimal paramters for ARIMA model?

How to find optimal paramters for ARIMA model?

This recipe helps you find optimal paramters for ARIMA model


Recipe Objective

The ARIMA model for time series analysis and forecasting can be tricky to configure. We can automate the process of evaluating a large number of hyperparameters for the ARIMA model by using a grid search procedure.

So this recipe is a short example on how to find optimal paramters for ARIMA model. Let's get started.

Step 1 - Import the library

import warnings import numpy as np import pandas as pd from statsmodels.tsa.arima_model import ARIMA from sklearn.metrics import mean_squared_error

Let's pause and look at these imports. Numpy, pandas and warnings are general ones. Here, statsmodels.tsa.arima_model will help in building our model. mean_squared_error will be used for calculating MSE score.

Step 2 - Setup the Data

df = pd.read_csv('', parse_dates=['date']).set_index('date')

Here, we have used one time series data from github. Also, we have set our index to date.

Now our dataset is ready.

Step 3 - Splitting Dataset

train_data = df[1:len(df)-12] test_data = df[len(df)-12:]

Here, we have simply broken our dataset to two parts as test and train.

Step 4 - GridSearch

p_values = [0, 1] d_values = range(0, 2) q_values = range(0, 2)

Here, we have defined p,d and q for hyperparameter testing.

Step 5 - Looping for testing

for p in p_values: for d in d_values: for q in q_values: order = (p,d,q) warnings.filterwarnings("ignore") model = ARIMA(train_data.value, order=order).fit() predictions = model.predict(start=len(train_data), end=len(train_data) + len(test_data)-1) error = mean_squared_error(test_data, predictions) print('ARIMA%s MSE=%.3f' % (order,error))

With each loop, we choose one parameter, fit the model and calculate the MSE over predictions. Later we choose the best model by looking at lowest MSE score.

Step 6 - Lets look at our dataset now

Once we run the above code snippet, we will see:

Srcoll down the ipython file to visualize the results.

Best model to choose is (1,0,1).

Relevant Projects

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.