Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 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 | d70e9b36889c |
| children | 80cbafd69109 |
comparison
equal
deleted
inserted
replaced
| 307:8e66ced156dd | 308:8bafd054cda4 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Libraries for machine learning algorithms''' | 2 '''Libraries for machine learning algorithms''' |
| 3 | |
| 4 import numpy as np | |
| 3 | 5 |
| 4 __metaclass__ = type | 6 __metaclass__ = type |
| 5 | 7 |
| 6 class Centroid: | 8 class Centroid: |
| 7 'Wrapper around instances to add a counter' | 9 'Wrapper around instances to add a counter' |
| 53 centroids.append(Centroid(instance)) | 55 centroids.append(Centroid(instance)) |
| 54 else: | 56 else: |
| 55 centroids[i].add(instance) | 57 centroids[i].add(instance) |
| 56 | 58 |
| 57 return centroids | 59 return centroids |
| 60 | |
| 61 def spectralClustering(similarityMatrix,k): | |
| 62 ''' Steps of Spectral Clustering''' | |
| 63 n= len(similarityMatrix) | |
| 64 # create Laplacian matrix | |
| 65 rowsum = np.sum(similarityMatrix,axis=0) | |
| 66 D = np.diag(1 / np.sqrt(rowsum)) | |
| 67 I = np.identity(n) | |
| 68 L = I - np.dot(D,np.dot(similarityMatrix,D)) | |
| 69 # compute eigenvectors of L | |
| 70 U,sigma,V = np.linalg.svd(L) | |
| 71 # create feature vector from k first eigenvectors | |
| 72 # by stacking eigenvectors as columns | |
| 73 features = np.array(V[:k]).T | |
| 74 # k-means | |
| 75 from scipy.cluster.vq import kmeans, whiten, vq | |
| 76 features = whiten(features) | |
| 77 centroids,distortion = kmeans(features,k,iter=20) # default iter = 20 | |
| 78 code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) | |
| 79 return code,sigma | |
| 80 |
