Code Monkey home page Code Monkey logo

tsa_exp7's Introduction

Ex.No: 07 -- AUTO REGRESSIVE MODEL...

Date:

AIM :

To Implement an Auto Regressive Model using Python.

ALGORITHM :

Step 1 :

Import necessary libraries.

Step 2 :

Read the CSV file into a DataFrame.

Step 3 :

Perform Augmented Dickey-Fuller test.

Step 4 :

Split the data into training and testing sets.Fit an AutoRegressive (AR) model with 13 lags.

Step 5 :

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

Step 6 :

Make predictions using the AR model.Compare the predictions with the test data.

Step 7 :

Calculate Mean Squared Error (MSE).Plot the test data and predictions.

PROGRAM :

Import necessary libraries :

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 CSV file into a DataFrame :

data = pd.read_csv("/content/Temperature.csv")  
data['date'] = pd.to_datetime(data['date'])
data.set_index('date', inplace=True)

Perform Augmented Dickey-Fuller test :

result = adfuller(data['temp']) 
print('ADF Statistic:', result[0])
print('p-value:', result[1])

Split the data into training and testing sets :

train_data = data.iloc[:int(0.8*len(data))]
test_data = data.iloc[int(0.8*len(data)):]

Fit an AutoRegressive (AR) model with 13 lags :

lag_order = 13
model = AutoReg(train_data['temp'], lags=lag_order)
model_fit = model.fit()

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

plot_acf(data['temp'])
plt.title('Autocorrelation Function (ACF)')
plt.show()
plot_pacf(data['temp'])
plt.title('Partial Autocorrelation Function (PACF)')
plt.show()

Make predictions using the AR model :

predictions = model_fit.predict(start=len(train_data), end=len(train_data)+len(test_data)-1)

Compare the predictions with the test data :

mse = mean_squared_error(test_data['temp'], predictions)
print('Mean Squared Error (MSE):', mse)

Plot the test data and predictions :

plt.plot(test_data.index, test_data['temp'], label='Test Data')
plt.plot(test_data.index, predictions, label='Predictions')
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.title('AR Model Predictions vs Test Data')
plt.legend()
plt.show()

OUTPUT :

GIVEN DATA :

1

Augmented Dickey-Fuller test :

2

PACF - ACF :

3 5

Mean Squared Error :

6

PREDICTION :

7

RESULT :

Thus, we have successfully implemented the auto regression function using python.

tsa_exp7'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.