Created
February 9, 2021 16:46
-
-
Save juangesino/e140683a604dbfaf1458278e1af1e357 to your computer and use it in GitHub Desktop.
Crazy Simple Anomaly Detection for Customer Success
This file contains 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
# Crazy Simple Anomaly Detection for Customer Success |
This file contains 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
# Create Pandas DataFrame | |
temp = pd.concat( | |
[ | |
x, | |
y, | |
rolling_mad, | |
rolling_mad_upper, | |
rolling_mad_lower, | |
], | |
axis=1, | |
) | |
# Rename columns | |
temp.columns = ["timeline", "values", "mad", "upper_mad", "lower_mad"] | |
# Drop initial values (outside rolling window) | |
temp.dropna(subset=["mad"], inplace=True) | |
# Determine anomalies | |
temp["is_anomaly"] = ~( | |
temp["values"].between(temp["lower_mad"], temp["upper_mad"]) | |
) | |
temp["anomaly_status"] = temp["is_anomaly"].apply( | |
lambda v: "ANOMALY" if v else "NOT ANOMALY" | |
) |
This file contains 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
threshold_ratio = 0.5 | |
rolling_mad_upper = rolling_mad * (1 + threshold_ratio) | |
rolling_mad_lower = rolling_mad * (1 - threshold_ratio) |
This file contains 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
mad = lambda x: np.median(np.fabs(x - np.median(x))) |
This file contains 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
# We'll use a 7-day window | |
rolling_window = 7 | |
rolling_mad = y.rolling(window=rolling_window, center=False).median() | |
+ y.rolling(window=rolling_window, center=False).apply(mad, raw=True) |
This file contains 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
X = df["day"] | |
Y = df["customer_3"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment