Closed SerenaQYHuang closed 5 years ago
In the Clustering coefficient part, I think explanations will be needed. For beginners, it's a bit difficult to get it. For example, what are "XXX","XXX","XXX"? If they are pseudo codes, how come I can still get a number running this code? (The output is 0.5731367499320134)
numbernx.algorithms.clustering(g,['XXX','XXX','XXX'])
nx.average_clustering(g)
And what does it mean by the numbers in the result in this following piece of code? Why is Champtercier 0 and Myriel 0.666666666667?
gorithms.clustering(g, ['Myriel', 'Champtercier', 'Count', 'Cravatte', 'Napoleon', 'Geborand', 'CountessdeLo', 'OldMan'])
# {'Champtercier': 0,
# 'Count': 0,
# 'CountessdeLo': 0,
# 'Cravatte': 0,
# 'Geborand': 0,
# 'Myriel': 0.06666666666666667,
# 'Napoleon': 0,
# 'OldMan': 0}
nx.average_clustering(g)
0.5731367499320134
nx.average_clustering(nx.complete_graph(5))
1.0
I've got an error with the following codes:
from networkx.algorithms import community
communities = list(community.girvan_newman(g))
#communities[0]
plt.figure(figsize=(15, 15))
pos =nx.spring_layout(g)
#nx.draw_networkx_nodes(g, pos, node_color='#ccccff', alpha=0.5)
nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.3)
for i in range(0, len(communities)):
nodelist = communities[i]
print(nodelist)
nx.draw_networkx_nodes(g, pos, nodelist=nodelist, node_color=color(i), alpha=0.8)
labels = dict([(n, '%s:%s' % (n, g.nodes[n]['group'])) for n in nodelist])
nx.draw_networkx_labels(g, pos, labels=labels, font_color='#666666')
Output:
import matplotlib color = matplotlib.cm.Accent #import color map, there are many color maps, you can checkout the color maps by the following: # import matplotlib.cm as cm # dir(cm) plt.figure(figsize=(15, 15)) pos =nx.spring_layout(g) nx.draw_networkx_nodes(g, pos, node_color='#ccccff', alpha=0.5) nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.3) labels = dict([(n, n) for n in **g1**.nodes]) _ = nx.draw_networkx_labels(**g1**, pos, labels=labels, font_color='#666666') for group in range(1, 20): nodelist = [n for n in g.nodes if g.nodes[n]['group'] == group] # If g.nodes's group = 1, add those nodes into the nodelist. They will be the same color 1 . If g.nodes's group = 2, they will be added to another nodelist ,and be colored 2. #print(nodelist) nx.draw_networkx_nodes(**g1**, pos, nodelist=nodelist, node_color=color(group), alpha=0.8)
Please be noted that g1 is not defined here. I think we should define it first
modified
I've got an error with the following codes:
from networkx.algorithms import community communities = list(community.girvan_newman(g)) #communities[0] plt.figure(figsize=(15, 15)) pos =nx.spring_layout(g) #nx.draw_networkx_nodes(g, pos, node_color='#ccccff', alpha=0.5) nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.3) for i in range(0, len(communities)): nodelist = communities[i] print(nodelist) nx.draw_networkx_nodes(g, pos, nodelist=nodelist, node_color=color(i), alpha=0.8) labels = dict([(n, '%s:%s' % (n, g.nodes[n]['group'])) for n in nodelist]) nx.draw_networkx_labels(g, pos, labels=labels, font_color='#666666')
Output:
try to change communities value
communities = list(community.label_propagation_communities(g))
for another question, I will try my best updating the explanation.
I've got an error with the following codes:
from networkx.algorithms import community communities = list(community.girvan_newman(g)) #communities[0] plt.figure(figsize=(15, 15)) pos =nx.spring_layout(g) #nx.draw_networkx_nodes(g, pos, node_color='#ccccff', alpha=0.5) nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.3) for i in range(0, len(communities)): nodelist = communities[i] print(nodelist) nx.draw_networkx_nodes(g, pos, nodelist=nodelist, node_color=color(i), alpha=0.8) labels = dict([(n, '%s:%s' % (n, g.nodes[n]['group'])) for n in nodelist]) nx.draw_networkx_labels(g, pos, labels=labels, font_color='#666666')
Output:
try to change communities value
communities = list(community.label_propagation_communities(g))
It doesn't work. Instead, a new problem occurred.
Actually the previous error didn't occur here but in the last several lines:
for i in range(0, len(communities)):
nodelist = communities[i]
print(nodelist)
nx.draw_networkx_nodes(g, pos, nodelist=nodelist, node_color=color(i), alpha=0.8)
labels = dict([(n, '%s:%s' % (n, g.nodes[n]['group'])) for n in nodelist])
nx.draw_networkx_labels(g, pos, labels=labels, font_color='#666666')
After talking with @SerenaQYHuang privately, we just solved the problem. Just upgrade the networkx
module from 2.0 to 2.2
Please be noted that g1 is not defined here. I think we should define it first