-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2_jazz.py
49 lines (42 loc) · 1.66 KB
/
q2_jazz.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
# girvan newman clustering
import itertools
import time
import networkx as nx
import networkx.algorithms.community as nx_algo
from networkx.algorithms.community import greedy_modularity_communities
from networkx.algorithms.community.centrality import girvan_newman
from sklearn.cluster import SpectralClustering
start_time = time.time()
jazz = nx.read_weighted_edgelist("C:\\Users\Kaushal\\Desktop\\jazz.net")
remove_edges = 7
comp = girvan_newman(jazz)
count = 1
print("_____________________________________________")
print("Betweenness-based clustering using the Girvan-Newman")
#generating tuples by removing different edges
for communities in itertools.islice(comp, remove_edges):
print("Edge Removal no. {0}".format(count))
if count == 3:
modu = list(sorted(c) for c in communities)
count = count + 1
print(tuple(sorted(c) for c in communities))
print(len(communities))
print("Modularity score is:{0}".format(nx_algo.modularity(jazz, modu)))
print("_____________________________________________")
print("Modularity Based Clustering")
#implementing modularity for dolphin
community = list(greedy_modularity_communities(jazz))
count = 0
for comm in community:
print(sorted(comm))
print("Modularity score is:{0}".format(nx_algo.modularity(jazz, community)))
print("___________________________________")
adj_mat = nx.to_numpy_matrix(jazz)
#implementing spectral clustering for dolphin
spec_clust = SpectralClustering(5, affinity='precomputed', n_init=100)
spec_clust.fit(adj_mat)
print('Spectral Clustering')
print(spec_clust.labels_)
# print(modu)
print("___________________________________")
print("time elapsed: {:.2f}s".format(time.time() - start_time))