Advertisement
nlw

Spectral clustering example

nlw
Mar 10th, 2011
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. ################################
  2. ## Spectral clustering example, using python and scipy
  3. ##
  4. ## Coded by Nicolau Werneck <nwerneck@gmail.com> in 2011-03-10
  5. ## Based on "A Tutorial on Spectral Clustering", by Ulrike von Luxburg
  6. ################################
  7.  
  8. from pylab import *
  9. from scipy.sparse.linalg import eigen
  10. import numpy as np
  11.  
  12. ## The similarity function
  13. def dist(x,y):
  14.     sig = 3.0
  15.     return exp( -sig*(x-y)**2 )
  16.  
  17.  
  18. ## The "main" function... notice that running
  19. ## this will save three images to the local
  20. ## directory. If you want just ot see them you
  21. ## need to uncomment the ion(), and comment
  22. ## the savefig()s
  23.  
  24. if __name__=='__main__':
  25.  
  26.     #ion()
  27.  
  28.     Ns = 1000
  29.     who = array(floor(rand(Ns)*5), dtype=np.int)
  30.  
  31.     mu = array([0, 6, 12, 12, 18])
  32.     sig = array([1,1,1,1,1]) * 1.05
  33.  
  34.     q = randn(Ns)*sig[who]+mu[who]
  35.    
  36.     q.sort()
  37.  
  38.     figure(1, figsize=(6,3))
  39.     subplot(1,2,1)
  40.     title('Input data, sorted')
  41.     plot(q)
  42.     subplot(1,2,2)
  43.     title('Hitogram of input data')
  44.     hist(q, bins=sqrt(Ns), range=(-3,21)  )
  45.  
  46.     savefig('spec1.png', dpi=100)
  47.  
  48.  
  49.     ## The graph matrix with similarity weights...
  50.     W = zeros((Ns,Ns))
  51.  
  52.     Knn = 20
  53.  
  54.     ## Put the values in the matrix according to k-nearest neighbours
  55.     ## approach. OBS: we have are considering the inputs have been
  56.     ## sorted...
  57.     for n in range(Ns):
  58.         for j in range( max(n-Knn, 0), min(Ns, n+Knn) ):
  59.             W[n,j] = dist(q[n], q[j])
  60.             W[j,n] = W[n,j]
  61.  
  62.  
  63.     ## The degree matrix
  64.  
  65.     D = diag(np.sum(W,0))
  66.  
  67.     L = identity(Ns) - dot(inv(D), W).T
  68.  
  69.  
  70.     vw=np.max(abs(W))
  71.     vl=np.max(abs(L))
  72.  
  73.  
  74.     figure(2, figsize=(6,3))
  75.     suptitle('Weight and Laplacian matrices')
  76.     subplot(1,2,1)
  77.     imshow(W, interpolation='nearest', cmap='RdBu', vmin=-vw, vmax=vw)
  78.     subplot(1,2,2)
  79.     imshow(L, interpolation='nearest', cmap='RdBu', vmin=-vl/20, vmax=vl/20)
  80.     savefig('spec2.png', dpi=100)
  81.  
  82.  
  83.     figure(3, figsize=(6,3))
  84.     lam,u = eigen(L , k=4, which='SR')
  85.     lamall,ulixo = eigen(L , k=10, which='SR')
  86.  
  87.     lamall=real(lamall)
  88.  
  89.     lam =real(lam)
  90.     u   =real(u)
  91.  
  92.     print lam
  93.     subplot(1,2,1)
  94.     title('First eigenvalues')
  95.     plot(sort(lamall), '-+')
  96.     subplot(1,2,2)
  97.     title('First eigenvectors')
  98.     plot(u[:,:4])
  99.     savefig('spec3.png', dpi=100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement