66"""
77
88import networkx as nx
9+ import numpy as np
910import matplotlib .pyplot as plt
10- from matrices import *
1111import spectralClustering
12+ from matrices import *
1213from stochasticBlockModel import SBM
13- import numpy as np
14+ from sklearn . cluster import KMeans
1415
1516#----------------------------------------------------------------------
1617def main ():
17- fig = plt .figure ()
1818#----------------------------------------------------------------------
1919# Stochastic block model parameters
2020#----------------------------------------------------------------------
21- n_vertices = 300 # number of vertices
21+ n_vertices = 1000 # number of vertices
2222n_communities = 2 # number of communities
2323
2424# Fixing cin > cout is referred to as the assortative case, because vertices
@@ -40,27 +40,35 @@ def main():
4040indices = [j + 1 for j , x in enumerate (sbm .community_labels ) if x == i ]
4141print ("Community C{}, n{} = {} vertices, color: {}, E[di] = {}" .format (str (i ), i , sbm .n_per_community [i ], color_map [i ], sbm .expected_degrees [i ]))
4242
43- if n_vertices > 100 :
44- print ("Can't draw if number of vertices is too big" )
43+ if n_vertices > 300 : print ("Can't draw if number of vertices is too big" )
4544else :
4645G = nx .from_numpy_matrix (sbm .adjacency_matrix ) # generate networkx graph
4746labels = {key : key + 1 for key in xrange (n_vertices )} # vertices numbers
4847node_color = color_map [sbm .community_labels ]
49- plt .title ("Generated graph using Stochastic block model" )
48+ plt .title ("Generated graph using Stochastic block model\n {} nodes and {} communities" . format ( n_vertices , n_communities ) )
5049nx .draw (G , labels = labels , node_color = node_color , font_size = 10 )
5150plt .figure ()
5251
5352#----------------------------------------------------------------------
5453# Spectral clustering
5554#----------------------------------------------------------------------
55+ n_clusters = 2
5656eigvals , eigvects = np .linalg .eig (BetheHessian (sbm .adjacency_matrix )) # eigvects[:,i] is the eigenvector corresponding to the eigenvalue eigvals[i]
57- plt .title ("Eigenvalues histogram " )
57+ plt .title ("Histogram of Bethe Hessian matrix eigenvalues " )
5858plt .hist (eigvals , bins = 100 ) # plot histogram
5959
60- indices = eigvals .argsort ()[:2 ] # find the two smallest eigenvalues indices
60+ indices = eigvals .argsort ()[:n_clusters ] # find the two smallest eigenvalues indices
61+ W = np .column_stack ((eigvects [:,indices [0 ]], eigvects [:,indices [1 ]]))
6162plt .figure ()
6263plt .title ("Eigenvectors corresponding to the two smallest eigenvalues" )
63- plt .plot (eigvects [:,indices [0 ]], eigvects [:,indices [1 ]], 'ro' , markersize = 4 )
64+ plt .plot (W [:,0 ], W [:,1 ], 'o' , markersize = 5 )
65+
66+ kmeans = KMeans (n_clusters = n_clusters ).fit (W )
67+ plt .figure ()
68+ plt .title ("K-means" )
69+ for i in xrange (n_clusters ):
70+ ds = W [np .where (kmeans .labels_ == i )]
71+ plt .plot (ds [:,0 ], ds [:,1 ], 'o' , markersize = 5 )
6472plt .show ()
6573
6674##----------------------------------------------------------------------
0 commit comments