Time Series Forecasting: Leveraging ARIMA and Python with Streamlit for Powerful Visualizations”
In a world driven by data, the ability to predict future trends and patterns has become an invaluable skill across industries. Time series forecasting, a technique that leverages historical data to predict future values, plays a pivotal role in decision-making, resource allocation, and risk management. The Autoregressive Integrated Moving Average (ARIMA) model stands out as a robust and widely-used tool.
In this article,we will explore time series forecasting using ARIMA and Python, exploring not only the intricacies of the model but also the power of visualization through Streamlit.
Let us start by understanding what ARIMA means:
AutoRegressive: This part of ARIMA looks at the relationship between the current value and the previous values in the series. In simpler terms, it considers that future values are influenced by past values. If today’s weather is warm, there’s a good chance tomorrow’s weather will also be warm.
Integrated: This part involves making the time series data “stationary.” In stationary data, the statistical properties (like mean and variance) don’t change over time. Sometimes, data can have trends or seasonal patterns that need to be removed for accurate forecasting. “Integration” involves subtracting the previous value from the current value to remove these trends.
Moving Average: This part considers the relationship between the current value and the previous prediction errors (the difference between predicted and actual values) in the series. It helps to capture short-term fluctuations in the data that might not be explained by the AR or I parts alone.
ARIMA combines all these components to create a model that can capture both short-term fluctuations and long-term trends in time series data
Lets dive deeper with practical implementation using Python
Use case: We will be forecasting next three months sales for products by seller taking into consideration last three months sales data.
Prerequisites
— pip install statsmodels
— pip install pandas
— pip install streamlit
- Create a folder called “Forecasting”
- Create a “app.py” file inside the folder Forecasting
- Download the file “sales.csv” and save it inside the folder Forecasting
Copy the below code in the app.py file
import pandas as pd
import statsmodels.api as sm
import warnings
from dateutil.relativedelta import relativedelta
import streamlit as st
class SalesPredictor:
warnings.filterwarnings("ignore")
st.set_page_config(layout="wide")
def __init__(self):
self.sales_df=pd.read_csv('sales.csv')
self.products = list(set(self.sales_df['Product'].tolist()))
self.sellers = list(set(self.sales_df['Seller'].tolist()))
self.sales_df['Date'] = pd.to_datetime(self.sales_df['Date'])
def forecast_arima(self, product, seller):
subset = self.sales_df[(self.sales_df['Product'] == product) & (self.sales_df['Seller'] == seller)]
time_series = pd.Series(subset['Sales'].values, index=subset['Date'])
last_month = time_series.index[len(time_series) - 1]
last_month = last_month + relativedelta(months=1)
model = sm.tsa.ARIMA(time_series, order=(2, 1, 1))
model_fit = model.fit()
forecast_steps = 3
forecast = model_fit.forecast(steps=forecast_steps)
dates = pd.date_range(start=last_month, periods=forecast_steps, freq='M')
date_pred = [date.strftime('%b %y') for date in dates]
seller_pred = [seller] * forecast_steps
product_pred = [product] * forecast_steps
return date_pred, seller_pred, product_pred, forecast.tolist()
def run(self):
col_table, col_graph = st.columns(2)
with st.sidebar:
st.title("Sales Forecasting")
selected_product = st.selectbox("Select a product", self.products)
selected_seller = st.selectbox("Select a seller", self.sellers)
if st.button("Predict"):
date_pred, seller_pred, product_pred, forecast_pred = self.forecast_arima(selected_product,
selected_seller)
predicted_dataframe = pd.DataFrame({
"Date": date_pred,
"Product": product_pred,
"Seller": seller_pred,
"Forecast": forecast_pred
})
col_graph.line_chart(
predicted_dataframe,
use_container_width=True,
height=200,
x='Forecast',
y='Date'
)
col_table.write(predicted_dataframe)
if __name__ == "__main__":
predictor = SalesPredictor()
predictor.run()
Detail explaination of what the code does:
Import Libraries: The necessary libraries are imported, including pandas
, statsmodels
, warnings
, relativedelta
from dateutil
, and streamlit
.
Class Initialization: The SalesPredictor
class is defined. Inside its __init__
method, the class initializes by reading sales data from a CSV file called 'sales.csv' and converting the 'Date' column to a datetime format. It also sets up the list of unique products and sellers.
ARIMA Forecasting Method: The forecast_arima
method is defined within the class. It takes a product
and seller
as inputs, then:
- Filters the sales data for the selected product and seller.
- Creates a time series from the filtered sales data.
- Uses the ARIMA (AutoRegressive Integrated Moving Average) model to forecast sales for the next 3 months (adjustable).
- Returns lists of formatted dates, seller names, product names, and forecasted sales values.
Main Run Method: The run
method is defined within the class. This method sets up the Streamlit web application.
- It uses the
st.set_page_config
function to configure the layout of the page as "wide". - The sidebar is used to present user input controls for selecting a product and a seller. When the “Predict” button is clicked, the
forecast_arima
method is called to generate forecasts. - The forecasts are displayed as a line chart in the main content area, using the
col_graph.line_chart
function. - The forecast data is also displayed as a table below the line chart using the
col_table.write
function.
Main Block: The code block checks if the script is being run as the main program. If it is, an instance of the SalesPredictor
class is created, and the run
method is called to start the Streamlit app
To run the code, open a CMD prompt inside the “Forecasting” directory and run the command python -m streamlit run app.py
You will now have an option so select a product and the seller to forecast sales for the next three months
A Message from AI Mind
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the AI Mind Publication
- 🧠 Improve your AI prompts effortlessly and FREE
- 🧰 Discover Intuitive AI Tools