Forecasting Demand, Finding Sales Data - Facebook Prophet, Google Trends & Python
Introduction
Let's talk about forecasting demand, this is as old as money and commerce. Rainy season, you stock up on umbrellas; winter, winter coats, etc. I'll walk you through a simple example using open-source FBProphet in Python, one of the most powerful forecasting engines and also one of the easiest to use (once you manage to install it).
Code
from IPython.display import Image
Image(filename='Forecasting-Demand.png')
# Image(filename='fb-prophet.png')
Forecasting Online Interest and Demand¶
Let's talk about forecasting demand, this is as old as money and commerce. Rainy seasons you stock up on umbrellas, for winter, winter coats, etc. It is also critical today, where so many stores around the world are closed due to covid19 and want an online presence, a virtual store to mirror Amazon and the likes. And most products aren't as straightforward as selling umbrellas.
I'll walk you through a simple example using open-source FBProphet in Python, one of the most powerful forecasting engines and also one of the easiest to use (once you manage to install it). I'll also show you external data sources that work great as proxy values to forecast demand when you want to understand products you don't carry or you don't have enough data stored.
To properly forecast demand you need enough data to teach a model the different cycles and season around that product, preferably a few of those cycles. FBProphet recommends 3 years of data so the model can understand the different holidays, peak sales, low sales, etc.
Let me show you an example using anonymized data from a Kaggle competition the "Store Item Demand Forecasting Challenge"
Open Source FBProphet¶
"Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well"
"It works best with time series that have strong seasonal effects and several seasons of historical data."
"Prophet is used in many applications across Facebook for producing reliable forecasts for planning and goal setting. We’ve found it to perform better than any other approach in the majority of cases."
FBProphet Quick Start Guide¶
https://facebook.github.io/prophet/docs/quick_start.html
Installing FB Prophet:¶
!pip3 install fbprophet --user
!pip3 install holidays==0.9.12
Kaggle data¶
https://www.kaggle.com/c/demand-forecasting-kernels-only/data
import time
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
import io, base64, os, json, re, glob
import datetime
from datetime import timedelta
import pandas as pd
import pydata_google_auth
import numpy as np
from fbprophet import Prophet
import statsmodels.api as sm
df_raw = pd.read_csv('demand-forecasting-kernels-only/train.csv',
low_memory=False,
parse_dates=['date'],
index_col=['date'])
df_raw = df_raw.sort_values('date', ascending=True)
print(df_raw.shape)
df_raw.head()
Store 1 - All Sales¶
plt.subplots(1, figsize = (16, 5))
plt.grid()
plt.title('Store 1 - All Sales')
for item in set(df_raw[df_raw['store'] == 1]['item']):
plt.plot(df_raw[(df_raw['store'] == 1) &
(df_raw['item'] == item)]['sales'].rolling(window=50).mean())
Item 1 All Stores¶
plt.subplots(1, figsize = (16, 5))
plt.grid()
plt.title('Item 1 - Sales per Store')
for store in set(df_raw[df_raw['item'] == 1]['store']):
plt.plot(df_raw[(df_raw['item'] == 1) &
(df_raw['store'] == store)]['sales']
.rolling(window=50).mean())
Predicting the Future Facebook's FBProphet¶
# preparing the data into FBP format:
train_dataset = df_raw[(df_raw['item'] == 1) & df_raw['store'] == 1]
train_dataset.reset_index(level=0, inplace=True)
train_dataset = train_dataset[['date', 'sales']]
train_dataset.columns = ["ds", "y"]
train_dataset = train_dataset.sample(5000)
prophet_basic = Prophet()
prophet_basic.fit(train_dataset)
future= prophet_basic.make_future_dataframe(periods=365)
future
forecast=prophet_basic.predict(future)
#Plotting the predicted data
fig1 =prophet_basic.plot(forecast)
fig1 = prophet_basic.plot_components(forecast)
from fbprophet.plot import add_changepoints_to_plot
fig = prophet_basic.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), prophet_basic, forecast)
What to Do If You Don't Have Years of Data or You Are Trying out a New Product?¶
- Google Trends - Explore what the world is searching
- Amazon Best Sellers - Our most popular products based on sales. Updated hourly
- StackLine - Industry News
Google Trends - Vitamin D¶
# Google Trends - Vitamin D
# https://trends.google.com/trends/explore?date=today%205-y&geo=US&q=vitamin%20d
df_vitamins = pd.read_csv('vitamin_d.csv')
df_vitamins['ds'] = pd.to_datetime(df_vitamins['ds'])
df_vitamins = df_vitamins.sort_values('ds', ascending=True)
print(df_raw.shape)
df_vitamins.head()
train_dataset = df_vitamins.copy()
prophet_basic = Prophet()
prophet_basic.fit(train_dataset)
future= prophet_basic.make_future_dataframe(periods=365)
future.tail(100)
forecast=prophet_basic.predict(future)
#Plotting the predicted data
fig1 =prophet_basic.plot(forecast)
fig1 = prophet_basic.plot_components(forecast)
# access predictions
forecast.tail()
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
Let's talk about forecasting demand, this is as old as money and commerce. Rainy season, you stock up on umbrellas; winter, winter coats, etc. I'll walk you through a simple example using open-source FBProphet in Python, one of the most powerful forecasting engines and also one of the easiest to use (once you manage to install it).