Python 如何使用networkX检测有向加权图中的社区?
我有一个有向加权图,一个连接的组件,希望检测社区 我试过了Python 如何使用networkX检测有向加权图中的社区?,python,networkx,graph-theory,Python,Networkx,Graph Theory,我有一个有向加权图,一个连接的组件,希望检测社区 我试过了 community.best_partition(G) 但是,要获取分区,我会得到类型错误:错误的图形类型,请仅使用非定向图形 我把我的图转换成无向图,然后得到了分区。这是一种解决问题的方法,而无需将定向转换为无向并同时使用权重吗 此外,我还有一个问题,我如何才能在每个分区社区中获得前5个中心度节点?例如: undirected_strong_partition = community.best_partition(undirecte
community.best_partition(G)
但是,要获取分区,我会得到类型错误:错误的图形类型,请仅使用非定向图形
我把我的图转换成无向图,然后得到了分区。这是一种解决问题的方法,而无需将定向转换为无向并同时使用权重吗
此外,我还有一个问题,我如何才能在每个分区社区中获得前5个中心度节点?例如:
undirected_strong_partition = community.best_partition(undirectedstrong_community,weight='weight')
undirected_strong_partition
{'node1': 0,
'node2': 0,
'node3': 0,
'node4': 0,
'node5': 1,
'node6': 2,
'node7': 2,
'node8': 2,
'node9': 2,
...}
如果可能的话,我想在每个社区中获得前5个度节点。例如:
{community0: [nodetop1,nodetop2...nodetop5]}
这可以通过以下方式解决: 代码:
import community
import networkx as nx
# Generate test graph
G = nx.fast_gnp_random_graph(100, 0.1)
# Relabel nodes
G = nx.relabel_nodes(G, {i: f"node_{i}" for i in G.nodes})
# Compute partition
partition = community.best_partition(G)
# Get a set of the communities
communities = set(partition.values())
# Create a dictionary mapping community number to nodes within that community
communities_dict = {c: [k for k, v in partition.items() if v == c] for c in communities}
# Filter that dictionary to map to first sort the nodes in the community by degree, then take the top 5.
highest_degree = {k: sorted(v, key=lambda x: G.degree(x))[-5:] for k, v in communities_dict.items()}
>>> highest_degree
{0: ['node_91', 'node_24', 'node_19', 'node_8', 'node_83'],
1: ['node_54', 'node_69', 'node_88', 'node_48', 'node_84'],
2: ['node_79', 'node_34', 'node_52', 'node_46', 'node_36'],
3: ['node_98', 'node_96', 'node_86', 'node_76', 'node_30'],
4: ['node_29', 'node_40', 'node_10', 'node_90', 'node_32'],
5: ['node_94', 'node_97', 'node_59', 'node_25', 'node_37'],
6: ['node_31', 'node_56', 'node_57', 'node_62', 'node_63']}
输出:
import community
import networkx as nx
# Generate test graph
G = nx.fast_gnp_random_graph(100, 0.1)
# Relabel nodes
G = nx.relabel_nodes(G, {i: f"node_{i}" for i in G.nodes})
# Compute partition
partition = community.best_partition(G)
# Get a set of the communities
communities = set(partition.values())
# Create a dictionary mapping community number to nodes within that community
communities_dict = {c: [k for k, v in partition.items() if v == c] for c in communities}
# Filter that dictionary to map to first sort the nodes in the community by degree, then take the top 5.
highest_degree = {k: sorted(v, key=lambda x: G.degree(x))[-5:] for k, v in communities_dict.items()}
>>> highest_degree
{0: ['node_91', 'node_24', 'node_19', 'node_8', 'node_83'],
1: ['node_54', 'node_69', 'node_88', 'node_48', 'node_84'],
2: ['node_79', 'node_34', 'node_52', 'node_46', 'node_36'],
3: ['node_98', 'node_96', 'node_86', 'node_76', 'node_30'],
4: ['node_29', 'node_40', 'node_10', 'node_90', 'node_32'],
5: ['node_94', 'node_97', 'node_59', 'node_25', 'node_37'],
6: ['node_31', 'node_56', 'node_57', 'node_62', 'node_63']}
你能解释一下这是怎么回事吗?理想情况下,为什么
community.best_partition()
返回该错误?您可以回答无向图的问题。但是OP有一个有向图。