Mastering Demand Forecasting Techniques with Python

Demand Forecasting Featured Image

Demand forecasting is an essential part of supply chain management, as it helps businesses to estimate the quantity of goods and services that customers will demand in the future. This information is useful for businesses to plan their inventory, production, and distribution activities. By using data and statistical methods, businesses can predict future demand with greater accuracy, resulting in better decision-making and improved business outcomes.

Python is a popular programming language for data analysis and machine learning, and it offers several libraries and tools for demand forecasting. In this article, we will explore a simple example of demand forecasting using Python, pandas, and matplotlib.

Step 1: Import Required Libraries The first step is to import the required libraries, including pandas, numpy, datetime, and matplotlib. Pandas is a powerful data analysis library, numpy provides support for numerical calculations, datetime is used for date and time operations, and matplotlib is used for data visualization.

Step 2: Create Mock Data In this example, we will create mock sales data using the numpy library. We will generate sales data for four products (A, B, C, and D) for the year 2022. The sales data will be generated randomly for each day, with the total sales increasing gradually over time. Finally, we will save the sales data to a CSV file.

Step 3: Load the Sales Data from CSV file Next, we will load the sales data from the CSV file into a pandas dataframe.

Step 4: Extract Week Number, Year, and Group Sales Data We will extract the week number and year from the date column of the sales data using the datetime library. Then we will group the sales data by week, year, and product to get the total sales for each product in each week.

Step 5: Calculate 3-week Moving Average for Each Product To smooth out the fluctuations in the sales data, we will calculate a 3-week moving average for each product.

Step 6: Create an Empty DataFrame for Forecast Results We will create an empty dataframe to store the forecast results.

Step 7: Loop through Each Week and Generate Forecasts for the Next 5 Weeks We will loop through each week in the sales data and generate forecasts for the next 5 weeks. For each week, we will get the last 3 weeks of sales data and calculate the average sales for those weeks. Then we will generate a forecast for each of the next 5 weeks by using the average sales for the previous 3 weeks. Finally, we will add the forecast results to the dataframe created in step 6.

Step 8: Plot the Forecasted Sales Finally, we will use matplotlib to plot the forecasted sales data. The x-axis will represent the week number, and the y-axis will represent the forecasted sales.

This example demonstrates how to perform demand forecasting using Python, pandas, and matplotlib. While this is a relatively simple example, it can be easily extended to handle more complex scenarios with larger datasets and more sophisticated statistical models. By using demand forecasting, businesses can improve their operational efficiency, reduce costs, and deliver better value to their customers.

Python Code:

# Step 1: Import Required Libraries 
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import matplotlib.pyplot as plt

# Step 2: Create Mock Data, You can skip this step and directly go to step 3 if you have your own data with column date,product,sales,week_number,year
# create mock sales data
product_list = ['A', 'B', 'C', 'D']
date_rng = pd.date_range(start='1/1/2022', end='12/31/2022', freq='D')
sales_data = pd.DataFrame(date_rng, columns=['date'])
sales_data['product'] = np.random.choice(product_list, len(date_rng))
start_sales = np.random.randint(50, 100)
end_sales = np.random.randint(150, 200)
sales_data['sales'] = np.linspace(start_sales, end_sales, len(date_rng)) * np.linspace(0.8, 1.2, len(date_rng))
sales_data['sales'] = sales_data['sales'].round().astype(int)

# save sales data to CSV file
sales_data.to_csv('sales_data.csv', index=False)

# Step 3: Load the Sales data from CSV file
sales_data = pd.read_csv('sales_data.csv')

# Step 4: extract week number, year from date column and group sales data by week, year, and product
sales_data['date'] = pd.to_datetime(sales_data['date'])
sales_data['week_number'] = sales_data['date'].dt.isocalendar().week
sales_data['year'] = sales_data['date'].dt.year
product_data = sales_data.groupby(['week_number', 'year', 'product'])['sales'].sum().reset_index()

# Step 5: calculate 3-week moving average for each product
product_data['moving_avg'] = product_data.groupby(['week_number', 'year'])['sales'].transform(lambda x: x.rolling(window=3).mean())



# Step 6: create an empty DataFrame for forecast results
results = pd.DataFrame(columns=['week_number', 'year', 'forecast'])

# Step 7: loop through each week and generate forecasts for the next 5 weeks
for week, year in product_data[['week_number', 'year']].drop_duplicates().to_numpy():
    # filter data for the current week and year
    week_sales = product_data.loc[(product_data['week_number'] == week) & (product_data['year'] == year)]
    # get the last 3 weeks of sales for the current week and year
    last_3_weeks = week_sales.tail(3)['sales'].to_list()
    # generate forecasts for the next 5 weeks
    for i in range(1, 6):
        forecast_week_number = week + i
        forecast_year = year if forecast_week_number <= 52 else year + 1
        forecast = sum(last_3_weeks) / len(last_3_weeks)
        results = pd.concat([results, pd.DataFrame({'week_number': [forecast_week_number], 'year': [forecast_year], 'forecast': [forecast]})], ignore_index=True)

print(results)

# Step 8: plot the forecasted sales
plt.plot(results.index, results['forecast'])
plt.title('Forecasted Sales From Mock Sales Data')
plt.xlabel('Week')
plt.ylabel('Sales')
plt.show()
Demand Forecasting Plot from Mock Sales Data
Demand Forecasting Plot from Mock Sales Data
%d bloggers like this: