Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 293:ee3302528cdc
rearranged new code by Paul (works now)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 08 Feb 2013 18:13:29 -0500 |
| parents | 5957aa1d69e1 |
| children | 6c068047edbf |
comparison
equal
deleted
inserted
replaced
| 292:8b2c8a4015f1 | 293:ee3302528cdc |
|---|---|
| 56 else: | 56 else: |
| 57 centroids[i].add(instance) | 57 centroids[i].add(instance) |
| 58 | 58 |
| 59 return centroids | 59 return centroids |
| 60 | 60 |
| 61 def spectralClustering(similarityMatrix,k): | 61 def spectralClustering(similarityMatrix, k, iter=20): |
| 62 '''Spectral Clustering algorithm''' | 62 '''Spectral Clustering algorithm''' |
| 63 n = len(similarityMatrix) | 63 n = len(similarityMatrix) |
| 64 # create Laplacian matrix | 64 # create Laplacian matrix |
| 65 rowsum = np.sum(similarityMatrix,axis=0) | 65 rowsum = np.sum(similarityMatrix,axis=0) |
| 66 D = np.diag(1 / np.sqrt(rowsum)) | 66 D = np.diag(1 / np.sqrt(rowsum)) |
| 72 # by stacking eigenvectors as columns | 72 # by stacking eigenvectors as columns |
| 73 features = np.array(V[:k]).T | 73 features = np.array(V[:k]).T |
| 74 # k-means | 74 # k-means |
| 75 from scipy.cluster.vq import kmeans, whiten, vq | 75 from scipy.cluster.vq import kmeans, whiten, vq |
| 76 features = whiten(features) | 76 features = whiten(features) |
| 77 centroids,distortion = kmeans(features,k,iter=20) # default iter = 20 | 77 centroids,distortion = kmeans(features,k, iter) |
| 78 code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) | 78 code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) |
| 79 return code,sigma | 79 return code,sigma |
