|
@@ -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 = {}
|