From 92cf65fc18025387231cd0e44e53c64c63085553 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 1 Dec 2024 17:50:46 +0700 Subject: [PATCH] Create time_series_forecasting.py --- src/ai/time_series_forecasting.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/ai/time_series_forecasting.py diff --git a/src/ai/time_series_forecasting.py b/src/ai/time_series_forecasting.py new file mode 100644 index 000000000..6038fe107 --- /dev/null +++ b/src/ai/time_series_forecasting.py @@ -0,0 +1,29 @@ +import pandas as pd +from statsmodels.tsa.arima.model import ARIMA +import matplotlib.pyplot as plt + +class TimeSeriesForecasting: + def __init__(self, data): + self.data = data + + def fit_model(self, order=(1, 1, 1)): + """Fit the ARIMA model to the data.""" + self model = ARIMA(self.data, order=order) + self.model_fit = self.model.fit() + + def forecast(self, steps=5): + """Forecast future values.""" + forecasted_values = self.model_fit.forecast(steps=steps) + return forecasted_values + + def plot_forecast(self, steps=5): + """Plot the forecasted values along with the original data.""" + forecasted_values = self.forecast(steps) + plt.figure(figsize=(12, 6)) + plt.plot(self.data, label='Historical Data') + plt.plot(range(len(self.data), len(self.data) + steps), forecasted_values, label='Forecasted Values', color='red') + plt.title('Time Series Forecasting') + plt.xlabel('Time') + plt.ylabel('Values') + plt.legend() + plt.show()