【问题标题】:Calculate modularity using networkx使用networkx计算模块化
【发布时间】:2021-12-13 05:37:33
【问题描述】:

使用具有属性Race 的节点创建一个图形,该属性可以是dog car elephantgiraffe,然后将其分类为四个列表(四个社区)。见以下代码:

import pandas as pd
import networkx as nx

df = pd.read_csv(path + 'raw_data.csv')

G = nx.Graph()

for i in range(len(df)):
    node = df.Name.values[i]
    race = df.Race.values[i]
    
    G.add_nodes_from([(node, {"Race":race})])

dog = [x for x,y in G.nodes(data=True) if y['Race']=='Dog']
cat = [x for x,y in G.nodes(data=True) if y['Race']=='Cat']
elephant = [x for x,y in G.nodes(data=True) if y['Race']=='Elephant']
giraffe = [x for x,y in G.nodes(data=True) if y['Race']=='Giraffe']

然后我想计算模块化,使用 networkx 像这个例子:

import networkx.algorithms.community as nx_comm

G = nx.barbell_graph(3, 0)
nx_comm.modularity(G, [{0, 1, 2}, {3, 4, 5}])

是否可以将四个列表添加到模块化功能中作为社区?还是有其他方法可以做到这一点?

感谢所有帮助!

【问题讨论】:

    标签: python pandas networkx


    【解决方案1】:

    试试下面的?

    nx_comm.modularity(G, [set(race) for race in (dog, cat, elephant, giraffe)])
    

    【讨论】:

    • 不起作用,但还是感谢您的帮助:)
    • 错误是什么,@lo7?
    猜你喜欢
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多