Mercurial Hosting > traffic-intelligence
comparison python/ml.py @ 285:5957aa1d69e1
Integrating Mohamed's changes
Changed the indicator interface to access values, so that the generic LCSS implementation can be used
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sat, 26 Jan 2013 19:02:25 -0500 |
parents | d70e9b36889c |
children | ee3302528cdc |
comparison
equal
deleted
inserted
replaced
284:f2cf16ad798f | 285:5957aa1d69e1 |
---|---|
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 '''Spectral Clustering algorithm''' | |
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 |