Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juangesino/e140683a604dbfaf1458278e1af1e357 to your computer and use it in GitHub Desktop.
Save juangesino/e140683a604dbfaf1458278e1af1e357 to your computer and use it in GitHub Desktop.
Crazy Simple Anomaly Detection for Customer Success
# Crazy Simple Anomaly Detection for Customer Success
# 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"
)
threshold_ratio = 0.5
rolling_mad_upper = rolling_mad * (1 + threshold_ratio)
rolling_mad_lower = rolling_mad * (1 - threshold_ratio)
mad = lambda x: np.median(np.fabs(x - np.median(x)))
# 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)
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