微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

社区检测 Louvain 算法的模块化在添加边缘权重时不会改变

如何解决社区检测 Louvain 算法的模块化在添加边缘权重时不会改变

当向我的图中添加边权重时,与没有边权重相比,Louvain 算法的模块化分数不会改变(它保持在大约 0.8)。我将权重计算为连接节点之间的欧几里德相似度分数,因此它们的范围在 0 到 1 之间。

我对社区检测很陌生,那么可能是什么导致了这个问题?

编辑:

import community
import networkx as nx 
from community import best_partition
from community import modularity

#Reduced dictionary explanation: the key(eg. BIDU) is one node,#the nested key(eg: 865326452400504832) is the second node,#and the value is the edge weight computed as the average 
#Euclidean similarity (based on a feature vector) of the
# nested key with all other nested keys within the same 
#key(eg.0.7885422115698439 = vector similarity between 865326452400504832 and 865326457001697280)

similarity_reduced = {"BIDU": {865326452400504832: 0.7885422115698439,865326457001697280: 0.7885422115698439},'JD':{865326452400504832: 0.6138829308835345,865326457001697280: 0.6259871779548237,869811004955389952: 0.4513278972685143},'TCEHY':{865326452400504832: 0.6020128491158321,865326457001697280: 0.6078782014152254,869528165076447232: 0.42134883896121356}}

  
def create_graph():
  G = nx.Graph()
  for keys,values in similarity_reduced.items():
    G.add_node(keys)
    for key,value in values.items():
      G.add_edge(keys,key,weight = value)
      if key in G.nodes():
          continue
      else :
          G.add_node(key)
  return G

G = create_graph()
nx.set_node_attributes(G,attributes)
print(nx.info(G))

Returns:
Name: 
Type: Graph
Number of nodes: 7
Number of edges: 8
Average degree:   2.2857

#These are all the edges in the graph
list(G.edges(data=True))

Returns:
"[('BIDU',865326452400504832,{'weights': 0.7885422115698439}),('BIDU',865326457001697280,(865326452400504832,'JD',{'weights': 0.6138829308835345}),'TCEHY',{'weights': 0.6020128491158321}),(865326457001697280,{'weights': 0.6259871779548237}),{'weights': 0.6078782014152254}),('JD',869811004955389952,{'weights': 0.4513278972685143}),('TCEHY',869528165076447232,{'weights': 0.42134883896121356})]
"

partitions = best_partition(G,weight="weight")
mod = modularity(partitions,G,weight="weight")
mod
Returns:
"0.1484375"

´´´

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。