【复杂网络分析】基于《哈利波特》人物关系做复杂网络分析

在这里插入图片描述

基于《哈利波特与魔法石》这部电影中的主要角色构建一张关系图

用python的networkx包画出关系图。

  1. 首先需要安装networkxmatplotlib库(如果没有安装的话):

    • 在命令行中执行pip install networkx matplotlib
  2. 以下是使用networkxmatplotlib绘制《哈利·波特与魔法石》主要角色关系图的
    这段代码首先创建了一个有向图G,然后添加了主要角色作为节点,接着根据角色之间的关系添加了边,最后使用spring_layout布局算法来确定节点位置,并绘制出关系图。

import networkx as nx
import matplotlib.pyplot as plt


# 创建有向图
G = nx.DiGraph()

# 添加角色节点
characters = ["Harry Potter", "Ron Weasley", "Hermione Granger", "Albus Dumbledore", "Rubeus Hagrid",
              "Voldemort", "Professor Quirrell", "Fred and George Weasley", "Ginny Weasley",
              "Molly and Arthur Weasley", "Neville Longbottom", "Minerva McGonagall", "Severus Snape"]
for character in characters:
    G.add_node(character)

# 添加边(关系)
# 朋友关系用双向箭头
G.add_edge("Harry Potter", "Ron Weasley")
G.add_edge("Ron Weasley", "Harry Potter")
G.add_edge("Harry Potter", "Hermione Granger")
G.add_edge("Hermione Granger", "Harry Potter")
# 非朋友关系用单向箭头
G.add_edge("Harry Potter", "Albus Dumbledore")
G.add_edge("Harry Potter", "Rubeus Hagrid")
G.add_edge("Voldemort", "Harry Potter")
G.add_edge("Voldemort", "Professor Quirrell")
G.add_edge("Ron Weasley", "Fred and George Weasley")
G.add_edge("Ron Weasley", "Ginny Weasley")
G.add_edge("Ron Weasley", "Molly and Arthur Weasley")
G.add_edge("Hermione Granger", "Neville Longbottom")
G.add_edge("Harry Potter", "Minerva McGonagall")
G.add_edge("Harry Potter", "Severus Snape")


# 绘制图形
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_size = 1000, node_color = "skyblue", font_size = 8)
plt.title("Characters Relationship in Harry Potter and the Sorcerer's Stone")
plt.show()
  1. 代码解释:
    • 对于朋友关系(如哈利和罗恩、哈利和赫敏),通过添加双向边来表示。
    • 对于其他如导师、亲属、敌人等关系,保持单向边的添加方式。
    • 最后按照相同的方式绘制图形。

计算一下Harry Potter这个节点的centrality betweenness是多少

  1. 首先需要导入networkx库来计算介数中心性(Betweenness Centrality)。以下是在之前代码基础上添加计算介数中心性的步骤:
根据提供的引用内容,我们可以了解到《哈利波特》是一部关于哈利、赫敏、罗恩等人在大法师邓布利多的帮助下,使用魔法抵抗伏地魔的故事。同时,根据引用和引用,我们可以使用Python对小说中的人物名字和出现频率进行统计和分析。 以下是Python代码示例: 1. 统计人物名字TOP20的词语 ```python import jieba import pandas as pd from collections import Counter from pyecharts import Bar # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计人物名字出现的次数 names = ['哈利', '赫敏', '罗恩', '邓布利多', '马尔福', '斯内普', '小天狼星'] names_count = Counter([word for word in words if word in names]) # 绘制柱状图 bar = Bar('主要人物Top20', background_color='white', title_pos='center', title_text_size=20) x = names_count.most_common(20) bar.add('', [i[0] for i in x], [i[1] for i in x], xaxis_interval=0, xaxis_rotate=30, is_label_show=True) bar.render() ``` 2. 统计整部小说出现最多的词语TOP15 ```python import jieba import pandas as pd from collections import Counter # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计词语出现的次数 words_count = Counter(words) # 去除停用词 stopwords = pd.read_csv('stopwords.txt', index_col=False, quoting=3, sep='\t', names=['stopword'], encoding='utf-8') words = [word for word in words if word not in stopwords] # 统计出现最多的词语TOP15 top15 = words_count.most_common(15) print(top15) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AndrewHZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值