From 2c0905091be20f795ade99cd87d354db357086af Mon Sep 17 00:00:00 2001 From: "Steve L. Nyemba" Date: Wed, 1 Feb 2017 07:16:06 -0600 Subject: [PATCH] analysis of anomalies @TODO: road test --- src/api/ml-index.py | 9 --------- src/utils/ml.py | 47 +++++++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 src/api/ml-index.py diff --git a/src/api/ml-index.py b/src/api/ml-index.py deleted file mode 100644 index 866b442..0000000 --- a/src/api/ml-index.py +++ /dev/null @@ -1,9 +0,0 @@ -from flask import Flask, render_template -from flask_socketio import SocketIO - -app = Flask(__name__) -app.config['SECRET_KEY'] = '[0v8-247]-4qdm-h8r5!' -socketio = SocketIO(app) - -if __name__ == '__main__': - socketio.run(app) \ No newline at end of file diff --git a/src/utils/ml.py b/src/utils/ml.py index cde50e5..c2c049f 100644 --- a/src/utils/ml.py +++ b/src/utils/ml.py @@ -25,9 +25,18 @@ class ML: # r = [] for row in data : - for item in row : - if attr in item and item[attr] == value: - r.append(item) + if isinstance(row,list) : + for item in row : + + if attr in item and item[attr] == value: + r.append(item) + else: + # + # We are dealing with a vector of objects + # + if attr in row and row[attr] == value: + r.append(row) + return r @staticmethod def Extract(lattr,data): @@ -43,7 +52,8 @@ class ML: @TODO: determine computationally determine epsilon """ class AnomalyDetection: - + def __init__(self): + pass def split(self,data,index=-1,threshold=0.65) : N = len(data) # if N < LIMIT: @@ -226,7 +236,9 @@ class AnomalyDetection: sigma = [ list(row) for row in sigma] return {"cov":sigma,"mean":list(u)} -class AnalyzeAnomalies(AnomalyDetection): +class AnalyzeAnomaly(AnomalyDetection): + def __init__(self): + AnomalyDetection.__init__(self) """ This analysis function will include a predicted status because an anomaly can either be - A downtime i.e end of day @@ -236,17 +248,28 @@ class AnalyzeAnomalies(AnomalyDetection): """ def predict(self,xo,info): x = xo[len(xo)-1] - r = AnomalyDetection.predict(x,info) + r = AnomalyDetection.predict(self,[x],info) # # In order to determine what the anomaly is we compute the slope (idle or crash) # The slope is computed using the covariance / variance of features # - N = len(info['features']) - xy = ML.Extract(info['features'],xo) - xy = np.matrix(xy) - vxy= [xy[:,i] for i in range(0,N)] - print N,vxy.shape - alpha = info['cov'] / vxy + if r is not None: + N = len(info['features']) + xy = ML.Extract(info['features'],xo) + xy = np.array(xy) + + vxy= np.array([ np.var(xy[:,i]) for i in range(0,N)]) + cxy=np.array(info['parameters']['cov']) + #cxy=np.cov(np.transpose(xy)) + if np.sum(vxy) == 0: + vxy = cxy + + alpha = cxy/vxy + + + + r = {"anomaly":r[0][1],"slope":list(alpha[:,0])} + return r class Regression: parameters = {}