Code Monkey home page Code Monkey logo

tsa_exp8's Introduction

Ex.No: 08 -- MOVING AVERAGE MODEL AND EXPONENTIAL SMOOTHING...

Date:

AIM :

To implement Moving Average Model and Exponential smoothing Using Python.

ALGORITHM :

Step 1 :

Import necessary libraries.

Step 2 :

Read the AirLinePassengers data from a CSV file,Display the shape and the first 20 rows of the dataset.

Step 3 :

Set the figure size for plots.

Step 4 :

Suppress warnings.

Step 5 :

Plot the first 50 values of the 'Value' column.

Step 6 :

Perform rolling average transformation with a window size of 5.

Step 7 :

Display the first 10 values of the rolling mean.

Step 8 :

Perform rolling average transformation with a window size of 10.

Step 9 :

Create a new figure for plotting,Plot the original data and fitted value.

Step 10 :

Show the plot.

Step 11 :

Also perform exponential smoothing and plot the graph.

PROGRAM :

Import the packages :

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.ar_model import AutoReg
from sklearn.metrics import mean_squared_error

Read the Airline Passengers dataset from a CSV file :

data = pd.read_csv("/content/airline.csv")

Display the shape and the first 50 rows of the dataset :

print("Shape of the dataset:", data.shape)
print("First 50 rows of the dataset:")
print(data.head(50))

Plot the first 50 values of the 'International' column :

plt.plot(data['International '].head(50))
plt.title('First 50 values of the "International" column')
plt.xlabel('Index')
plt.ylabel('International Passengers')
plt.show()

Perform rolling average transformation with a window size of 5 :

rolling_mean_5 = data['International '].rolling(window=5).mean()

Display the first 10 values of the rolling mean :

print("First 10 values of the rolling mean with window size 5:")
print(rolling_mean_5.head(10))

Perform rolling average transformation with a window size of 10 :

rolling_mean_10 = data['International '].rolling(window=10).mean()

Plot the original data and fitted value (rolling mean with window size 10) :

plt.plot(data['International '], label='Original Data')
plt.plot(rolling_mean_10, label='Rolling Mean (window=10)')
plt.title('Original Data and Fitted Value (Rolling Mean)')
plt.xlabel('Index')
plt.ylabel('International Passengers')
plt.legend()
plt.show()

Fit an AutoRegressive (AR) model with 13 lags :

lag_order = 13
model = AutoReg(data['International '], lags=lag_order)
model_fit = model.fit()

Plot Partial Autocorrelation Function (PACF) and Autocorrelation Function (ACF) :

plot_acf(data['International '])
plt.title('Autocorrelation Function (ACF)')
plt.show()

plot_pacf(data['International '])
plt.title('Partial Autocorrelation Function (PACF)')
plt.show()

Make predictions using the AR model :

predictions = model_fit.predict(start=lag_order, end=len(data)-1)

Compare the predictions with the original data :

mse = mean_squared_error(data['International '][lag_order:], predictions)
print('Mean Squared Error (MSE):', mse)

Plot the original data and predictions :

plt.plot(data['International '][lag_order:], label='Original Data')
plt.plot(predictions, label='Predictions')
plt.title('AR Model Predictions vs Original Data')
plt.xlabel('Index')
plt.ylabel('International Passengers')
plt.legend()
plt.show()

OUTPUT :

Plot the original data and fitted value :

m1

Plot Partial Autocorrelation Function (PACF) and Autocorrelation Function (ACF) :

m2

m3

Plot the original data and predictions :

m4

RESULT :

Thus, we have successfully implemented the Moving Average Model and Exponential smoothing using python.

tsa_exp8's People

Contributors

varalakshmi1084 avatar vishwarathinam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.