Last active
June 5, 2019 16:58
-
-
Save Diyago/68c228b52b94d3abd50d46b0fa3dd6b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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