IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> Neo4j 实战篇(一)基于Py2Neo构建电影知识图谱 -> 正文阅读

[人工智能]Neo4j 实战篇(一)基于Py2Neo构建电影知识图谱

运行环境


  • windows11
  • JDK 8
  • anaconda3
  • python 3.9
  • Neo4j 3.5.32
  • python jupyter库
  • py2neo
  • Visual Studio Code 2022

项目地址:
Gitee : https://gitee.com/ccuni/py2neo-neo4j-actual-combat

GitHub:https://github.com/unirithe/py2neo-neo4j-actual-combat

一、数据集说明

数据集来自 IMDB 影视网的电影、演员数据,数据并不全,仅供学习参考。
数据集参考上方的 Gitee 或 GitHub地址

  • movie_act.csv 演员id到电影id的映射信息

  • 在这里插入图片描述

  • movie_actor.csv 5334个演员的信息,名称和头像
    在这里插入图片描述

  • movie_moive.csv 2926部电影的详情信息
    在这里插入图片描述

  • movie_popularity.csv 保留着62部受欢迎的电影信息
    在这里插入图片描述

  • user_user.csv 不知道有啥用的id信息
    在这里插入图片描述

二、数据预处理

这里将原先的csv数据转为 pandas的DataFrame后再转化成字典,从而能构建Node对象,插入到Neo4j中

2.1 选择受欢迎的电影

list_mid = df['popularity']['movieid_id']

# 查找受欢迎的电影信息
# Find the movies which is popularity 
df_popularity_movie = df['movie'][df['movie']['movieid'].isin(list_mid)]
df_popularity_movie

在这里插入图片描述

# 将DataFrame格式转化为dict,到时候方便插入Neo4j
# make DataFrame to Dict, in order to insert neo4j
dict_movie = {}

for i in range(len(df_popularity_movie)):
    row = df_popularity_movie.iloc[i]
    dict_movie.update({row['movieid'] : row.to_dict()})
print('rows: ' , len(dict_movie))

在这里插入图片描述

2.2 查找每部受欢迎电影的所有演员

dict_actor_movie = {}
for mid in df_popularity_movie['movieid']:
    flag = df['actor_movie']['movieid_id'].eq(mid)
    actors = df['actor_movie'][flag]['actorid_id'].to_list()
    dict_actor_movie.update({mid : actors})
print('rows: ' , len(dict_actor_movie))

在这里插入图片描述

2.3 查找热门电影里每个演员的信息

dict_actor = {}
actors = set()
for ac in dict_actor_movie.values():
    for actor in ac:
        actors.add(actor)
for aid in actors:
    flag = (df['actor']['actorid'] == aid)
    row = df['actor'][flag].iloc[0]
    dict_actor.update({aid: row.to_dict()})
print('rows: ' , len(dict_actor_movie))

在这里插入图片描述

三、Py2Neo 操作

3.1 连接 Neo4j

from py2neo import Graph
url = "http://localhost:7474"
username = "neo4j"
password = "123456"
graph = Graph(url, auth=(username, password))
print("neo4j info: {}".format(str(graph)))

输出结果:neo4j info: Graph(‘http://localhost:7474’)

3.2 插入电影和演员节点

from py2neo import Graph, Node, Subgraph
import time 
s_time = time.time()

node_list = []

graph.delete_all()

for mid, movie in dict_movie.items():
    node_list.append(Node("movie", **movie))
for aid, actor in dict_actor.items():
    node_list.append(Node("actor", **actor))
graph.create(subgraph=Subgraph(node_list))

# 查看一个节点
print(node_list[0])

在这里插入图片描述

输出当前Neo4j 电影和演员节点的个数

print('movie: ', graph.nodes.match("movie").count())
print('actor: ', graph.nodes.match('actor').count())

输出结果:
movie: 62
actor: 240

3.3 建立电影和演员之间的联系

from py2neo import NodeMatcher, Relationship
node_matcher = NodeMatcher(graph)
list_rel = []
for mid, actors in dict_actor_movie.items(): 
    node_movie = node_matcher.match("movie", movieid=mid).first()
    if node_movie != None:
        for actor in actors:
            node_actor = node_matcher.match("actor", actorid=actor).first()
            if node_actor != None:
                list_rel.append(Relationship(node_actor, "acted", node_movie, name='acted'))

# 批量建立
# batch build 
once = 50
maxi = len(list_rel)
for i in range(0, maxi, once):
    subgraph = Subgraph(relationships=list_rel[i:i+once])
    graph.separate(subgraph)
    graph.create(subgraph)
    print(f'[INFO] >> created {len(subgraph)} relations')

输出结果:
在这里插入图片描述

登录 Neo4j 的web页面查询插入的结果:http://localhost:7474

`

四、基于Neo4j的数据分析

4.1 查找电影的所有关系

from py2neo import RelationshipMatcher
rmatcher = RelationshipMatcher(graph)

i = 0
for node_movie in graph.nodes.match('movie').all():
    print(i, '-' * 10 , node_movie['name'] + '-' *10)
    for rel in graph.match([None, node_movie]).all():
        print('--', rel)
    i += 1
    print('\n\n')

部分输出结果:(共有62部受欢迎的电影)
在这里插入图片描述

4.2 查找根据演员数和评分排序的Top10电影

nodes_movie = graph.nodes.match('movie').all()

#关于已出现的演员人数的前十名
# Top10 about the number of appeared actor  

# 如果演员人数一致就根据评分判断,选择评分高的电影
# If the number of actors is the same,
#   judge according to the score and choose the film with high rate.
rm = RelationshipMatcher(graph)
'''
    Top10
'''
dict_movie_top10 = {}
for node_movie in nodes_movie:
    list_actors = rm.match([None, node_movie], r_type='acted').all()
    count = len(list_actors)
    dict_movie_top10.update({node_movie: {'count':int(count), 'actors':list_actors}})

list_movie_top10 = sorted(dict_movie_top10.items(), 
        key = lambda k : (k[1]['count'], float(k[0]['rate'])), reverse=True)[:10]

# list_movie_top10 is a list([turple(Node, dict)])
print('------------------ Top10 ------------------')
for node_movie, dict_count in list_movie_top10:
    print(dict_count['count'], node_movie['rate'], node_movie['name'])

输出结果:
在这里插入图片描述

翻译过后:
Translate to chinese

排名评分电影名称
19.1《肖申克的救赎》
29.1《Dekalog》
39.0《黑暗骑士》
49.0《教父:第二部》
58.9《低俗小说》
68.8《费城总是阳光明媚》
78.8《星球大战5:帝国反击战》
88.8《搏击俱乐部》
98.7《指环王:双塔奇兵》
108.6《星球大战》

4.3 保存 Top10数据到 Neo4j

graph.delete(Subgraph(graph.nodes.match('actor_top10').all()))
graph.delete(Subgraph(graph.nodes.match('movie_top10').all()))
graph.delete(Subgraph(RelationshipMatcher(graph).match(name='acted_top10')))

rel_top10 = []
nodeMatcher = NodeMatcher(graph)
for node_movie, dict_count in list_movie_top10:
    for actor_rel in dict_count['actors']:

        actor = Node('actor_top10', **dict(actor_rel.start_node))
        movie = Node('movie_top10', **dict(node_movie))

        actor_find = nodeMatcher.match('actor_top10', name=actor['name']).first()
        movie_find = nodeMatcher.match('movie_top10', name=movie['name']).first()
        if actor_find != None: ator = actor_find 
        if movie_find != None: movie = movie_find
        
        rel_top10.append(Relationship(actor, "acted", movie, name='acted_top10'))
        sub_rels=Subgraph(relationships=rel_top10)
        graph.separate(subgraph=sub_rels)
        graph.create(subgraph = sub_rels)

print('The number of actor_top10 node: ',graph.nodes.match('actor_top10').count())
print('The number of moive_top10 node: ', graph.nodes.match('movie_top10').count())
print('The number of relationsip: ', graph.relationships.match(name='acted_top10').count())

输出结果:
在这里插入图片描述

从 web中查询的结果如下:
在这里插入图片描述

五、总结

通过本次的尝试,我们使用py2neo进行了Neo4j的增删改查,熟悉使用 Node、Relationship、Graph,另外,还有大量的 pandas相关的操作。最终分析了影视电影和演员之间的关系,当然还有更多指标可以分析,比如:出现次数最多的演员以及电影、同步出现率最高的电影等等。

py2neo实现neo4j的增删改查还是挺轻松的。

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章           查看所有文章
加:2022-05-21 18:58:10  更:2022-05-21 19:02:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/16 22:43:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码