How to forecast a time series using exponential smoothing?

This recipe helps you forecast a time series using exponential smoothing

Recipe Objective

We can note that both Simple average and Weighted moving average lie on completely opposite ends. We would need something between these two extremes approaches which takes into account all the data while weighing the data points differently. It can be best done by exponential smoothing.

So this recipe is a short example on forecast a time series using exponential smoothing. Let's get started.

Access House Price Prediction Project using Machine Learning with Source Code

Step 1 - Import the library

import numpy as np import pandas as pd from statsmodels.tsa.arima_model import ARIMA from statsmodels.tsa.holtwinters import SimpleExpSmoothing

Let's pause and look at these imports. Numpy and pandas are general ones. Here statsmodels.tsa.holtwinters is used to import SimpleExpSmoothing library for building of model.

Step 2 - Setup the Data

df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']) df.head()

Here, we have used one time series data from github.

Now our dataset is ready.

Step 3 - Splitting Data

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

Here we have simply split data into size of 12 and rest elements

Step 4 - Building AR model

model = SimpleExpSmoothing(train_data.value) model_fitted = model.fit()

A simple model of exponential smoothing has been created in here for building our model on train values.

Step 5 - Printing the results

print('coefficients',model_fitted.params) predictions = model_fitted.predict(start=len(train_data), end=len(train_data) + len(test_data)-1) print(predictions)

Here, we have printed the coeffiecient of model and the predicted values.

Step 6 - Lets look at our dataset now

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

Scroll down the ipython file to visualize the output.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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 a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.