Mercurial Hosting > traffic-intelligence
changeset 308:8bafd054cda4
Added a function to compute LCSS distance between two indcators
author | Mohamed Gomaa |
---|---|
date | Tue, 25 Dec 2012 02:20:25 -0500 |
parents | 8e66ced156dd |
children | 80cbafd69109 |
files | python/indicators.py python/ml.py |
diffstat | 2 files changed, 46 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/python/indicators.py Tue Dec 25 02:16:10 2012 -0500 +++ b/python/indicators.py Tue Dec 25 02:20:25 2012 -0500 @@ -4,7 +4,7 @@ __metaclass__ = type import moving - +from utils import LCSS # need for a class representing the indicators, their units, how to print them in graphs... class TemporalIndicator: '''Class for temporal indicators @@ -82,7 +82,28 @@ plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) if self.maxValue: ylim(ymax = self.maxValue) - + + def valueSorted(self): + ''' return the values after sort the keys in the indicator''' + values=[] + keys = self.values.keys() + keys.sort() + for key in keys: + values.append(self.values[key]) + return values + @staticmethod + def getDLCSS(TemporalIndicator1,TemporalIndicator2, threshold, delta= np.inf , method='min' ): + ''' compute the distance between two indicators using LCSS + two common methods are used: min or mean of the indicators length''' + l1= TemporalIndicator1.valueSorted + l2= TemporalIndicator2.valueSorted + if method = 'min': + DLCSS= 1- (LCSS(l1,l2, threshold, delta, distance))/min(len(l1),len(l2))) + if method = 'mean': + average= len(l1)+len(l2))/2 + DLCSS= 1- ((LCSS(l1,l2, threshold, delta, distance))/average) + return DLCSS + class SeverityIndicator(TemporalIndicator): '''Class for severity indicators field mostSevereIsMax is True
--- a/python/ml.py Tue Dec 25 02:16:10 2012 -0500 +++ b/python/ml.py Tue Dec 25 02:20:25 2012 -0500 @@ -1,6 +1,8 @@ #! /usr/bin/env python '''Libraries for machine learning algorithms''' +import numpy as np + __metaclass__ = type class Centroid: @@ -55,3 +57,24 @@ centroids[i].add(instance) return centroids + +def spectralClustering(similarityMatrix,k): + ''' Steps of Spectral Clustering''' + n= len(similarityMatrix) + # create Laplacian matrix + rowsum = np.sum(similarityMatrix,axis=0) + D = np.diag(1 / np.sqrt(rowsum)) + I = np.identity(n) + L = I - np.dot(D,np.dot(similarityMatrix,D)) + # compute eigenvectors of L + U,sigma,V = np.linalg.svd(L) + # create feature vector from k first eigenvectors + # by stacking eigenvectors as columns + features = np.array(V[:k]).T + # k-means + from scipy.cluster.vq import kmeans, whiten, vq + features = whiten(features) + centroids,distortion = kmeans(features,k,iter=20) # default iter = 20 + code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) + return code,sigma +