Sitemap
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Follow publication

Website visitor forecast with Facebook Prophet: A Complete Tutorial

8 min readMar 10, 2022

--

Photo by Luc van Loon on Unsplash

4. Forecasting with Modal Assessment

* MAE (Mean absolute error)

* MSE (Mean Squared Error)

* RMSE (Root Mean Squared Error)

* R-squared (Coefficient of determination)

def evaluate(y_true, y_pred):dict = {‘MAE’: mean_absolute_error(y_true, y_pred),‘MSE’: mean_squared_error(y_true, y_pred),‘RMSE’: sqrt(mean_squared_error(y_true, y_pred)),‘R2’: r2_score(y_true, y_pred)}return dict
test_len_assess = 8train_df_assess = train_test_df_assess.iloc[:-test_len_assess, :]test_df_assess = train_test_df_assess.iloc[int(-test_len_assess):, :]
m = Prophet(interval_width=0.95, weekly_seasonality=False, daily_seasonality=False)m.add_country_holidays(country_name=’DE’)
m.fit(train_df_assess)
predict_df_assess = m.predict(test_df_assess)evaluate(test_df_assess[‘y’], predict_df_assess[‘yhat’])
The R² for this default Facebook Prophet model is 0.6 only.
The R² is 0.6 only. Not impressive at all. Image by Author
plot_measure = m.plot(predict_df_assess)
The blue part is the test part of this untuned Facebook Prophet model.
The blue part is the test part of this untuned Facebook Prophet model. Image by Author
future_assess_input = m.make_future_dataframe(periods=60,freq=’W’)future_assess_output = m.predict(future_assess_input)plot_assess = m.plot(future_assess_output)
Image by Author

5. Optimizing with Optuna

test_len_tun = int(train_test_df_tun.shape[0] / 10) # take only 1/10 as the test sizetrain_df_tun = train_test_df_tun.iloc[:-test_len_tun, :]val_df_tun = train_test_df_tun.iloc[-test_len_tun:int(-test_len_tun/2), :] # within the test pool, the first half is taken for validationtest_df_tun = train_test_df_tun.iloc[int(-test_len_tun/2):, :] # only the final half of the test pool is for the test
def find_params(trial):parameters = {‘changepoint_prior_scale’: trial.suggest_float(‘changepoint_prior_scale’, 0.005, 5),‘changepoint_range’: trial.suggest_float(‘changepoint_range’, 0.1, 0.9),‘seasonality_mode’: trial.suggest_categorical(‘seasonality_mode’, [‘multiplicative’, ‘additive’]),‘seasonality_prior_scale’: trial.suggest_float(‘seasonality_prior_scale’, 0.1, 10),‘yearly_seasonality’: trial.suggest_int(‘yearly_seasonality’, 1, 50),‘holidays_prior_scale’: trial.suggest_float(‘holidays_prior_scale’, 0.1, 10)}m = Prophet(**parameters, # ** means unpackinterval_width=0.95,weekly_seasonality=False,daily_seasonality=False)m.add_country_holidays(country_name=’DE’)m.fit(train_df_tun)validation = m.predict(val_df_tun)mae_for_optuna = mean_absolute_error(val_df_tun[‘y’], validation[‘yhat’])return mae_for_optuna
study = optuna.create_study(direction=’minimize’)study.optimize(find_params, n_trials=1000) #1000study.best_params
After 2–3 hours of waiting time, the search is done, best parameters are found.
After 2–3 hours of waiting time, the search is done, best parameters are found. Image by Author
para = {‘changepoint_prior_scale’: 1.9804273036896098,‘changepoint_range’: 0.6543491388579227,‘seasonality_mode’: ‘multiplicative’,‘seasonality_prior_scale’: 4.465868155817663,‘yearly_seasonality’: 18,‘holidays_prior_scale’: 2.650571507054187}
train_df_tun2 = pd.concat([train_df_tun, val_df_tun])m = Prophet(**para,interval_width=0.95,weekly_seasonality=False,daily_seasonality=False)m.add_country_holidays(country_name=’DE’)m.fit(train_df_tun2)# Then we test our newly trained model with the test df.predict_df_tun = m.predict(test_df_tun)evaluate(test_df_tun[‘y’], predict_df_tun[‘yhat’])
The blue part is the test part of this tuned Facebook Prophet model. Image by Author
future_optuna_df = m.make_future_dataframe(periods=60,freq=’W’)predict_optuna2 = m.predict(future_optuna_df)predict_optuna2.columnsforecast_final = predict_optuna2[[‘ds’, ‘trend’,’yhat’]]forecast_fig_final = px.line(forecast_final, x=”ds”, y=”yhat”, title=’The number of unique visitors of www.lorentzyeung.com forecast one year into the future.’)fig_final.show()
Congratulations, your Prophet with the best params is now done.
Congratulations, your Prophet with the best params is now done. Image by Author

Conclusion

If you are curious or want to find out what I have done in Part 1 of the article, please click here to part 1.

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Lorentz Yeung
Lorentz Yeung

Written by Lorentz Yeung

Data Analyst in Microsoft, Founder of El Arte Design and Marketing, Certified Digital Marketer, MSc in Digital Marketing, London based.

Responses (1)