数据集介绍

《悲惨世界》中的人物关系图,图中共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_lesmisFiles: 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 lineThe meaning of the columns in out.moreno_lesmis are: First column: ID of from node Second column: ID of to nodeThird column (if present): weight or multiplicity of edgeFourth column (if present):  timestamp of edges Unix timeThird column: edge weightUse 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自带的数据集

忙活了半天发现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()

Python处理Les Misérables network并利用networkx画图相关推荐

  1. python下的复杂网络编程包networkx的使用(摘抄)

    原文:http://blog.sciencenet.cn/home.php?mod=space&uid=404069&do=blog&classid=141080&vi ...

  2. python爬百度翻译-Python爬取百度翻译(利用json提取数据)

    本篇文章给大家带来的内容是关于Python爬取百度翻译(利用json提取数据),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 工具:Python 3.6.5.PyCharm开发工具. ...

  3. Python+NetworkX画图的nx.draw_networkx(函数详解)

    Python+NetworkX画图的nx.draw_networkx函数详解 Python+NetworkX画图的nx.draw_networkx(函数详解) Python+NetworkX画图的nx ...

  4. Python实现大学绩点计算——利用pandas对excel处理

    Python实现大学绩点计算--利用pandas对excel处理 pandas处理excel pandas读取excel pandas创建新的excel 编写代码计算大学绩点 更新时间 pandas处 ...

  5. python用户输入列表有缺陷_利用Python对Jira缺陷进行管理操作

    转自:51testing 1.1.概要 本文主要内容是教大家学习如何利用Python对Jira缺陷进行管理操作. 在平时的测试中,大家对于缺陷管理平台一定不陌生,平时的缺陷都要去平台进行操作.今天我们 ...

  6. 【python渗透测试】python在渗透测试中的利用(完全版,持续中出)

    跟着gpt学习python在渗透测试中的利用

  7. python的论文图表_如何利用Python绘制学术论文图表方法

    论文中图表形式多样,常用的处理工具有excel.MATLAB以及Python等,excel自处理的方法有两个缺陷: 1.当数据较多时,容易出现excel"翻白眼"的现象: 2.需要 ...

  8. networkx画图(番外)——(1)自定义节点布局

    networkx画图(番外)--(1)自定义节点布局 networkx虽然非常方便,但在一些超大规模的图数据上,依然显得吃力.所以大多数时候,它仅仅是被用来做一些实例性的分析和可视化展示的,这需要学会 ...

  9. maltab利用plot画图后更改线条颜色

    在matlab中,利用plot画图默认好像只有八种颜色,分别为:b--蓝色,r--红色,m--品红,k--黑色,g--绿色,c--青色,y--黄色,w--白色.有时候我们画的线条比较多,这几种颜色可能 ...

最新文章

  1. 2022-2028年中国模胚行业市场研究及前瞻分析报告
  2. python每行输出8个式子_求大神用python写出算术题的式子和结果。
  3. 7-35 蒙特卡罗方法求圆周率 (30 分)
  4. IPFS的相关操作命令
  5. Spring框架中的单例Bean是线程安全的吗?
  6. JavaScript大杂烩4 - 理解JavaScript对象的继承机制
  7. MapReduce程序之数据排序
  8. 【收藏】机器学习数据集列表:你需要收藏!
  9. Dubbo的负载均衡、集群容错、服务降级等机制详解
  10. 2018百度之星资格赛:1002:子串查询
  11. 第一次作业+105032014142
  12. NXP LPC芯片程序下载
  13. python求excel的最大行_解决使用Pandas 读取超过65536行的Excel文件问题
  14. 在北京租房 舒舍的租客素质怎么样?
  15. JS正则表达式匹配域名
  16. html2canvas黑背景,解决canvas转base64/jpeg时透明区域变成黑色背景的方法
  17. 软件测试团队口号及队名,团队队名口号(精选50句)
  18. 信号与系统学习笔记(大纲)
  19. 报表平台——体系架构
  20. spark2.2以后版本任务调度将增加黑名单机制

热门文章

  1. 罗斯蒙特阿牛巴流量计485G200ZCUPS2T100T3
  2. java MVC英语文献_外文文献: MVC简介Introduction to MVC.pdf
  3. 登陆页面登录成功之后的操作
  4. 前端vue/h5下载Springboot后端静态资源(txt/word/excel)
  5. PPT 课程 (631配色原则)
  6. BUU misc 小明的保险箱
  7. 色彩构成与搭配——色彩的原理及分类
  8. 【Python Brain】SNN脉冲神经网络开源模拟器 The Brian Simulator 仿真指南
  9. IT外包行业到底需要什么样的老板?
  10. Learning Python Part II 之 赋值语句(Assignments)