superbowls = pd.DataFrame({ 'holiday': 'superbowl', 'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']), 'lower_window': 0, 'upper_window': 1, }) holidays = pd.concat((playoffs, superbowls)) m = Prophet(holidays=holidays) m.fit(df) future = m.make_future_dataframe(periods=365) forecast = m.predict(future) #print(forecast) # 通过 forecast 数据框,展示节假日效应 forecast[(forecast['playoff'] + forecast['superbowl']).abs() > 0][[ 'ds', 'playoff', 'superbowl' ]][-10:] # 可以使用 plot_forecast_component(从fbprophet.plot导入)来画出独立的节假日的成分 from fbprophet.plot import plot_forecast_component m.plot_forecast_component(forecast, 'superbowl') # In[ ]: # 预测的成分分析绘图,展示预测中的趋势、周效应和年度效应 model.plot_components(forecast) print(forecast.columns)
def test_holiday(): playoffs = pd.DataFrame({ 'holiday': 'playoff', 'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16', '2010-01-24', '2010-02-07', '2011-01-08', '2013-01-12', '2014-01-12', '2014-01-19', '2014-02-02', '2015-01-11', '2016-01-17', '2016-01-24', '2016-02-07']), 'lower_window': 0, 'upper_window': 1, }) superbowls = pd.DataFrame({ 'holiday': 'superbowl', 'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']), 'lower_window': 0, 'upper_window': 1, }) holidays = pd.concat((playoffs, superbowls)) df = pd.read_csv(prophet_path + os.sep + 'example_wp_log_peyton_manning.csv') m = Prophet(holidays=holidays) m.fit(df) future = m.make_future_dataframe(periods=30) forecast = m.predict(future) # forecast[(forecast['playoff'] + forecast['superbowl']).abs() > 0][['ds', 'playoff', 'superbowl']][-10:] fig = m.plot_components(forecast) m.plot(forecast) m.plot_forecast_component(forecast, 'superbowl') # Prophet uses a Fourier order of 3 for weekly seasonality and 10 for yearly seasonality. m.plot_yearly() # 越高越多变 # N Fourier terms corresponds to 2N variables used for modeling the cycle m = Prophet(yearly_seasonality=20).fit(df) a = m.plot_yearly() # 添加月的周期性 m = Prophet(weekly_seasonality=False) m.add_seasonality(name='monthly', period=30.5, fourier_order=5) forecast = m.fit(df).predict(future) fig = m.plot_components(forecast) # holidays_prior_scale. By default this parameter is 10, holidays are overfitting? m = Prophet(holidays=holidays, holidays_prior_scale=0.05).fit(df) forecast = m.predict(future) # forecast[(forecast['playoff'] + forecast['superbowl']).abs() > 0][['ds', 'playoff', 'superbowl']][-10:] # seasonality_prior_scale m = Prophet() m.add_seasonality(name='weekly', period=7, fourier_order=3, prior_scale=0.1) # Add some func def nfl_sunday(ds): date = pd.to_datetime(ds) if date.weekday() == 6 and (date.month > 8 or date.month < 2): return 1 else: return 0 df['nfl_sunday'] = df['ds'].apply(nfl_sunday) m = Prophet() m.add_regressor('nfl_sunday') m.fit(df) future['nfl_sunday'] = future['ds'].apply(nfl_sunday) forecast = m.predict(future) fig = m.plot_components(forecast)