Prompt Title: Prophet hyperparameter tuning

Created 1 year ago
Votes 0
Views 122 times
0

Prompt Detail:

The fbprophet library for time series forecasting is not a scikit-learn estimator, so you cannot use GridSearchCV with fbprophet directly. However, you can use other methods for hyperparameter tuning in fbprophet.

fbprophet has several hyperparameters that can be tuned, including the trend changepoint prior scale, the seasonality prior scale, the seasonal Fourier order, and the regularization parameters for the linear and nonlinear components. One way to tune these hyperparameters is by using a grid search or random search.

Here's an example of how to use a grid search with fbprophet:

python
from fbprophet import Prophet from sklearn.model_selection import ParameterGrid import pandas as pd # Load data df = pd.read_csv('data.csv') # Define parameter grid param_grid = { 'changepoint_prior_scale': [0.01, 0.1, 1.0], 'seasonality_prior_scale': [0.01, 0.1, 1.0], 'seasonal_order': [(3, 1, 0), (4, 1, 0), (5, 1, 0)], 'yearly_fourier_order': [3, 5, 7], 'weekly_fourier_order': [3, 5, 7], 'daily_fourier_order': [3, 5, 7], 'reg_lambda': [0.0, 0.1, 1.0], 'reg_alpha': [0.0, 0.1, 1.0], } # Define evaluation metric def evaluate_model(y_true, y_pred): # Define evaluation metric return mean_squared_error(y_true, y_pred) # Iterate over parameter grid results = [] for params in ParameterGrid(param_grid): # Instantiate Prophet object with given parameters m = Prophet(changepoint_prior_scale=params['changepoint_prior_scale'], seasonality_prior_scale=params['seasonality_prior_scale'], seasonality_mode='additive', yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=True, seasonality_order=params['seasonal_order'], yearly_fourier_order=params['yearly_fourier_order'], weekly_fourier_order=params['weekly_fourier_order'], daily_fourier_order=params['daily_fourier_order'], reg_lambda=params['reg_lambda'], reg_alpha=params['reg_alpha']) # Fit model m.fit(df) # Make predictions future = m.make_future_dataframe(periods=365) forecast = m.predict(future) # Evaluate model y_true = df['y'].values y_pred = forecast['yhat'].values[-365:] score = evaluate_model(y_true, y_pred) # Add results to list results.append({'params': params, 'score': score}) # Print best hyperparameters best_params = sorted(results, key=lambda x: x['score'])[0]['params'] print(best_params)

In this example, we define a parameter grid for fbprophet hyperparameters, including the changepoint prior scale, the seasonality prior scale, the seasonal Fourier order, and the regularization parameters. We then define an evaluation metric (evaluate_model) and iterate over the parameter grid, instantiating a Prophet object with the given parameters, fitting the model on the data, making predictions, and evaluating the model using the evaluation metric. We add the results to a list and print the best hyperparameters.

Leave a Comment