Python中怎样用networkx画多节点的复制人物关系图
Admin 2022-09-13 群英技术资讯 1446 次浏览
今天这篇我们来学习和了解“Python中怎样用networkx画多节点的复制人物关系图”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“Python中怎样用networkx画多节点的复制人物关系图”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!《悲惨世界》中的人物关系图,图中共77个节点、254条边。
数据集截图:

打开README文件:
Les Misérables network, part of the Koblenz Network Collection
===========================================================================
This directory contains the TSV and related files of the moreno_lesmis network: This undirected network contains co-occurances of characters in Victor Hugo's novel 'Les Misérables'. A node represents a character and an edge between two nodes shows that these two characters appeared in the same chapter of the the book. The weight of each link indicates how often such a co-appearance occured.
More information about the network is provided here:
http://konect.cc/networks/moreno_lesmis
Files:
meta.moreno_lesmis -- Metadata about the network
out.moreno_lesmis -- The adjacency matrix of the network in whitespace-separated values format, with one edge per line
The meaning of the columns in out.moreno_lesmis are:
First column: ID of from node
Second column: ID of to node
Third column (if present): weight or multiplicity of edge
Fourth column (if present): timestamp of edges Unix time
Third column: edge weight
Use the following References for citation:
@MISC{konect:2017:moreno_lesmis,
title = {Les Misérables network dataset -- {KONECT}},
month = oct,
year = {2017},
url = {http://konect.cc/networks/moreno_lesmis}
}
@book{konect:knuth1993,
title = {The {Stanford} {GraphBase}: A Platform for Combinatorial Computing},
author = {Knuth, Donald Ervin},
volume = {37},
year = {1993},
publisher = {Addison-Wesley Reading},
}
@book{konect:knuth1993,
title = {The {Stanford} {GraphBase}: A Platform for Combinatorial Computing},
author = {Knuth, Donald Ervin},
volume = {37},
year = {1993},
publisher = {Addison-Wesley Reading},
}
@inproceedings{konect,
title = {{KONECT} -- {The} {Koblenz} {Network} {Collection}},
author = {Jérôme Kunegis},
year = {2013},
booktitle = {Proc. Int. Conf. on World Wide Web Companion},
pages = {1343--1350},
url = {http://dl.acm.org/citation.cfm?id=2488173},
url_presentation = {https://www.slideshare.net/kunegis/presentationwow},
url_web = {http://konect.cc/},
url_citations = {https://scholar.google.com/scholar?cites=7174338004474749050},
}
@inproceedings{konect,
title = {{KONECT} -- {The} {Koblenz} {Network} {Collection}},
author = {Jérôme Kunegis},
year = {2013},
booktitle = {Proc. Int. Conf. on World Wide Web Companion},
pages = {1343--1350},
url = {http://dl.acm.org/citation.cfm?id=2488173},
url_presentation = {https://www.slideshare.net/kunegis/presentationwow},
url_web = {http://konect.cc/},
url_citations = {https://scholar.google.com/scholar?cites=7174338004474749050},
}
从中可以得知:该图是一个无向图,节点表示《悲惨世界》中的人物,两个节点之间的边表示这两个人物出现在书的同一章,边的权重表示两个人物(节点)出现在同一章中的频率。
真正的数据在out.moreno_lesmis_lesmis中,打开并另存为csv文件:

networkx中对无向图的初始化代码为:
g = nx.Graph()
g.add_nodes_from([i for i in range(1, 78)])
g.add_edges_from([(1, 2, {'weight': 1})])
节点的初始化很容易解决,我们主要解决边的初始化:先将dataframe转为列表,然后将其中每个元素转为元组。
df = pd.read_csv('out.csv')
res = df.values.tolist()
for i in range(len(res)):
res[i][2] = dict({'weight': res[i][2]})
res = [tuple(x) for x in res]
print(res)
res输出如下(部分):
[(1, 2, {'weight': 1}), (2, 3, {'weight': 8}), (2, 4, {'weight': 10}), (2, 5, {'weight': 1}), (2, 6, {'weight': 1}), (2, 7, {'weight': 1}), (2, 8, {'weight': 1})...]
因此图的初始化代码为:
g = nx.Graph() g.add_nodes_from([i for i in range(1, 78)]) g.add_edges_from(res)
nx.draw(g) plt.show()

忙活了半天发现networkx有自带的数据集,其中就有悲惨世界的人物关系图:
g = nx.les_miserables_graph() nx.draw(g, with_labels=True) plt.show()

# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
# 77 254
df = pd.read_csv('out.csv')
res = df.values.tolist()
for i in range(len(res)):
res[i][2] = dict({'weight': res[i][2]})
res = [tuple(x) for x in res]
print(res)
# 初始化图
g = nx.Graph()
g.add_nodes_from([i for i in range(1, 78)])
g.add_edges_from(res)
g = nx.les_miserables_graph()
nx.draw(g, with_labels=True)
plt.show()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
路径字符串磁盘将数据写入各个扇区中,使数据分散在各个未使用的块上来储存数据。并用一套文件系统对数据进行管理,微观上,文件系统使用INode结构体来记录这些块区的地址以及数据的先后顺序,实现对这些乱序储存的数据管理。宏观上,为了方便用户的管理,使用路径字符串的方式来“描述”文件的位置,但这只是一种逻辑位置,并不存在此种数据上的层级关系,一个文件字符串在将会映射到一个的INode结构体中,再使用iN
K-Means是聚类算法的一种,以距离来判断数据点间的相似度并对数据进行聚类,下面这篇文章主要给大家介绍了关于如何利用python实现kmeans聚类的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
python中的not和is none有何不同?很多新手刚解决python时,对于not和is none的用法区别不是很了解,对此,这篇文章就主要给大家介绍一下python中的not和is none。
这几天公司一个项目用到了一个技术点,需要判断图片是不是空白,后面经过思考,空白图片不就是一个纯色图片吗?所有判断空白图片只需要判断是不是纯色图片,刚好pillow模块有一个功能判断图片的,下面直接上代码:
本篇文章给大家带来了关于Python的相关知识,主要介绍了python中namedtuple函数的用法解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008