'Graph and console output are different. Both should be the same .Where is my mistake?
Karata_Club dataset has 4 total communities, according to my dataset. But when plot the graph the graph shows the five different communities,both should be same .
import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nxcom
import community
G=nx.read_adjlist('D:\Research Folder\Datasets\Karata club\karate.txt') \\You can take any Karate club dataset to check.
# find the value of K-core
G2 = nx.k_core(G,k=4)
nx.draw(G2 , with_labels=True)
print(G2)
print(G2.nodes())
print(G2.edges())
# Find the communities
communities = sorted(nxcom.greedy_modularity_communities(G), key=len, reverse=True)
# Count the communities
print(f"The Total {len(communities)} communities.")
print(communities)
for node in G:
partition = community.best_partition(G) # compute communities
print(partition)
pos = nx.spring_layout(G) # compute graph layout
plt.figure(figsize=(9, 9)) # image is 9 x 9 inches
plt.axis('off')
nx.draw_networkx_nodes(G, pos, node_size=600, cmap=plt.cm.RdYlBu, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.3)
nx.draw_networkx_labels(G, pos)
plt.show(G)
Solution 1:[1]
Here you are using networkx to compute number of communities
communities = sorted(nxcom.greedy_modularity_communities(G), key=len,reverse=True)
Which uses Clauset-Newman-Moore algo to find communities in the graph.
And while calculating the partition (also used to print graph) you are using "community" module
for node in G: partition = community.best_partition(G) # compute communities print(partition)
and this module uses Louvain heuristices algorithm for community detection.
Since both the algorithms are using different methods for community detection thus results from both algorithm might differ, that is what happening in your case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | micro5 |

