prophet icon indicating copy to clipboard operation
prophet copied to clipboard

Error with cross_validation

Open denizn opened this issue 8 years ago • 1 comments

I have received below error message when trying to use below code. Can we disable error message "regressor constant" while using cross_validation at least? I think the error message does not make sense in general, because a regressor can be zero for the training period but not be constant for the testing period.

from fbprophet.diagnostics import cross_validation from fbprophet.diagnostics import performance_metrics

df_cv = cross_validation(model, initial=(str(len(df)-75)+' days'), period='56 days', horizon = '28 days') df_p = performance_metrics(df_cv) df_p


ValueError Traceback (most recent call last) in () 2 from fbprophet.diagnostics import performance_metrics 3 ----> 4 df_cv = cross_validation(model, initial=(str(len(df)-75)+' days'), period='56 days', horizon = '28 days') 5 df_p = performance_metrics(df_cv) 6 df_p

~\AppData\Local\Continuum\anaconda3\envs\data\lib\site-packages\fbprophet-0.2.1-py3.5.egg\fbprophet\diagnostics.py in cross_validation(model, horizon, period, initial) 147 raise ValueError( 148 'Not enough data for specified horizon, period, and initial.') --> 149 return simulated_historical_forecasts(model, horizon, k, period) 150 151

~\AppData\Local\Continuum\anaconda3\envs\data\lib\site-packages\fbprophet-0.2.1-py3.5.egg\fbprophet\diagnostics.py in simulated_historical_forecasts(model, horizon, k, period) 93 m = prophet_copy(model, cutoff) 94 # Train model ---> 95 m.fit(df[df['ds'] <= cutoff]) 96 # Calculate yhat 97 index_predicted = (df['ds'] > cutoff) & (df['ds'] <= cutoff + horizon)

~\AppData\Local\Continuum\anaconda3\envs\data\lib\site-packages\fbprophet-0.2.1-py3.5.egg\fbprophet\forecaster.py in fit(self, df, **kwargs) 933 self.history_dates = pd.to_datetime(df['ds']).sort_values() 934 --> 935 history = self.setup_dataframe(history, initialize_scales=True) 936 self.history = history 937 self.set_auto_seasonalities()

~\AppData\Local\Continuum\anaconda3\envs\data\lib\site-packages\fbprophet-0.2.1-py3.5.egg\fbprophet\forecaster.py in setup_dataframe(self, df, initialize_scales) 260 df.reset_index(inplace=True, drop=True) 261 --> 262 self.initialize_scales(initialize_scales, df) 263 264 if self.logistic_floor:

~\AppData\Local\Continuum\anaconda3\envs\data\lib\site-packages\fbprophet-0.2.1-py3.5.egg\fbprophet\forecaster.py in initialize_scales(self, initialize_scales, df) 308 n_vals = len(df[name].unique()) 309 if n_vals < 2: --> 310 raise ValueError('Regressor {} is constant.'.format(name)) 311 if standardize == 'auto': 312 if set(df[name].unique()) == set([1, 0]):

ValueError: Regressor Christmas Day is constant.

denizn avatar May 30 '18 00:05 denizn

There's a discussion of this issue in #359. The short version is that if the regressor is an important part of the model, then the estimates of the model's predictive power that we're trying to get with cross validation should include the regressor. I added a paragraph to the end of that documentation page to explain this (https://github.com/facebook/prophet/blob/v0.3/notebooks/diagnostics.ipynb).

The same principle is true for seasonalities, although it is not enforced for seasonalities like it is for extra regressors.

Anyway, we can discuss this further in #359, I'm going to close it here though to keep the discussion in that one place.

bletham avatar May 30 '18 22:05 bletham