Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 309:80cbafd69109
Added spectral clustering function
| author | Mohamed Gomaa |
|---|---|
| date | Tue, 25 Dec 2012 02:24:21 -0500 |
| parents | 8bafd054cda4 |
| children | 6c068047edbf |
comparison
equal
deleted
inserted
replaced
| 308:8bafd054cda4 | 309:80cbafd69109 |
|---|---|
| 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 | |
| 61 def spectralClustering(similarityMatrix,k): | 62 def spectralClustering(similarityMatrix,k): |
| 62 ''' Steps of Spectral Clustering''' | 63 ''' Steps of Spectral Clustering''' |
| 63 n= len(similarityMatrix) | 64 n= len(similarityMatrix) |
| 64 # create Laplacian matrix | 65 # create Laplacian matrix |
| 65 rowsum = np.sum(similarityMatrix,axis=0) | 66 rowsum = np.sum(similarityMatrix,axis=0) |
| 74 # k-means | 75 # k-means |
| 75 from scipy.cluster.vq import kmeans, whiten, vq | 76 from scipy.cluster.vq import kmeans, whiten, vq |
| 76 features = whiten(features) | 77 features = whiten(features) |
| 77 centroids,distortion = kmeans(features,k,iter=20) # default iter = 20 | 78 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 code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) |
| 79 return code,sigma | 80 return code,sigma |
| 80 |
