!pip install plotly
Requirement already satisfied: plotly in /usr/local/lib/python3.7/dist-packages (4.4.1) Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from plotly) (1.15.0) Requirement already satisfied: retrying>=1.3.3 in /usr/local/lib/python3.7/dist-packages (from plotly) (1.3.3)
!pip install pystan==2.19.1.1
Requirement already satisfied: pystan==2.19.1.1 in /usr/local/lib/python3.7/dist-packages (2.19.1.1) Requirement already satisfied: Cython!=0.25.1,>=0.22 in /usr/local/lib/python3.7/dist-packages (from pystan==2.19.1.1) (0.29.23) Requirement already satisfied: numpy>=1.7 in /usr/local/lib/python3.7/dist-packages (from pystan==2.19.1.1) (1.19.5)
!pip install fbprophet
Requirement already satisfied: fbprophet in /usr/local/lib/python3.7/dist-packages (0.7.1) Requirement already satisfied: Cython>=0.22 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (0.29.23) Requirement already satisfied: cmdstanpy==0.9.5 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (0.9.5) Requirement already satisfied: pystan>=2.14 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (2.19.1.1) Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (1.19.5) Requirement already satisfied: pandas>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (1.1.5) Requirement already satisfied: matplotlib>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (3.2.2) Requirement already satisfied: LunarCalendar>=0.0.9 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (0.0.9) Requirement already satisfied: convertdate>=2.1.2 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (2.3.2) Requirement already satisfied: holidays>=0.10.2 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (0.10.5.2) Requirement already satisfied: setuptools-git>=1.2 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (1.2) Requirement already satisfied: python-dateutil>=2.8.0 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (2.8.1) Requirement already satisfied: tqdm>=4.36.1 in /usr/local/lib/python3.7/dist-packages (from fbprophet) (4.41.1) Requirement already satisfied: pymeeus<=1,>=0.3.13 in /usr/local/lib/python3.7/dist-packages (from convertdate>=2.1.2->fbprophet) (0.5.11) Requirement already satisfied: pytz>=2014.10 in /usr/local/lib/python3.7/dist-packages (from convertdate>=2.1.2->fbprophet) (2018.9) Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from holidays>=0.10.2->fbprophet) (1.15.0) Requirement already satisfied: korean-lunar-calendar in /usr/local/lib/python3.7/dist-packages (from holidays>=0.10.2->fbprophet) (0.2.1) Requirement already satisfied: hijri-converter in /usr/local/lib/python3.7/dist-packages (from holidays>=0.10.2->fbprophet) (2.1.3) Requirement already satisfied: ephem>=3.7.5.3 in /usr/local/lib/python3.7/dist-packages (from LunarCalendar>=0.0.9->fbprophet) (4.0.0.2) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->fbprophet) (2.4.7) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->fbprophet) (1.3.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->fbprophet) (0.10.0)
import pandas as pd
from fbprophet import Prophet
tesla_df = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/FACEBOOK PROPHET/Data/dataset.csv")
tesla_df.head()
Date | Store/Product | Value | |
---|---|---|---|
0 | 20180101 | LOS_ANGELES-TESLA_MODEL_X | 2926.000 |
1 | 20180102 | LOS_ANGELES-TESLA_MODEL_X | 2687.531 |
2 | 20180103 | LOS_ANGELES-TESLA_MODEL_X | 2793.000 |
3 | 20180104 | LOS_ANGELES-TESLA_MODEL_X | 2394.000 |
4 | 20180105 | LOS_ANGELES-TESLA_MODEL_X | 2660.000 |
tesla_df.shape
(3240, 3)
Finding the types of data in the csv file
tesla_df.dtypes
Date int64 Store/Product object Value float64 dtype: object
Finding the Value count for feature "Store/Product"
tesla_df.value_counts("Store/Product")
Store/Product SAN_FRANCISCO-TESLA_MODEL_S 1080 LOS_ANGELES-TESLA_MODEL_X 1080 LOS_ANGELES-TESLA_MODEL_S 1080 dtype: int64
duplicates = tesla_df[tesla_df.duplicated(keep='last')]
tesla_df.isna().sum()
Date 0 Store/Product 0 Value 0 dtype: int64
1) There are 3 features in the given data namely "Date", "Store/Product" and "Value".
2) There are 3240 data points in the data set.
3) The date has been provided in the form of integers but it needs to be changed into datetime format as required by prophet.
4) The Store/Product is a categorical feature which has categories namely: SAN_FRANCISCO-TESLA_MODEL_S
LOS_ANGELES-TESLA_MODEL_X
LOS_ANGELES-TESLA_MODEL_S.
5) There are no duplicate and null values in the dataset which means it is a curated toy dataset.
tesla_df['Date'] = pd.to_datetime(tesla_df['Date'],format="%Y%m%d")
tesla_df.head()
Date | Store/Product | Value | |
---|---|---|---|
0 | 2018-01-01 | LOS_ANGELES-TESLA_MODEL_X | 2926.000 |
1 | 2018-01-02 | LOS_ANGELES-TESLA_MODEL_X | 2687.531 |
2 | 2018-01-03 | LOS_ANGELES-TESLA_MODEL_X | 2793.000 |
3 | 2018-01-04 | LOS_ANGELES-TESLA_MODEL_X | 2394.000 |
4 | 2018-01-05 | LOS_ANGELES-TESLA_MODEL_X | 2660.000 |
Creating Data Frame For Each Model and Store
LA_TMX = tesla_df[tesla_df['Store/Product'] == 'LOS_ANGELES-TESLA_MODEL_X'].copy()
LA_TMS = tesla_df[tesla_df['Store/Product'] == 'LOS_ANGELES-TESLA_MODEL_S'].copy()
SF_TMS = tesla_df[tesla_df['Store/Product'] == 'SAN_FRANCISCO-TESLA_MODEL_S'].copy()
LA_TMX.value_counts('Store/Product')
Store/Product LOS_ANGELES-TESLA_MODEL_X 1080 dtype: int64
LA_TMS.value_counts('Store/Product')
Store/Product LOS_ANGELES-TESLA_MODEL_S 1080 dtype: int64
SF_TMS.value_counts('Store/Product')
Store/Product SAN_FRANCISCO-TESLA_MODEL_S 1080 dtype: int64
Removing the Store/Product column and making the columns as ds and y
LA_TMX.drop('Store/Product', axis=1, inplace=True)
LA_TMS.drop('Store/Product', axis=1, inplace=True)
SF_TMS.drop('Store/Product', axis=1, inplace=True)
LA_TMX.columns = ['ds', 'y']
LA_TMS.columns = ['ds', 'y']
SF_TMS.columns = ['ds', 'y']
# Creating instance of prophet class for each model and store
LA_TMX_model = Prophet(interval_width=0.95)
LA_TMS_model = Prophet(interval_width=0.95)
SF_TMS_model = Prophet(interval_width=0.95)
Fitting the model for Each Model and store
training_LA_TMX = LA_TMX_model.fit(LA_TMX)
training_LA_TMS = LA_TMS_model.fit(LA_TMS)
training_SF_TMS = SF_TMS_model.fit(SF_TMS)
INFO:numexpr.utils:NumExpr defaulting to 2 threads. INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this. INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this. INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
future_LA_TMX = LA_TMX_model.make_future_dataframe(periods=400, freq='D')
# periods tell how much far in future you want to predict and frequency tells how oftem do you want to predict, "D" means daily.
forecast = LA_TMX_model.predict(future_LA_TMX)
forecast.head()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-01-01 | 3510.912234 | 871.555187 | 3977.316382 | 3510.912234 | 3510.912234 | -1110.246686 | -1110.246686 | -1110.246686 | 16.628584 | 16.628584 | 16.628584 | -1126.875270 | -1126.875270 | -1126.875270 | 0.0 | 0.0 | 0.0 | 2400.665548 |
1 | 2018-01-02 | 3511.660929 | 710.023657 | 3868.435316 | 3511.660929 | 3511.660929 | -1120.378413 | -1120.378413 | -1120.378413 | -11.274964 | -11.274964 | -11.274964 | -1109.103449 | -1109.103449 | -1109.103449 | 0.0 | 0.0 | 0.0 | 2391.282516 |
2 | 2018-01-03 | 3512.409624 | 652.493080 | 4069.926535 | 3512.409624 | 3512.409624 | -1094.548052 | -1094.548052 | -1094.548052 | -4.403695 | -4.403695 | -4.403695 | -1090.144357 | -1090.144357 | -1090.144357 | 0.0 | 0.0 | 0.0 | 2417.861572 |
3 | 2018-01-04 | 3513.158319 | 876.447695 | 4036.116088 | 3513.158319 | 3513.158319 | -1047.593632 | -1047.593632 | -1047.593632 | 22.480903 | 22.480903 | 22.480903 | -1070.074535 | -1070.074535 | -1070.074535 | 0.0 | 0.0 | 0.0 | 2465.564688 |
4 | 2018-01-05 | 3513.907015 | 853.545053 | 4037.614734 | 3513.907015 | 3513.907015 | -1083.603163 | -1083.603163 | -1083.603163 | -34.579879 | -34.579879 | -34.579879 | -1049.023283 | -1049.023283 | -1049.023283 | 0.0 | 0.0 | 0.0 | 2430.303852 |
forecast.tail()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1475 | 2022-01-16 | 5027.332710 | 2730.714401 | 5885.206624 | 4999.629799 | 5051.395177 | -798.293216 | -798.293216 | -798.293216 | 25.180486 | 25.180486 | 25.180486 | -823.473702 | -823.473702 | -823.473702 | 0.0 | 0.0 | 0.0 | 4229.039494 |
1476 | 2022-01-17 | 5028.404473 | 2741.881293 | 5736.160243 | 5000.584797 | 5052.545774 | -793.963340 | -793.963340 | -793.963340 | 16.628584 | 16.628584 | 16.628584 | -810.591924 | -810.591924 | -810.591924 | 0.0 | 0.0 | 0.0 | 4234.441133 |
1477 | 2022-01-18 | 5029.476236 | 2687.078641 | 5765.247523 | 5001.539795 | 5053.696371 | -811.112189 | -811.112189 | -811.112189 | -11.274964 | -11.274964 | -11.274964 | -799.837224 | -799.837224 | -799.837224 | 0.0 | 0.0 | 0.0 | 4218.364047 |
1478 | 2022-01-19 | 5030.547999 | 2640.739302 | 5825.651223 | 5002.494793 | 5054.846968 | -795.654136 | -795.654136 | -795.654136 | -4.403695 | -4.403695 | -4.403695 | -791.250441 | -791.250441 | -791.250441 | 0.0 | 0.0 | 0.0 | 4234.893863 |
1479 | 2022-01-20 | 5031.619762 | 2730.270540 | 5919.629347 | 5003.449791 | 5055.997565 | -762.333519 | -762.333519 | -762.333519 | 22.480903 | 22.480903 | 22.480903 | -784.814422 | -784.814422 | -784.814422 | 0.0 | 0.0 | 0.0 | 4269.286243 |
yhat is very important column as it represents the forecast
plot1 = LA_TMX_model.plot(forecast)
plot2 = LA_TMX_model.plot_components(forecast)
future_LA_TMS = LA_TMS_model.make_future_dataframe(periods=400, freq='D')
forecast = LA_TMS_model.predict(future_LA_TMS)
forecast.head()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-01-01 | 3886.808831 | 985.365602 | 4323.860788 | 3886.808831 | 3886.808831 | -1220.828565 | -1220.828565 | -1220.828565 | 18.334243 | 18.334243 | 18.334243 | -1239.162808 | -1239.162808 | -1239.162808 | 0.0 | 0.0 | 0.0 | 2665.980266 |
1 | 2018-01-02 | 3887.624832 | 959.858394 | 4370.049837 | 3887.624832 | 3887.624832 | -1231.901600 | -1231.901600 | -1231.901600 | -12.209735 | -12.209735 | -12.209735 | -1219.691865 | -1219.691865 | -1219.691865 | 0.0 | 0.0 | 0.0 | 2655.723232 |
2 | 2018-01-03 | 3888.440833 | 894.873652 | 4556.965475 | 3888.440833 | 3888.440833 | -1203.627413 | -1203.627413 | -1203.627413 | -4.709087 | -4.709087 | -4.709087 | -1198.918325 | -1198.918325 | -1198.918325 | 0.0 | 0.0 | 0.0 | 2684.813420 |
3 | 2018-01-04 | 3889.256834 | 1003.461071 | 4435.016229 | 3889.256834 | 3889.256834 | -1152.290033 | -1152.290033 | -1152.290033 | 24.635374 | 24.635374 | 24.635374 | -1176.925407 | -1176.925407 | -1176.925407 | 0.0 | 0.0 | 0.0 | 2736.966801 |
4 | 2018-01-05 | 3890.072835 | 846.335755 | 4466.650546 | 3890.072835 | 3890.072835 | -1192.196377 | -1192.196377 | -1192.196377 | -38.342005 | -38.342005 | -38.342005 | -1153.854373 | -1153.854373 | -1153.854373 | 0.0 | 0.0 | 0.0 | 2697.876458 |
forecast.tail()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1475 | 2022-01-16 | 5558.750127 | 2796.474724 | 6416.917739 | 5531.409104 | 5588.301101 | -879.244383 | -879.244383 | -879.244383 | 27.300625 | 27.300625 | 27.300625 | -906.545008 | -906.545008 | -906.545008 | 0.0 | 0.0 | 0.0 | 4679.505744 |
1476 | 2022-01-17 | 5559.939195 | 3116.905735 | 6470.441623 | 5532.485572 | 5589.648718 | -874.083684 | -874.083684 | -874.083684 | 18.334243 | 18.334243 | 18.334243 | -892.417927 | -892.417927 | -892.417927 | 0.0 | 0.0 | 0.0 | 4685.855510 |
1477 | 2022-01-18 | 5561.128262 | 2968.390101 | 6417.442318 | 5533.562040 | 5590.996335 | -892.833864 | -892.833864 | -892.833864 | -12.209735 | -12.209735 | -12.209735 | -880.624129 | -880.624129 | -880.624129 | 0.0 | 0.0 | 0.0 | 4668.294398 |
1478 | 2022-01-19 | 5562.317329 | 2867.124933 | 6430.258132 | 5534.642035 | 5592.258633 | -875.917545 | -875.917545 | -875.917545 | -4.709087 | -4.709087 | -4.709087 | -871.208458 | -871.208458 | -871.208458 | 0.0 | 0.0 | 0.0 | 4686.399784 |
1479 | 2022-01-20 | 5563.506396 | 3044.191526 | 6447.832735 | 5535.763060 | 5593.503723 | -839.516675 | -839.516675 | -839.516675 | 24.635374 | 24.635374 | 24.635374 | -864.152049 | -864.152049 | -864.152049 | 0.0 | 0.0 | 0.0 | 4723.989721 |
plot1 = LA_TMS_model.plot(forecast)
plot2 = LA_TMS_model.plot_components(forecast)
future_SF_TMS = SF_TMS_model.make_future_dataframe(periods=400, freq='D')
forecast = SF_TMS_model.predict(future_LA_TMX)
forecast.head()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-01-01 | 3230.730465 | 808.152938 | 3721.846350 | 3230.730465 | 3230.730465 | -1001.341709 | -1001.341709 | -1001.341709 | 14.749387 | 14.749387 | 14.749387 | -1016.091096 | -1016.091096 | -1016.091096 | 0.0 | 0.0 | 0.0 | 2229.388756 |
1 | 2018-01-02 | 3231.232777 | 803.406999 | 3708.645891 | 3231.232777 | 3231.232777 | -1010.751576 | -1010.751576 | -1010.751576 | -10.262399 | -10.262399 | -10.262399 | -1000.489177 | -1000.489177 | -1000.489177 | 0.0 | 0.0 | 0.0 | 2220.481201 |
2 | 2018-01-03 | 3231.735089 | 819.895322 | 3728.471814 | 3231.735089 | 3231.735089 | -987.843564 | -987.843564 | -987.843564 | -4.028393 | -4.028393 | -4.028393 | -983.815171 | -983.815171 | -983.815171 | 0.0 | 0.0 | 0.0 | 2243.891525 |
3 | 2018-01-04 | 3232.237400 | 905.752707 | 3787.284977 | 3232.237400 | 3232.237400 | -945.815788 | -945.815788 | -945.815788 | 20.317623 | 20.317623 | 20.317623 | -966.133411 | -966.133411 | -966.133411 | 0.0 | 0.0 | 0.0 | 2286.421612 |
4 | 2018-01-05 | 3232.739712 | 917.200128 | 3819.344538 | 3232.739712 | 3232.739712 | -978.677344 | -978.677344 | -978.677344 | -31.121464 | -31.121464 | -31.121464 | -947.555879 | -947.555879 | -947.555879 | 0.0 | 0.0 | 0.0 | 2254.062368 |
forecast.tail()
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1475 | 2022-01-16 | 4580.380290 | 2447.653528 | 5233.354638 | 4536.966246 | 4616.753699 | -724.087885 | -724.087885 | -724.087885 | 22.830304 | 22.830304 | 22.830304 | -746.918188 | -746.918188 | -746.918188 | 0.0 | 0.0 | 0.0 | 3856.292405 |
1476 | 2022-01-17 | 4581.357818 | 2410.108540 | 5339.879389 | 4537.823576 | 4617.996833 | -720.602434 | -720.602434 | -720.602434 | 14.749387 | 14.749387 | 14.749387 | -735.351821 | -735.351821 | -735.351821 | 0.0 | 0.0 | 0.0 | 3860.755385 |
1477 | 2022-01-18 | 4582.335346 | 2397.521006 | 5270.073176 | 4538.680907 | 4619.239967 | -735.933925 | -735.933925 | -735.933925 | -10.262399 | -10.262399 | -10.262399 | -725.671526 | -725.671526 | -725.671526 | 0.0 | 0.0 | 0.0 | 3846.401422 |
1478 | 2022-01-19 | 4583.312874 | 2304.736358 | 5201.904956 | 4539.538238 | 4620.483100 | -721.944814 | -721.944814 | -721.944814 | -4.028393 | -4.028393 | -4.028393 | -717.916421 | -717.916421 | -717.916421 | 0.0 | 0.0 | 0.0 | 3861.368061 |
1479 | 2022-01-20 | 4584.290402 | 2592.216382 | 5340.320143 | 4540.395568 | 4621.726234 | -691.756144 | -691.756144 | -691.756144 | 20.317623 | 20.317623 | 20.317623 | -712.073767 | -712.073767 | -712.073767 | 0.0 | 0.0 | 0.0 | 3892.534258 |
plot1 = SF_TMS_model.plot(forecast)
plot2 = SF_TMS_model.plot_components(forecast)