0% found this document useful (0 votes)
214 views

FB Prophet Python

The document cleans and prepares sales data for BMW and Audi cars, builds Prophet forecasting models on the data, generates forecasts 36 months into the future, and merges the forecasts to compare predicted sales between the two companies on the same plot. Key steps include dropping columns, sorting by date, aggregating by month, fitting Prophet models, generating and predicting forecasts, and merging/plotting the results.

Uploaded by

sherman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

FB Prophet Python

The document cleans and prepares sales data for BMW and Audi cars, builds Prophet forecasting models on the data, generates forecasts 36 months into the future, and merges the forecasts to compare predicted sales between the two companies on the same plot. Key steps include dropping columns, sorting by date, aggregating by month, fitting Prophet models, generating and predicting forecasts, and merging/plotting the results.

Uploaded by

sherman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Use Top 10

cols = ['Make','Model','Year','Referer','Timestamp']
BMW.drop(cols, axis=1, inplace=True)
Audi.drop(cols, axis=1, inplace=True)
BMW = BMW.sort_values('Created')
Audi = Audi.sort_values('Created')
BMW = BMW.groupby('Created')['Price'].sum().reset_index()
Audi = Audi.groupby('Created')['Price'].sum().reset_index()
BMW = BMW.set_index('Created')
Audi = Audi.set_index('Created')
y_BMW = BMW['Price'].resample('MS').mean()
y_Audi = Audi['Price'].resample('MS').mean()
BMW = pd.DataFrame({'Created':y_BMW.index, 'Price':y_BMW.values})
Audi = pd.DataFrame({'Created': y_Audi.index, 'Price': y_Audi.values})
store = BMW.merge(Audi, how='inner', on='Created')
store.rename(columns={'Price_x': 'BMW_Price', 'Price_y': 'Audi_Price'}, inplace=True)
store.head()

from fbprophet import Prophet


BMW = BMW.rename(columns={'Created': 'ds', 'Prices': 'y'})
BMW_model = Prophet(interval_width=0.95)
BMW_model.fit(BMW)
Audi = Audi.rename(columns={'Created': 'ds', 'Prices': 'y'})
Audi_model = Prophet(interval_width=0.95)
Audi_model.fit(Audi)
BMW_forecast = BMW_model.make_future_dataframe(periods=36, freq='MS')
BMW_forecast = BMW_model.predict(BMW_forecast)
Audi_forecast = Audi_model.make_future_dataframe(periods=36, freq='MS')
Audi_forecast = Audi_model.predict(Audi_forecast)
plt.figure(figsize=(18, 6))
BMW_model.plot(BMW_forecast, xlabel = 'Created', ylabel = 'Prices')
plt.title('BMW Prices');

BMW_names = ['BMW_%s' % column for column in BMW_forecast.columns]


Audi_names = ['Audi_%s' % column for column in Audi_forecast.columns]
merge_BMW_forecast = BMW_forecast.copy()
merge_Audi_forecast = Audi_forecast.copy()
merge_BMW_forecast.columns = BMW_names
merge_Audi_forecast.columns = Audi_names
forecast = pd.merge(merge_BMW_forecast, merge_Audi_forecast, how = 'inner', left_on =
'BMW_ds', right_on = 'Audi_ds')
forecast = forecast.rename(columns={'BMW_ds': 'Created'}).drop('Audi_ds', axis=1)
forecast.head()

plt.figure(figsize=(10, 7))
plt.plot(forecast['Created'], forecast['BMW_yhat'], 'b-')
plt.plot(forecast['Created'], forecast['Audi_yhat'], 'r-')
plt.legend(); plt.xlabel('Created'); plt.ylabel('Sales')
plt.title('BMW vs. Audi Supplies Estimate');

You might also like