diff 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
line wrap: on
line diff
--- a/python/ml.py	Fri Dec 21 18:33:36 2012 -0500
+++ b/python/ml.py	Sat Jan 26 19:02:25 2013 -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,23 @@
             centroids[i].add(instance)
 
     return centroids
+
+def spectralClustering(similarityMatrix,k):	
+	'''Spectral Clustering algorithm'''
+	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