我注意到,如果我使用相同的值更改图中的所有边权重,则community.best_partition并不总是导致相同的社区。
我在所有情况下都使用相同的随机状态,图形完全相同,只是不是所有边权重都等于 1,例如它们可能等于 5。模块化的定义抵消了这个乘以邻接矩阵的因素当我阅读算法时,我找不到应该改变结果的步骤。是否有导致这种差异的原因?
import networkx as nx
import community
from sklearn.metrics import adjusted_rand_score
def main():
g = nx.davis_southern_women_graph()
nodes = g.nodes()
clusters_init = community.best_partition(g, random_state=10)
print("modularity with initial clusters = %.15f" % community.modularity(clusters_init, g))
labels_init = [clusters_init[n] for n in nodes]
for num in range(1, 9):
for u, v in g.edges():
g[u][v]["weight"] = num
clusters = community.best_partition(g, random_state=10)
labels = [clusters[n] for n in nodes]
print("value of edge weight = %d," % num, "modularity = %.15f," % …Run Code Online (Sandbox Code Playgroud)