Exponential Smoothing
Solution :
Library Installation:
To install required library files, Open Command Prompt or Terminal and execute the following commands
$ pip install scikit-learn
$ pip install pandas
$ pip install matplotlib
$ pip install statsmodels
Dataset (stock_data.csv) :
Date,Open,High,Low,Close,Adj Close,Volume
2015-01-02,26.778,27.240,26.564,27.004,23.404,24742002
2015-01-05,27.073,27.487,26.901,27.060,23.452,26967002
2015-01-06,27.118,27.264,26.684,27.029,23.424,23850002
2015-01-07,27.183,27.450,26.959,27.254,23.607,25516002
2015-01-08,27.387,27.717,27.093,27.486,23.815,2877100
2015-01-09,27.500,27.850,27.300,27.700,24.000,3000000
2015-01-12,27.800,28.100,27.600,27.950,24.200,3100000
2015-01-13,28.000,28.300,27.900,28.150,24.350,3200000
2015-01-14,28.200,28.500,28.000,28.400,24.600,3300000
2015-01-15,28.500,28.900,28.300,28.700,24.900,3400000
2015-01-16,28.800,29.000,28.500,28.900,25.100,3500000
2015-01-19,29.000,29.200,28.700,29.100,25.300,3600000
2015-01-20,29.100,29.500,28.900,29.400,25.600,3700000
2015-01-21,29.300,29.700,29.100,29.600,25.800,3800000
2015-01-22,29.500,29.900,29.300,29.900,26.000,3900000
PROGRAM: (EXPSMOTH.py)
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
# Load stock data
df = pd.read_csv('stock_data.csv', index_col='Date', parse_dates=True)
stock_symbol = 'SampleStock'
# Split train/test
train = df['Close'][:-5]
test = df['Close'][-5:]
# Fit Exponential Smoothing model
model_fit = ExponentialSmoothing(train, trend='add', seasonal=None).fit()
# Make predictions
predictions = model_fit.forecast(steps=len(test))
# Plot actual vs predicted
plt.figure(figsize=(10,6))
plt.plot(test.index, test, label='Actual')
plt.plot(test.index, predictions, label='Predicted', color='red')
plt.title(f'{stock_symbol} Stock Price Forecast using Exponential Smoothing')
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT:
๐ป Run This Python Program
Exponential Smoothing
The selected Python program is loaded
automatically into the editor.
Edit the program, provide input through
STDIN if required
and click Run.
The compiler is embedded using OneCompiler.
Your Python program runs inside the embedded
Python environment; Study Glance does not execute
Python code directly on your web server.
If the program asks for input, type the value in the
STDIN box before clicking Run.
These programs use libraries such as NumPy, Pandas,
Matplotlib, scikit-learn, SciPy and statsmodels, and
graph windows (
plt.show()) cannot be displayed
here. The data files shown in the lesson (CSV) are loaded
into the editor automatically. If a library is not
available in the embedded compiler, use
Copy Program and run the program on your
computer (after pip install of the libraries)
or in Google Colab.