In [1]:
import numpy as np
import pandas as pd

import seaborn as sns; sns.set()
import matplotlib.pyplot as plt

#import plotly
import fbprophet
from fbprophet import Prophet
In [2]:
# Read data from excel file

xlsx = pd.ExcelFile('VentaH.xlsx')
farma_csv = pd.read_excel(xlsx,  'datos')
In [3]:
farma = farma_csv

#Convert string to datetime data type
farma['Mes'] = pd.DatetimeIndex(farma['Mes'])
In [4]:
#Filters

#Select product AAA1 only
farmax = farma[farma['Material'].isin(['AAA1'])]

#Remove these two Clients
farmax = farmax[~farmax['Cliente'].isin(['Client11','Client12'] )]
In [5]:
#Sum and group by date
farmaxsum = farmax.groupby(['Fecha']).sum().reset_index()
In [6]:
#See statistics

farmaxsum.describe()
Out[6]:
unidades Importe Período Año
count 48.000000 4.800000e+01 48.000000 48.000000
mean 12960.312500 2.121520e+08 839.383006 250496.729167
std 2400.889943 3.779284e+07 529.174497 74544.073758
min 8550.000000 1.274028e+08 121.361600 171360.000000
25% 11513.750000 1.854606e+08 423.329075 203112.000000
50% 12371.500000 2.088018e+08 804.278850 212793.000000
75% 14759.250000 2.440701e+08 1128.486375 323931.000000
max 17881.000000 2.970063e+08 2061.149600 395724.000000
In [7]:
#Rename columns as required by Prophet
farmaxsum = farmaxsum.rename(columns={'Fecha': 'ds', 'unidades':'y'})
In [8]:
plt.figure(figsize=(20,5))
#plt.axvline(x=1000)
sns.lineplot(x="ds", y="y",   estimator = 'mean' , data=farmaxsum)
#sns.lineplot(x=1)
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x230fd01f788>
In [9]:
import fbprophet
from fbprophet import Prophet

my_model = Prophet()
my_model.fit(farmaxsum)

future = my_model.make_future_dataframe(periods=24, freq='m')

m = Prophet(n_changepoints=10)
forecast = m.fit(farmaxsum).predict(future)
fig = m.plot(forecast)
INFO:numexpr.utils:NumExpr defaulting to 8 threads.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [10]:
#Plot Prophet's Forecast

from fbprophet.plot import plot
plot(my_model, forecast, figsize=(19, 7))
Out[10]:
In [11]:
#Plot time serie's components

fig = my_model.plot_components(forecast)
In [12]:
#Exportar resultado a un archivo plano

forecast.to_csv('fcst.csv',  decimal=',' , sep='\t', encoding='utf-8')