Skip to content

Instantly share code, notes, and snippets.

@hassanj47
Created June 6, 2022 00:31
Show Gist options
  • Save hassanj47/b808429a3c62f8494faf45b9a0626afa to your computer and use it in GitHub Desktop.
Save hassanj47/b808429a3c62f8494faf45b9a0626afa to your computer and use it in GitHub Desktop.
prophet model for time series survival analysis
def build_prophet(df):
'''Build prophet model and generate events for survival modelling.'''
# prepping data for prophet assuming data starts from Jan, 2015
df['ds'] = pd.date_range(start='2015-01-01', periods=len(df))
df['y'] = df.sales
df = df[['ds','y']]
# train/test splits
train = df[:-TEST_DAYS]
test = df[-TEST_DAYS:]
model = Prophet(daily_seasonality=True)
model.fit(train)
future = model.make_future_dataframe(FORECAST_DAYS, include_history=False)
forecast = model.predict(future)
results = pd.merge(forecast, test, on='ds')
# resample on weeks
results = results.set_index('ds').resample('w').sum().reset_index()
# generate metric suitable for survival analysis
results_survival = survival_mape_exp(results, WIN_SIZE_WEEKS)
# generate events and censors
thresh_week, event = survival_events(results_survival)
return pd.DataFrame({'thresh_week':[thresh_week], 'event':[event]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment