【发布时间】:2021-12-13 05:37:33
【问题描述】:
使用具有属性Race 的节点创建一个图形,该属性可以是dog car elephant 或giraffe,然后将其分类为四个列表(四个社区)。见以下代码:
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}])
是否可以将四个列表添加到模块化功能中作为社区?还是有其他方法可以做到这一点?
感谢所有帮助!
【问题讨论】: