知识图谱学习(一) py2neo
文章目录
- 一、python 与neo4j 数据库交互
- 1.创建图对象
- 2.创建数据对象
- Relationship
- query
- 匹配所有节点
- 匹配符合指定条件节点
- Update
- 修改单个节点
- 修改多个节点
- 两个节点新加关系
- 删除
- 删除关系链 delete
- 只删除关系 separate
- 批处理
- 创建多个节点
- 删除所有的关系
- 二、版本问题
- 三、参考链接
一、python 与neo4j 数据库交互
py2neo==4.3.0
1.创建图对象
from py2neo import Graph
'''
host:服务器ip地址,默认为'localhost'
http_port:http协议——服务器监听端口,默认7474
https_port:https协议——服务器监听端口,默认7473
user:登录用户名,默认'neo4j'
password:登录密码,无默认值,故若数据库其他参数都为默认值,则可直接通过密码登录
'''
graph = Graph('http://localhost:7474', auth=("neo4j", "123456"))
2.创建数据对象
Relationship
from py2neo import Graph, Node, Relationshipg = Graph('http://localhost:7474', username='neo4j', password='123456')
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
ab = Relationship(a, 'KNOWS', b)
g.create(ab)
query
# 新版
from py2neo import Graph, Node, Relationship, Subgraphg = Graph('http://localhost:7474', username='neo4j', password='123456')tx = g.begin()
jiazhen = Node("Person", name="陈家珍", age=66)
fugui = Node("Person", name='徐福贵', age=67)
youqian = Node("Person", name="徐有钱")
renxing = Node("Person", name="徐任性")cat = Node("Person", name='cat')
dog = Node("Person", name='dog')wife = Relationship(fugui, "WIFE", jiazhen)
brother_1 = Relationship(fugui, "BROTHER", youqian)
brother_2 = Relationship(fugui, "BROTHER", renxing)
hus = Relationship(jiazhen, 'HUS', fugui)
know = Relationship(cat, 'KNOWS', dog)relation_list = Subgraph(relationships=[wife, brother_2, brother_1, hus, know])tx.create(relation_list)
tx.commit()
匹配所有节点
from py2neo import Graph, Node, Relationshipg = Graph('http://localhost:7474', auth=("neo4j", "123456"))nodes = g.nodes.match()for node in nodes:print(node)
print(node.items())
print(node.labels)
匹配符合指定条件节点
from py2neo import Graph, Node, Relationship, NodeMatcherg = Graph('http://localhost:7474', auth=("neo4j", "123456"))# 用来查找节点的对象
matcher = NodeMatcher(g)# first 返回第一个符合条件的节点
# node1 = matcher.match('Person', name='徐有钱').first()
node1 = matcher.match('Person').where("_.name='徐有钱'").first()
print(node1)# all 返回所有符合条件的节点
nodes = matcher.match('Person')for node in nodes:print('姓名:%s \t 年龄:%s '% (node['name'], node['age']))nodes = matcher.match('Person').where("_.age=66")
print('*' * 25 + '年龄66的节点' + '*' * 25)
for node in nodes:print('姓名:%s \t 年龄:%s ' % (node['name'], node['age']))# 模糊匹配 要用Cypher
nodes = matcher.match("Person").where("_.name =~ '徐.*'")print('*' * 25 + '姓徐的节点' + '*' * 25)
for node in nodes:print('姓名:%s \t 年龄:%s ' % (node['name'], node['age']))
Update
py2neo==2020.1.0
本来想继续使用py2neo==4.3.0版本,无奈总是报错,针对该错误的情况的代码修改方式较少,更多的推荐是更改py2neo版本。
修改单个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraphg = Graph('http://localhost:7474', auth=("neo4j", "123456"))from py2neo import Graph, NodeMatchertx = g.begin()
# 找到你要找的Nodes
matcher = NodeMatcher(g)# 修改单个节点
init_node = matcher.match("Person").where('_.name="徐福贵"')
new_node = init_node.first()
new_node['name'] = "福贵"
sub = Subgraph(nodes=[new_node])
tx.push(sub)
tx.commit()
修改多个节点
# 修改多个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraphg = Graph('http://localhost:7474', auth=("neo4j", "123456"))from py2neo import Graph, NodeMatchertx = g.begin()
# 找到你要找的Nodes
matcher = NodeMatcher(g)init_node = matcher.match("Person")
new_nodes = []
for node in init_node.all():node['name'] = '⭐'+node['name']new_nodes.append(node)sub = Subgraph(nodes=new_nodes)
tx.push(sub)
tx.commit()
两个节点新加关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraphg = Graph('http://localhost:7474', auth=("neo4j", "123456"))matcher = NodeMatcher(g)fugui = matcher.match('Person', name='⭐福贵').first()
youqian = matcher.match('Person', name='⭐徐有钱').first()relation = Relationship(fugui, 'Brother', youqian)g.create(relation)
删除
删除关系链 delete
节点也会被删除
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcherg = Graph('http://localhost:7474', auth=("neo4j", "123456"))matcher = NodeMatcher(g)
r_matcher = RelationshipMatcher(g)
a = matcher.match('Person', name='⭐Alice').first()
b = matcher.match('Person', name='⭐Bob').first()relation = r_matcher.match(nodes=[a, b]).first() #也可以是none,表示任意节点。注意前后顺序
# nodes=[None,b] 表示所有以b为终点的关系
# nodes=[a,None] 表示所有以a为起点的关系
# nodes=[None,None] 表示所有节点的关系
print(relation)
g.delete(relation)
只删除关系 separate
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcherg = Graph('http://localhost:7474', auth=("neo4j", "123456"))matcher = NodeMatcher(g)
r_matcher = RelationshipMatcher(g)
cat = matcher.match('Person', name='⭐cat').first()
dog = matcher.match('Person', name='⭐dog').first()relation = r_matcher.match(nodes=[cat, dog]).first()
print(relation)
g.separate(relation)
批处理
创建多个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcherg = Graph('http://localhost:7474', auth=("neo4j", "123456"))tx = g.begin()
node_list = [Node("Num", name=str(i)) for i in range(4)]node_list = Subgraph(nodes=node_list)tx.create(node_list)
tx.commit()
删除所有的关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcherg = Graph('http://localhost:7474', auth=("neo4j", "123456"))
matcher = RelationshipMatcher(g)
tx = g.begin()
relationship_list = matcher.match().all()node_list = Subgraph(relationships=relationship_list)tx.separate(node_list)
tx.commit()
二、版本问题
Neo4j版本4.2.1,Python版本3.6 的条件下 py2neo4.3.0版本无法使用
NodeMatcher(graph).match().all()
py2neo4.0.0版本可使用NodeMatcher(graph).match().all(),但无法与当前版本的Neo4j建立连接
py2neo4.3.0版本可以使用
NodeMatcher(graph).match().first()
py2neo4.3.0版本无法使用
node1 = matcher.match('Person', name='徐有钱').first()
py2neo4.3.0版本可以使用
node1 = matcher.match('Person').where("_.name='徐有钱'").first()
py2neo最新版本无法直接访问Neo4j,情况同py2neo4.0.0版本近似 py2neo3.1.2版本支持Python3.5以下版本
因此推荐使用py2neo 2020.1.0版本
三、参考链接
python 与neo4j 数据库交互
neo4j基本使用及其Python语言操作
知识图谱学习(一) py2neo相关推荐
- 知识图谱学习笔记(1)
知识图谱学习笔记第一部分,包含RDF介绍,以及Jena RDF API使用 知识图谱的基石:RDF RDF(Resource Description Framework),即资源描述框架,其本质是一个 ...
- 0元包邮 | 知识图谱学习导图
知识图谱可以说是整个AI的未来,是实现人工智能从"感知"跃升到"认知"的基础.近年来,作为实现认知智能的核心驱动力,已广泛应用在金融.电商.医疗.政务等诸多领域 ...
- 知识图谱学习资料汇总
知识图谱学习资料汇总 持续更新中- 知识图谱介绍 (1)知识图谱入门笔记(参考王昊奋) 知乎:https://zhuanlan.zhihu.com/c_211846834 (2)Mining Know ...
- 知识图谱学习(二):电商知识图谱
知识图谱学习(二):电商知识图谱 --本文摘自机械工业出版社华章图书<阿里巴巴B2B电商算法实战>,参考文献请参见原书. 目录 知识图谱学习(二):电商知识图谱 前言 互联网创业潮 电商生 ...
- 知识图谱学习(笔记整理)
本篇来自于文章<从技术实现到产品落地,"知识图谱"的未来还有多远?> 知识图谱学习(一) 一.组成部分 知识图谱大致可分为:知识建模.知识获取.知识融合. 知识存储.知 ...
- 知识图谱学习(一)(笔记整理)
本篇来自于文章<从技术实现到产品落地,"知识图谱"的未来还有多远?> 知识图谱学习(一) 一.组成部分 知识图谱大致可分为:知识建模.知识获取.知识融合. 知识存储.知 ...
- 干货!针对知识图谱学习的高效超参搜索算法
点击蓝字 关注我们 AI TIME欢迎每一位AI爱好者的加入! 超参数调优对于知识图谱学习是一个重要问题,会严重影响模型性能,但由于模型训练时间长,现有的超参数搜索算法往往效率低下. 为解决这一问题, ...
- 知识图谱学习笔记-非结构化数据处理
非结构话数据到知识图谱 非结构数据-> 信息抽取(命名实体识别.关系抽取)-> 图谱构建(实体消歧.链接预测)-> 图分析算法 一.文本分析关键技术 拼写纠错 分词 词干提取 词的过 ...
- 知识图谱学习小组学习大纲
(这是为北京知识学习小组第一期 kgbj1 准备的为期4周的学习大纲) 2016年6月3日 鲍捷 这个学习小组的目的,不是按教科书的定义去学习"知识图谱",更不是做研究.我们更多是 ...
- 【论文翻译】统一知识图谱学习和建议:更好地理解用户偏好
一.摘要 将知识图谱(KG)纳入推荐系统有望提高推荐的准确性和可解释性.然而,现有方法主要假设KG是完整的并且简单地在实体原始数据或嵌入的浅层中转移KG中的"知识".这可能导致性能 ...
最新文章
- JAVA语言基础-面向对象(集合框架02List、泛型)
- 【经典】5种IO模型 | IO多路复用
- NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利
- nginx优化配置选项
- Android可滑动画板,Android 利用 Canvas 画画板
- php 调用图,php 缩略图类(附调用示例)
- 二维概率密度求解边缘密度
- SCN和Checkpoint
- IKVM.NET_第二篇_应用
- CSDN中Markdown格式(编辑器)语法及其使用
- 区块链百科合集之 账 户 体 系
- 如何转换图片格式?建议收藏这两个方法
- PLC1200 模拟量采集
- 双击 文字 出现 文本框 的方法
- 【每周CV论文推荐】初学视觉注意力机制有哪些值得阅读的论文?
- 一文了解复旦大学NLP实验室的14篇EMNLP 2022长文内容
- 计算机网络技术中,分组交换技术在计算机网络技术中的作用及特点是什么?
- pdf工具类之添加页码
- 16.Hamilton(哈密顿)回路问题
- 企业级日志平台新秀!比 ELK 更轻量、高效
热门文章
- 计算机桌面底边出现库如何去掉,桌面图标有蓝底怎么去掉? 去掉桌面图标阴影技巧...
- python四级考试时间_2016年四六级英语考试注意事项四六级考试建议
- python实现最小二乘法进行线性拟合
- 图像超分辨率重建原理学习
- 缺氧游戏 不给计算机加水,缺氧 泥土用完了怎么办 | 手游网游页游攻略大全
- 【计算视觉】人体姿态识别研究综述(详细归纳!)
- txt 文本文档中空格替换
- django oscar_赢得奥斯卡奖之后会发生什么
- 关于在word中安装Mathtype
- php5.4.45连接mssql2000,用php在linux下连接mssql2000(转)