소스 검색

ml implementation

steve 8 년 전
부모
커밋
9bb3a746b2
2개의 변경된 파일23개의 추가작업 그리고 1개의 파일을 삭제
  1. 21 1
      src/api/index.py
  2. 2 0
      src/utils/ml.py

+ 21 - 1
src/api/index.py

@@ -52,7 +52,6 @@ f.close()
 #
 #
 #
 #
 from threading import Thread, RLock
 from threading import Thread, RLock
-p  = {'uri':'http://dev.the-phi.com:5984','dbname':'monitor','uid':'logs','filename':'logs.JSON'}
 p = CONFIG['store']['args']
 p = CONFIG['store']['args']
 class_read = CONFIG['store']['class']['read']
 class_read = CONFIG['store']['class']['read']
 class_write= CONFIG['store']['class']['write']
 class_write= CONFIG['store']['class']['write']
@@ -132,6 +131,27 @@ def requirements():
 def dashboard():
 def dashboard():
 	context = PARAMS['context']
 	context = PARAMS['context']
 	return render_template('dashboard.html',context=context)
 	return render_template('dashboard.html',context=context)
+
+"""
+	This function is designed to trigger learning for anomaly detection
+	@TODO: forward this to a socket i.e non-blocking socket
+"""
+@app.route('/learn')
+def learn():
+	app = request.args.get('app')
+	id	= request.args.get('id')
+	p = CONFIG['store']['args']
+	class_read = CONFIG['store']['class']['read']
+
+	p['qid'] =[id] #HANDLERS['processes']['config'].keys()
+	gReader = factory.instance(type=class_read,args=p)
+	
+	r = gReader.read()
+	r = r[id]
+	r = ML.Filter('label',app,r)
+	label = ML.Extract(['status'],r)
+	
+	
 if __name__== '__main__':
 if __name__== '__main__':
 	
 	
 	mthread.start()
 	mthread.start()

+ 2 - 0
src/utils/ml.py

@@ -78,10 +78,12 @@ class AnomalyDetection:
 		tp = 0 # true positive
 		tp = 0 # true positive
 		fp = 0 # false positive
 		fp = 0 # false positive
 		fn = 0 # false negative
 		fn = 0 # false negative
+		tn = 0 # true negative
 		for i in range(0,N):
 		for i in range(0,N):
 			tp += 1 if test[i][1]==labels[i] and test[i][1] == 1
 			tp += 1 if test[i][1]==labels[i] and test[i][1] == 1
 			fp += 1 if test[i][1] != labels[i] and test[i][1] == 1
 			fp += 1 if test[i][1] != labels[i] and test[i][1] == 1
 			fn += 1 if test[i][1] != labels[i] and test[i][1] == 0
 			fn += 1 if test[i][1] != labels[i] and test[i][1] == 0
+			tn += 1 if test[i][1] == labels[i] and test[i][1] == 0
 		precision = tp / (tp + fp)
 		precision = tp / (tp + fp)
 		recall	= tp / (tp + fn)
 		recall	= tp / (tp + fn)
 		fscore 	= (2 * precision * recall)/ (precision + recall)
 		fscore 	= (2 * precision * recall)/ (precision + recall)