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

Commit 5961803

Browse files
committed
Improve spectral clustering, correct bug in Modularity matrix
1 parent 6d0aa87 commit 5961803

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def main():
2020
#----------------------------------------------------------------------
2121
# Stochastic block model parameters
2222
#----------------------------------------------------------------------
23-
n_vertices = 3000 # number of vertices
23+
n_vertices = 200 # number of vertices
2424
n_communities = 2 # number of communities
2525

2626
# Fixing cin > cout is referred to as the assortative case, because vertices
@@ -29,7 +29,7 @@ def main():
2929
# is that any tractable algorithm will only detect communities if
3030
# abs(cin - cout) > n_communities*sqrt(c), where c is the average degree.
3131
cin = 15
32-
cout = 5
32+
cout = 1
3333
probability_matrix = (1.0/n_vertices)*(np.full((n_communities,n_communities), cout, dtype=int) + np.diag([cin-cout]*n_communities)) # matrix of edge probabilities
3434
sbm = SBM(n_vertices, n_communities, probability_matrix)
3535
print("Average degree: {}, abs(cin - cout): {}, n_commuties*sqrt(c): {}".format(sbm.average_degree, abs(cin-cout), n_communities*np.sqrt(sbm.average_degree)))
@@ -57,13 +57,13 @@ def main():
5757
n_clusters = 2
5858
if n_clusters != n_communities:
5959
print("Number of clusters ({}) is not equal to number of communities generated by the SBM ({})!".format(n_clusters, n_communities))
60-
spectral_labels, eigvals, eigvects, W = SpectralClustering(n_clusters, BetheHessian(sbm.adjacency_matrix)) # spectral clustering
60+
spectral_labels, eigvals, eigvects, W = SpectralClustering(n_clusters, ModularityMatrix(sbm.adjacency_matrix), "ModularityMatrix") # spectral clustering
6161
well_placed_vertices = pg.permutation_calculator(sbm.community_labels, spectral_labels, n_clusters)
6262
print("Spectral clustering accuracy: " + str(100*well_placed_vertices/n_vertices) + "%")
6363

6464
# Eigenvalues and eigenvectors
6565
if n_vertices <= PLOT_MAX_NODES:
66-
plt.title("Histogram of Bethe Hessian matrix eigenvalues")
66+
plt.title("Histogram of matrix eigenvalues")
6767
plt.hist(eigvals, bins=100) # plot histogram of the eigenvalues
6868
if n_clusters <= 2:
6969
plt.figure()

src/matrices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def ModularityMatrix(adjacency_matrix):
2828
:param matrix: np.array symetric square matrix
2929
:return: np.array square matrix
3030
"""
31-
d = np.sum(adjacency_matrix, axis=0)
32-
return adjacency_matrix - (np.dot(d, d.T) / np.sum(d))
31+
d = np.matrix(np.sum(adjacency_matrix, axis=0))
32+
return adjacency_matrix - (np.dot(d.T, d).astype(float) / np.sum(d))
3333

3434
# ----------------------------------------------------------------------
3535
def BetheHessian(adjacency_matrix, r=None):

src/spectralClustering.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
from sklearn.cluster import KMeans
1010

1111
#----------------------------------------------------------------------
12-
def SpectralClustering(n_clusters, matrix):
12+
def SpectralClustering(n_clusters, matrix, matrix_name):
1313
""""""
1414
eigvals, eigvects = np.linalg.eig(matrix) # eigvects[:,i] is the eigenvector corresponding to the eigenvalue eigvals[i]
15-
indices = eigvals.argsort()[:n_clusters] # find the 'n_clusters' smallest eigenvalues indices
15+
if matrix_name in ["BetheHessian", "LaplacianMatrix"]:
16+
indices = eigvals.argsort()[:n_clusters] # find the 'n_clusters' isolated eigenvalues
17+
elif matrix_name in ["ModularityMatrix"]:
18+
indices = eigvals.argsort()[-n_clusters:] # find the 'n_clusters' isolated eigenvalues
19+
else:
20+
raise ValueError("Unknown matrix name")
1621
W = eigvects[:,indices]
1722
kmeans = KMeans(n_clusters=n_clusters).fit(W) # kmeans
1823
return kmeans.labels_, eigvals, eigvects, W

0 commit comments

Comments
 (0)