Training GDS Similarity - tomgeudens/practical-neo4j GitHub Wiki

Context: Cut-and-paste commands for the Graph Data Science - Similarity algorithms session.

Prerequisite: This document will assume you have a Neo4j instance running and are connected to it with the Neo4j Browser. You also need to have the Game of Thrones database loaded.

Setup

001

// Make sure you're in the correct database
:use gameofthrones

Jaccard

002

// Simply Jaccard (try it with some different values)
RETURN gds.alpha.similarity.jaccard([1,2,3],[1,2,2,3]);

nodeSimilarity

003

// Throw bipartites in the pot
CALL gds.graph.create ('person-bipartites', ['Person', 'Book', 'House', 'Culture'], '*');

004

// Bring to room temperature (about 20 degrees Celsius)
CALL gds.nodeSimilarity.stream('person-bipartites',{
  degreeCutoff: 20,
  similarityCutoff: 0.45})
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name as character1, gds.util.asNode(node2).name as character2, similarity
ORDER BY similarity DESC;

005

// Degree of a node
MATCH (x:Person {name: "Gregor Clegane"})
RETURN apoc.node.degree(x), apoc.node.degree.in(x), apoc.node.degree.out(x);

006

// Better than a LIMIT
CALL gds.nodeSimilarity.stream('person-bipartites',{
  degreeCutoff: 20, topN: 10, topK: 1}) YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name as character1, gds.util.asNode(node2).name as character2, similarity
ORDER BY similarity DESC;

007

// Cleanup possible relics
MATCH (:Person)-[y:SIMILAR]->(:Person) DELETE y;

008

// Writing back
CALL gds.nodeSimilarity.write('person-bipartites',{
  degreeCutoff: 20,topN: 10, topK: 1,
  writeRelationshipType: 'SIMILAR',
  writeProperty: 'strength'});

009

// Verify the result
MATCH sg=(:Person {name: "Gregor Clegane"})-[:SIMILAR]-() RETURN sg;