To implement the Holt Winters Method Model using Python.
- You import the necessary libraries
- You load a CSV file containing daily airpassengers data into a DataFrame, parse the 'date' column as datetime, and perform some initial data exploration
- You group the data by date and resample it to a monthly frequency (beginning of the month
- You plot the time series data
- You import the necessary 'statsmodels' libraries for time series analysis
- You decompose the time series data into its additive components and plot them:
- You calculate the root mean squared error (RMSE) to evaluate the model's performance
- You calculate the mean and standard deviation of the entire sales dataset, then fit a Holt- Winters model to the entire dataset and make future predictions
- You plot the original passengers data and the predictions
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
data=pd.read_csv("/content/AirPassengers.csv")
data
data['Month'] = pd.to_datetime(data['Month'])
data.set_index('Month', inplace=True)
model = ExponentialSmoothing(data, trend="add", seasonal="add", seasonal_periods=12)
fit = model.fit()
n_steps = 12
forecast = fit.forecast(steps=n_steps)
forecast
plt.figure(figsize=(10, 6))
plt.plot(data.index, data, label='Original Data')
plt.plot(pd.date_range(start=data.index[-1], periods=n_steps+1, freq='M')[1:], forecast, label='Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Holt-Winters Forecast')
plt.legend()
plt.show()
Thus the program run successfully based on the Holt Winters Method model.