-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity.py
65 lines (50 loc) · 1.69 KB
/
community.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import networkx as nx
import numpy as np
from sklearn.decomposition import NMF
from sklearn.cluster import KMeans
from QOS_NMF import QOSNMF
import matplotlib.pyplot as plt
ground_truth = [0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1]
# Load the graph from the .gml file
G = nx.read_gml("karate.gml", label='id')
# seeing the graph
nx.draw_networkx(G, with_labels=True)
plt.show()
# Construct the adjacency matrix
A = nx.to_numpy_array(G)
# Step 2: Normalize the adjacency matrix
P = A / np.sum(A, axis=1, keepdims=True)
# Step 3.1: Perform NMF using Sklearn
"""
model = NMF(n_components=2, init='random', random_state=0)
W = model.fit_transform(P)
H = model.components_
"""
# Step 3.2: Perform NMF using COS-NMF
print(P.shape[0])
W, H = QOSNMF(P, P.shape[0])
# Step 4: Extract the communities
n_communities = 2
kmeans = KMeans(n_clusters=n_communities, random_state=0, n_init=10).fit(W)
communities = kmeans.labels_
print("Number of communities:", n_communities, "\nCommunities:", communities)
def extract_nodes(comm,n):
nodes = []
for i in range(len(comm)):
if communities[i] == n:
nodes.append(i+1)
return nodes
node_lists = {}
for i in range(n_communities):
node_lists[i] = extract_nodes(communities,i)
pos = nx.spring_layout(G)
# graph without colors
nx.draw_networkx(G, pos, edge_color='k', with_labels=True,font_weight='light', node_size=280, width=0.9)
# drawing each community with a different color
colors = ['r', 'b', 'g', 'c', 'm', 'y', 'k']
for i in range(n_communities):
nx.draw_networkx_nodes(G, pos, nodelist=node_lists[i], node_color=colors[i])
result = [i for i in communities]
print(result)
print(ground_truth)
plt.show()