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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.