Last active
October 12, 2018 06:54
-
-
Save dceoy/e0b527ef4b7c8e51f32c670afaef527b to your computer and use it in GitHub Desktop.
[Python] Hotelling's T-squared anomaly detection
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
#!/usr/bin/env python | |
import numpy as np | |
from scipy import stats | |
class HotellingsT2(object): | |
def __init__(self, alpha): | |
self.anomaly_score_threshold = stats.chi2.ppf(q=(1 - alpha), df=1) | |
def is_anomaly_1dim(self, new_x, normal_x): | |
anomaly_score = (new_x - np.mean(normal_x)) ** 2 / np.var(normal_x) | |
return anomaly_score > self.anomaly_score_threshold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment