【问题标题】:louvain community detection in complete weighted networks returns only 1 partition完全加权网络中的 louvain 社区检测仅返回 1 个分区
【发布时间】:2019-10-04 03:39:36
【问题描述】:

参考:https://stackoverflow.com/a/44907357/305883

我正在使用 python-louvain 实现来检测完整加权图中的社区。​​p>

但我只得到一个分区,包含所有节点。

代码:

import community # this is pip install python-louvain
import networkx as nx
import matplotlib.pyplot as plt

# Replace this with your networkx graph loading depending on your format !

# using graph g as a completed graph, weights between 0 and 1

#first compute the best partition
partition = community.best_partition(g)

#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(g)
count = 0.
for com in set(partition.values()) :
    count = count + 1.
    list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == com]
    nx.draw_networkx_nodes(g, pos, list_nodes, node_size = 20, node_color = str(count / size))


nx.draw_networkx_edges(g, pos, alpha=0.1)

plt.show()

我想从一个完整的加权网络中提取社区。​​p>

我也尝试了 girvan_newman (https://networkx.github.io/documentation/networkx-2.0/reference/algorithms/generated/networkx.algorithms.community.centrality.girvan_newman.html),但只能从 200 个节点(包括 198 个节点和 2 个节点)的完整图中检测到 2 个社区。​​p>

Louvain 是否可以正常工作以检测完整图中的社区? 更好的建议?

【问题讨论】:

  • 您确定您的权重在图表中的键 weight 之下吗?如果您在未加权的完整图上执行 Louvain,它在逻辑上会返回一个集群。

标签: python-3.x graph cluster-analysis networkx


【解决方案1】:

这种情况下使用的模型选择可能返回包含所有节点的单个块,这意味着没有足够的统计证据支持更多块。

你可以试试 Peixotos graph-tool 包,它有一个 weighted stochastic block model 的实现。

【讨论】:

  • @sparsky05(顺便说一句很棒的 nick sparsky)“没有足够的统计证据支持更多区块。”我之前在 0 和 1 之间调整了权重。可能有问题吗?对权重(例如指数 / arcTan 函数)应用转换以“强调”差异是否有意义?
  • 根据您使用的具体方法,您不需要在集群之前应用缩放。您可以尝试对权重进行转换,但在某些时候,您离阈值化数据不远了。
【解决方案2】:

如果你有一个加权网络,你需要使用weight='weight' 参数:

import networkx as nx
import community
import numpy as np
np.random.seed(0)

W = np.random.rand(15,15)
np.fill_diagonal(W,0.0)

G = nx.from_numpy_array(W)
louvain_partition = community.best_partition(G, weight='weight')
modularity2 = community.modularity(louvain_partition, G, weight='weight')
print("The modularity Q based on networkx is {}".format(modularity2))

基于networkx的模块化Q为0.0849022950503318

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 2019-02-14
    • 2018-01-05
    • 1970-01-01
    • 2014-06-12
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多