Skip to content

Instantly share code, notes, and snippets.

@Diyago
Last active June 5, 2019 16:58
Show Gist options
  • Save Diyago/68c228b52b94d3abd50d46b0fa3dd6b6 to your computer and use it in GitHub Desktop.
Save Diyago/68c228b52b94d3abd50d46b0fa3dd6b6 to your computer and use it in GitHub Desktop.
def detect_anomalies(forecast):
forecasted = forecast[['ds','trend', 'yhat', 'yhat_lower', 'yhat_upper', 'fact']].copy()
#forecast['fact'] = df['y']
forecasted['anomaly'] = 0
forecasted.loc[forecasted['fact'] > forecasted['yhat_upper'], 'anomaly'] = 1
forecasted.loc[forecasted['fact'] < forecasted['yhat_lower'], 'anomaly'] = -1
#anomaly importances
forecasted['importance'] = 0
forecasted.loc[forecasted['anomaly'] ==1, 'importance'] = \
(forecasted['fact'] - forecasted['yhat_upper'])/forecast['fact']
forecasted.loc[forecasted['anomaly'] ==-1, 'importance'] = \
(forecasted['yhat_lower'] - forecasted['fact'])/forecast['fact']
return forecasted
pred = detect_anomalies(pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment