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.

Learn Time Series Forecasting using ARIMA Model in Python

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('https://raw.githubusercontent.com/selva86/datasets/master/a10.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).

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

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.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.