Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit dbb7828

Browse files
committed
Add kmeans
1 parent 482cdf6 commit dbb7828

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/main.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
"""
77

88
import networkx as nx
9+
import numpy as np
910
import matplotlib.pyplot as plt
10-
from matrices import *
1111
import spectralClustering
12+
from matrices import *
1213
from stochasticBlockModel import SBM
13-
import numpy as np
14+
from sklearn.cluster import KMeans
1415

1516
#----------------------------------------------------------------------
1617
def 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
2222
n_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():
4040
indices = [j+1 for j, x in enumerate(sbm.community_labels) if x == i]
4141
print("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")
4544
else:
4645
G = nx.from_numpy_matrix(sbm.adjacency_matrix) # generate networkx graph
4746
labels = {key: key+1 for key in xrange(n_vertices)} # vertices numbers
4847
node_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))
5049
nx.draw(G, labels=labels, node_color=node_color, font_size=10)
5150
plt.figure()
5251

5352
#----------------------------------------------------------------------
5453
# Spectral clustering
5554
#----------------------------------------------------------------------
55+
n_clusters = 2
5656
eigvals, 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")
5858
plt.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]]))
6162
plt.figure()
6263
plt.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)
6472
plt.show()
6573

6674
##----------------------------------------------------------------------

0 commit comments

Comments
 (0)