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

使用 networkx 从 gephi 导入的图形中获取模块化类

如何解决使用 networkx 从 gephi 导入的图形中获取模块化类

我已经从 Gephi 导入了一个 graphml 到 NetworkX。

G = nx.read_graphml(r"pah\EXPORTCM0606.graphml")

在 Gephi 中,我计算了获得 6 个主要社区的模块化类,我希望现在在 NetworkX 中获取这些社区,以便获得他们推文中最常用的词。 所以我的问题是双重的: 如何使用 NetworkX 从 G 中获取这些已经在 Gephi 中计算出的模块化类社区?

然后如何将我从 MongoDB 生成的图与推文和从 Gephi 导入的图进行匹配? 使用推文从 MongoDB 生成图形的代码

from pymongo import MongoClient
client = MongoClient()
db = client.cuartoMilenio06062021

import networkx as nx

G = nx.DiGraph()

for result in db.tweets.find():
     uid = result['user']['screen_name']
     G.add_node(uid) 
       
     #Attributes
     if 'quoted_status' in result and 'text' in result: 
         node_attrs = {uid: {"text": result['quoted_status']['text']}}
         nx.set_node_attributes(G,node_attrs)   

谢谢。

解决方法

我将向您展示一个非常简单的示例,希望它能奏效。 在使用了 Gephi 中包含的网络 Power Grid.gml 后,我计算了 Gephi 内部的模块性,将图导出为 graphml 并使用 networkx 读取。

# read the network
import networkx as nx
G = nx.read_graphml('Power Grid.graphml')

然后给出类似 G.nodes[<id>] 的内容,将列出所有节点属性。 下面是 ID 为 0 的节点的示例。访问节点时:

G.nodes['0']

它给了我们以下内容:

{'label': '0','Modularity Class': 3,'size': 10.0,'r': 0,'g': 0,'b': 0,'x': -445.68524,'y': 141.22151}

请注意该节点有一个名为'Modularity Class'的属性,即Gephi计算的_modularity类?。然后可以例如。迭代节点并访问模块化类,如下所示:

# Print the modularity class for each node
for u in G.nodes():
    print(G.nodes[u]['Modularity Class'])

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