Exponential Smoothing Process Time Series Data in Python
Exponential Smoothing Process Time Series Data in Python: An Overview, Smoothing highlights patterns in data and lowers noise.
Time series data is smoothed using exponential smoothing. You will learn how to apply exponential smoothing in Python from this post.
Exponential Smoothing Process Time Series Data in Python
This technique uses patterns such as trends or seasons to forecast future values. More weight is given to recent data.
This aids in highlighting current alterations and trends. It is applied in the short-term forecasting process.
It is mostly utilized for economic and financial forecasting.
Different Exponential Smoothing Types
SES, or simple exponential smoothing: By giving recent observations more weight, this approach smoothes the data.
For data without trends or seasonality, it works best.
Correlation By Group in R » Data Science Tutorials
Holt’s Linear Trend Model: This technique takes data trends into consideration. To predict future values, it smooths the trend as well as the level.
The Holt-Winters Seasonal Model is used to analyze data that exhibits seasonality and trends.
It works well with data that has regular cycles because it incorporates additional smoothing for seasonal patterns.
Getting Ready
Let’s load our data and install the required libraries first. Initially, the imports:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
Let’s now import an example time series dataset and show the dataset’s first few rows:
# Create a sample time series data data = { 'Date': pd.date_range(start='2023-01-01', periods=100, freq='D'), 'Value': np.random.randn(100).cumsum() + 20 } df = pd.DataFrame(data) df.set_index('Date', inplace=True) # Display the dataset print(df.head())
We’re all set to go now.
1. Exponential Smoothing in Simple Form (SES)
Using a smoothing factor, the code uses Simple Exponential Smoothing (SES) on the data.
It forecasts the following ten periods by fitting the SES model to the data.
from statsmodels.tsa.holtwinters import SimpleExpSmoothing # Apply Simple Exponential Smoothing alpha = 0.2 ses_model = SimpleExpSmoothing(df['Value']).fit(smoothing_level=alpha, optimized=False) # Forecast for the next 10 periods forecast = ses_model.forecast(10) # Plot the original data and the forecast import matplotlib.pyplot as plt plt.figure(figsize=(12, 6)) plt.plot(df['Value'], label='Original Data') plt.plot(ses_model.fittedvalues, label='Fitted Values (SES)', color='red') plt.plot(forecast, label='Forecast', color='green') plt.title('Simple Exponential Smoothing') plt.xlabel('Date') plt.ylabel('Value') plt.legend() plt.show()
A flat line that extends from the most recent smoothed number is the forecast. Future values are assumed to remain constant with the most recent observation.
2. The Linear Trend Model of Holt
The code uses an additive trend component to apply Holt’s Linear Trend Model to the data. It forecasts the next ten periods and matches the model.
from statsmodels.tsa.holtwinters import ExponentialSmoothing # Apply Holt's Linear Trend Model holt_model = ExponentialSmoothing(df['Value'], trend='add').fit() # Forecast for the next 10 periods forecast_holt = holt_model.forecast(10) # Plot the original data and the forecast plt.figure(figsize=(12, 6)) plt.plot(df['Value'], label='Original Data') plt.plot(holt_model.fittedvalues, label='Fitted Values (Holt)', color='orange') plt.plot(forecast_holt, label='Forecast', color='green') plt.title("Holt's Linear Trend Model") plt.xlabel('Date') plt.ylabel('Value') plt.legend() plt.show()
The forecast line produced by Holt’s Linear Trend Model combines the trend and the smoothed level.
The forecast line slopes in the direction of the recent trend. Over time, it modifies to reflect the direction of the trend.
3. Seasonal Holt-Winters Model
The Holt-Winters model with additive trend and seasonality is applied to the data by the code. It forecasts the next ten periods and matches the model.
# Apply Holt-Winters Seasonal Model holt_winters_model = ExponentialSmoothing(df['Value'], trend='add', seasonal='add', seasonal_periods=12).fit() # Forecast for the next 10 periods forecast_hw = holt_winters_model.forecast(10) # Plot the original data and the forecast plt.figure(figsize=(12, 6)) plt.plot(df['Value'], label='Original Data') plt.plot(holt_winters_model.fittedvalues, label='Fitted Values (Holt-Winters)', color='purple') plt.plot(forecast_hw, label='Forecast', color='green') plt.title("Holt-Winters Seasonal Model") plt.xlabel('Date') plt.ylabel('Value') plt.legend() plt.show()
The smoothed level, trend, and seasonal patterns are combined by the Holt-Winters Seasonal Model to produce a forecast line.
The line forecasts values for the future and incorporates both the general trend and regular seasonal variations.
This forecast line displays the direction of the trend as well as sporadic seasonal variations.
Conclusion,
The potential By emphasizing current patterns, smoothing helps make time series data more readable.
This method can be used to smooth your data and enhance predictions using Python.
It’s an easy method to anticipate and comprehend your time series data more effectively.
Free Data Science Books » EBooks » finnstats