本文共 1725 字,大约阅读时间需要 5 分钟。
使用Neo4j进行三元关系数据查询,基于关联规则提取满足3人及3案有关系的集合。以下是优化后的技术说明:
graph = Graph(host="192.168.3.186://7474", auth=("neo4j", "wabjtam123"))data = graph.run('match data=(p:anjian)-[:参与 *2..5]-(u:anjian) with p,collect(distinct u.AJBH) as ajbhs match (p:anjian)-[:参与 *1..5]-(c:xyr) with p,ajbhs, collect(distinct c.XYRBH) as xyrbh_list,(ajbhs+p.AJBH) as ajbh_list where size(xyrbh_list)>2 and size(ajbh_list)>2 return p.AJBH as ajbh,ajbh_list,xyrbh_list')aj = []xyr_result = []aj_result = []for node in data: ajbh = node.get('ajbh') ajbh_list = node.get('ajbh_list') xyrbh_list = node.get('xyrbh_list') # 去重处理 ajbh_list = list(set(ajbh_list)) xyrbh_list = list(set(xyrbh_list)) flag = True if len(aj_result) > 0: for val in ajbh_list: if find(val, aj_result): flag = False break if flag: if len(ajbh_list) > 2 and len(xyrbh_list) > 2: aj.append(ajbh) aj_result.append(ajbh_list) xyr_result.append(xyrbh_list) print(f'匹配结果:{len(xyrbh_list)}人{len(ajbh_list)}案') print(f'当前总数:{len(aj_result)}') print(f'当前项目:{aj}') print('----------------------------') print(aj_result) print('----------------------------') print(aj_result) conn = psycopg2.connect(database="zfgfh", user="postgres", password="postgres", host="192.168.3.202", port="5432")cursor = conn.cursor()for ajbh in aj_result: for temp in ajbh: insert_sql = "insert into test.gm (\"ajbh\") values ('%s')" % temp print(insert_sql) cursor.execute(insert_sql)conn.commit()cursor.close() 以上代码实现了三元关系数据的关联规则挖掘,通过Cypher查询从数据库中提取满足3人3案关联关系的三元组。数据处理流程包括去重、存储以及最终插入数据库的操作。
转载地址:http://qtofk.baihongyu.com/