def test_seasonality_modes(self): # Model with holidays, seasonalities, and extra regressors holidays = pd.DataFrame({ 'ds': pd.to_datetime(['2016-12-25']), 'holiday': ['xmas'], 'lower_window': [-1], 'upper_window': [0], }) m = Prophet(seasonality_mode='multiplicative', holidays=holidays) m.add_seasonality('monthly', period=30, mode='additive', fourier_order=3) m.add_regressor('binary_feature', mode='additive') m.add_regressor('numeric_feature') # Construct seasonal features df = DATA.copy() df['binary_feature'] = [0] * 255 + [1] * 255 df['numeric_feature'] = range(510) df = m.setup_dataframe(df, initialize_scales=True) m.history = df.copy() m.set_auto_seasonalities() seasonal_features, prior_scales, component_cols, modes = ( m.make_all_seasonality_features(df)) self.assertEqual(sum(component_cols['additive_terms']), 7) self.assertEqual(sum(component_cols['multiplicative_terms']), 29) self.assertEqual( set(modes['additive']), {'monthly', 'binary_feature', 'additive_terms', 'extra_regressors_additive'}, ) self.assertEqual( set(modes['multiplicative']), {'weekly', 'yearly', 'xmas', 'numeric_feature', 'multiplicative_terms', 'extra_regressors_multiplicative', 'holidays', }, )
def test_seasonality_modes(self): # Model with holidays, seasonalities, and extra regressors holidays = pd.DataFrame({ 'ds': pd.to_datetime(['2016-12-25']), 'holiday': ['xmas'], 'lower_window': [-1], 'upper_window': [0], }) m = Prophet(seasonality_mode='multiplicative', holidays=holidays) m.add_seasonality('monthly', period=30, mode='additive', fourier_order=3) m.add_regressor('binary_feature', mode='additive') m.add_regressor('numeric_feature') # Construct seasonal features df = DATA.copy() df['binary_feature'] = [0] * 255 + [1] * 255 df['numeric_feature'] = range(510) df = m.setup_dataframe(df, initialize_scales=True) m.history = df.copy() m.set_auto_seasonalities() seasonal_features, prior_scales, component_cols, modes = ( m.make_all_seasonality_features(df)) self.assertEqual(sum(component_cols['additive_terms']), 7) self.assertEqual(sum(component_cols['multiplicative_terms']), 29) self.assertEqual( set(modes['additive']), {'monthly', 'binary_feature', 'additive_terms', 'extra_regressors_additive'}, ) self.assertEqual( set(modes['multiplicative']), {'weekly', 'yearly', 'xmas', 'numeric_feature', 'multiplicative_terms', 'extra_regressors_multiplicative', 'holidays', }, )
def main(): print("START") open_db() origin = raw_input("Type origin: ") destination = raw_input("Type destination: ") start = str(raw_input("Type start year_month (MM/YY): ")) end = str(raw_input("Type end year_month (optional): ")) company = raw_input("Type company (optional): ") start_date = "20" + start[3:6] + "-" + start[0:2] now = utcnow() if end: end_date = "20" + end[3:6] + "-" + end[0:2] else: end_date = str(now.year + 3) + "-" + str(now.month).zfill(2) # periods: number of months between now and end_date if int(end_date[0:4]) == now.year: periods = (datetime.strptime(end_date + '-01', '%Y-%m-%d').month - now.month) else: periods = (datetime.strptime(end_date + '-01', '%Y-%m-%d').year - now.year) * 12 capa = get_capa(origin, destination, company, end_date) capa['ds'] = pd.to_datetime(capa['year_month'] + "-01") capa = capa.groupby(capa.ds).sum() capa['ds'] = capa.index capa = capa[['ds', 'capacity']] capa.columns = ['ds', 'cap'] segment = get_segment(origin, destination, company, end_date) segment['ds'] = pd.to_datetime(segment['year_month'] + "-01") segment = segment.groupby(segment.ds).sum() segment['ds'] = segment.index segment = segment[['ds', 'passengers']] segment.columns = ['ds', 'y'] # Train the model, then prepare dates to be predicted model = Prophet(mcmc_samples=200) model.fit(segment) future = model.make_future_dataframe(periods=periods, freq='MS') # Add capacities to the prediction future = future.merge(capa, 'left', on='ds') forecast = model.predict(future) # restrict data to after the start_date forecast = forecast.loc[forecast['ds'] > (start_date + "-01")] model.history = model.history.loc[model.history['ds'] > (start_date + "-01")] # Since there is no weekly data, remove the weekly columns to avoid getting an empty plot forecast = forecast[[ 'ds', 'cap', 't', 'trend', 'seasonal_lower', 'seasonal_upper', 'trend_lower', 'trend_upper', 'yhat_lower', 'yhat_upper', 'yearly', 'yearly_lower', 'yearly_upper', 'seasonal', 'yhat' ]] model.plot(forecast).show() # The capacity doesn't look nice on the trend plot, so remove it before this plot del forecast['cap'] model.plot_components(forecast).show()
def test_override_n_changepoints(self): m = Prophet() history = DATA.head(20).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() self.assertEqual(m.n_changepoints, 15) cp = m.changepoints_t self.assertEqual(cp.shape[0], 15)
def test_override_n_changepoints(self): m = Prophet() history = DATA.head(20).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() self.assertEqual(m.n_changepoints, 15) cp = m.changepoints_t self.assertEqual(cp.shape[0], 15)
def test_get_zero_changepoints(self): m = Prophet(n_changepoints=0) N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], 1) self.assertEqual(cp[0], 0)
def test_get_zero_changepoints(self): m = Prophet(n_changepoints=0) N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], 1) self.assertEqual(cp[0], 0)
def test_get_changepoints(self): m = Prophet() N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], m.n_changepoints) self.assertEqual(len(cp.shape), 1) self.assertTrue(cp.min() > 0) cp_indx = int(np.ceil(0.8 * history.shape[0])) self.assertTrue(cp.max() <= history['t'].values[cp_indx])
def test_get_changepoints(self): m = Prophet() N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], m.n_changepoints) self.assertEqual(len(cp.shape), 1) self.assertTrue(cp.min() > 0) cp_indx = int(np.ceil(0.8 * history.shape[0])) self.assertTrue(cp.max() <= history['t'].values[cp_indx])
def test_get_changepoints(self): m = Prophet() N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], m.n_changepoints) self.assertEqual(len(cp.shape), 1) self.assertTrue(cp.min() > 0) self.assertTrue(cp.max() < N) mat = m.get_changepoint_matrix() self.assertEqual(mat.shape[0], N // 2) self.assertEqual(mat.shape[1], m.n_changepoints)
def test_get_changepoints(self): m = Prophet() N = DATA.shape[0] history = DATA.head(N // 2).copy() history = m.setup_dataframe(history, initialize_scales=True) m.history = history m.set_changepoints() cp = m.changepoints_t self.assertEqual(cp.shape[0], m.n_changepoints) self.assertEqual(len(cp.shape), 1) self.assertTrue(cp.min() > 0) self.assertTrue(cp.max() < N) mat = m.get_changepoint_matrix() self.assertEqual(mat.shape[0], N // 2) self.assertEqual(mat.shape[1], m.n_changepoints)
def test_copy(self): df = DATA_all.copy() df['cap'] = 200. df['binary_feature'] = [0] * 255 + [1] * 255 # These values are created except for its default values holiday = pd.DataFrame({ 'ds': pd.to_datetime(['2016-12-25']), 'holiday': ['x'] }) append_holidays = 'US' products = itertools.product( ['linear', 'logistic'], # growth [None, pd.to_datetime(['2016-12-25'])], # changepoints [3], # n_changepoints [0.9], # changepoint_range [True, False], # yearly_seasonality [True, False], # weekly_seasonality [True, False], # daily_seasonality [None, holiday], # holidays [None, append_holidays], # append_holidays ['additive', 'multiplicative'], # seasonality_mode [1.1], # seasonality_prior_scale [1.1], # holidays_prior_scale [0.1], # changepoint_prior_scale [100], # mcmc_samples [0.9], # interval_width [200] # uncertainty_samples ) # Values should be copied correctly for product in products: m1 = Prophet(*product) m1.history = m1.setup_dataframe(df.copy(), initialize_scales=True) m1.set_auto_seasonalities() m2 = diagnostics.prophet_copy(m1) self.assertEqual(m1.growth, m2.growth) self.assertEqual(m1.n_changepoints, m2.n_changepoints) self.assertEqual(m1.changepoint_range, m2.changepoint_range) self.assertEqual(m1.changepoints, m2.changepoints) self.assertEqual(False, m2.yearly_seasonality) self.assertEqual(False, m2.weekly_seasonality) self.assertEqual(False, m2.daily_seasonality) self.assertEqual(m1.yearly_seasonality, 'yearly' in m2.seasonalities) self.assertEqual(m1.weekly_seasonality, 'weekly' in m2.seasonalities) self.assertEqual(m1.daily_seasonality, 'daily' in m2.seasonalities) if m1.holidays is None: self.assertEqual(m1.holidays, m2.holidays) else: self.assertTrue((m1.holidays == m2.holidays).values.all()) self.assertEqual(m1.append_holidays, m2.append_holidays) self.assertEqual(m1.seasonality_mode, m2.seasonality_mode) self.assertEqual(m1.seasonality_prior_scale, m2.seasonality_prior_scale) self.assertEqual(m1.changepoint_prior_scale, m2.changepoint_prior_scale) self.assertEqual(m1.holidays_prior_scale, m2.holidays_prior_scale) self.assertEqual(m1.mcmc_samples, m2.mcmc_samples) self.assertEqual(m1.interval_width, m2.interval_width) self.assertEqual(m1.uncertainty_samples, m2.uncertainty_samples) # Check for cutoff and custom seasonality and extra regressors changepoints = pd.date_range('2012-06-15', '2012-09-15') cutoff = pd.Timestamp('2012-07-25') m1 = Prophet(changepoints=changepoints) m1.add_seasonality('custom', 10, 5) m1.add_regressor('binary_feature') m1.fit(df) m2 = diagnostics.prophet_copy(m1, cutoff=cutoff) changepoints = changepoints[changepoints <= cutoff] self.assertTrue((changepoints == m2.changepoints).all()) self.assertTrue('custom' in m2.seasonalities) self.assertTrue('binary_feature' in m2.extra_regressors)
def test_copy(self): df = DATA_all.copy() df['cap'] = 200. df['binary_feature'] = [0] * 255 + [1] * 255 # These values are created except for its default values holiday = pd.DataFrame( {'ds': pd.to_datetime(['2016-12-25']), 'holiday': ['x']}) products = itertools.product( ['linear', 'logistic'], # growth [None, pd.to_datetime(['2016-12-25'])], # changepoints [3], # n_changepoints [0.9], # changepoint_range [True, False], # yearly_seasonality [True, False], # weekly_seasonality [True, False], # daily_seasonality [None, holiday], # holidays ['additive', 'multiplicative'], # seasonality_mode [1.1], # seasonality_prior_scale [1.1], # holidays_prior_scale [0.1], # changepoint_prior_scale [100], # mcmc_samples [0.9], # interval_width [200] # uncertainty_samples ) # Values should be copied correctly for product in products: m1 = Prophet(*product) m1.history = m1.setup_dataframe( df.copy(), initialize_scales=True) m1.set_auto_seasonalities() m2 = diagnostics.prophet_copy(m1) self.assertEqual(m1.growth, m2.growth) self.assertEqual(m1.n_changepoints, m2.n_changepoints) self.assertEqual(m1.changepoint_range, m2.changepoint_range) self.assertEqual(m1.changepoints, m2.changepoints) self.assertEqual(False, m2.yearly_seasonality) self.assertEqual(False, m2.weekly_seasonality) self.assertEqual(False, m2.daily_seasonality) self.assertEqual( m1.yearly_seasonality, 'yearly' in m2.seasonalities) self.assertEqual( m1.weekly_seasonality, 'weekly' in m2.seasonalities) self.assertEqual( m1.daily_seasonality, 'daily' in m2.seasonalities) if m1.holidays is None: self.assertEqual(m1.holidays, m2.holidays) else: self.assertTrue((m1.holidays == m2.holidays).values.all()) self.assertEqual(m1.seasonality_mode, m2.seasonality_mode) self.assertEqual(m1.seasonality_prior_scale, m2.seasonality_prior_scale) self.assertEqual(m1.changepoint_prior_scale, m2.changepoint_prior_scale) self.assertEqual(m1.holidays_prior_scale, m2.holidays_prior_scale) self.assertEqual(m1.mcmc_samples, m2.mcmc_samples) self.assertEqual(m1.interval_width, m2.interval_width) self.assertEqual(m1.uncertainty_samples, m2.uncertainty_samples) # Check for cutoff and custom seasonality and extra regressors changepoints = pd.date_range('2012-06-15', '2012-09-15') cutoff = pd.Timestamp('2012-07-25') m1 = Prophet(changepoints=changepoints) m1.add_seasonality('custom', 10, 5) m1.add_regressor('binary_feature') m1.fit(df) m2 = diagnostics.prophet_copy(m1, cutoff=cutoff) changepoints = changepoints[changepoints <= cutoff] self.assertTrue((changepoints == m2.changepoints).all()) self.assertTrue('custom' in m2.seasonalities) self.assertTrue('binary_feature' in m2.extra_regressors)
def main(): print("START") open_db() origin = raw_input("Type origin: ") destination = raw_input("Type destination: ") start = str(raw_input("Type start year_month (MM/YY): ")) end = str(raw_input("Type end year_month (optional): ")) company = raw_input("Type company (optional): ") start_date = "20" + start[3:6] + "-" + start[0:2] now = utcnow() if end: end_date = "20" + end[3:6] + "-" + end[0:2] else: end_date = str(now.year + 3) + "-" + str(now.month).zfill(2) # periods: number of months between now and end_date if int(end_date[0:4]) == now.year: periods = (datetime.strptime(end_date + '-01', '%Y-%m-%d').month - now.month) else: periods = (datetime.strptime(end_date + '-01', '%Y-%m-%d').year - now.year) * 12 capa = get_capa(origin, destination, company, end_date) capa['ds'] = pd.to_datetime(capa['year_month'] + "-01") capa = capa.groupby(capa.ds).sum() capa['ds'] = capa.index capa = capa[['ds', 'capacity']] capa.columns = ['ds', 'cap'] segment = get_segment(origin, destination, company, end_date) segment['ds'] = pd.to_datetime(segment['year_month'] + "-01") segment = segment.groupby(segment.ds).sum() segment['ds'] = segment.index segment = segment[['ds', 'passengers']] segment.columns = ['ds', 'y'] # Train the model, then prepare dates to be predicted model = Prophet(mcmc_samples=100, changepoint_prior_scale=0.001) # Since there is no weekly data, remove the weekly columns to avoid getting an empty plot model.weekly_seasonality = False model.fit(segment) future = model.make_future_dataframe(periods=periods, freq='MS') # Add capacities to the prediction future = future.merge(capa, 'left', on='ds') forecast = model.predict(future) # restrict data to after the start_date forecast = forecast.loc[forecast['ds'] > (start_date + "-01")] model.history = model.history.loc[model.history['ds'] > (start_date + "-01")] model.plot(forecast).legend( handles=[forecast['t'], forecast['cap'], forecast['yhat']], labels=["Passengers", "Capacity", "Prediction"], loc=1) model.plot(forecast, uncertainty=True, xlabel='Date', ylabel='Passengers') plt.title(origin + " - " + destination) model.plot(forecast, uncertainty=True, xlabel='Date', ylabel='Passengers').savefig("/home/laurent/plot.jpg") plt.title(origin + " - " + destination) # The capacity doesn't look nice on the trend plot, so remove it before this plot del forecast['cap'] model.plot_components(forecast, uncertainty=True, plot_cap=False) model.plot_components( forecast, uncertainty=True, plot_cap=False).savefig("/home/laurent/plot_components.jpg")