Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-11-2021 09:52 PM
I want to split the graph into two sets, train and test. For each node, add an additional node label, either 'train' or 'test'. For example, 1/3 goes to 'test' and 2/3 goes to 'train'. Is is possible to do this with a cypher in neo4j?
MATCH (n)
WITH count(n) as total
UNWIND range(1,total) as range
WITH toInteger(rand() * (total - 1 + 1)) + 1 as randomID
WHERE ID(n) = randomID
SET n:Test
It should be something like this. The total needs to be divided into a test and train based on the 1:2 ratio.
Solved! Go to Solution.
05-12-2021 05:40 PM
We can do this, but it will be a little easier if we leverage APOC Procedures so we can add labels dynamically.
MATCH (n)
WITH n, rand() * 3 as random
WITH n, CASE WHEN random < 1 THEN 'Test' ELSE 'Train' END as label
CALL apoc.create.addLabels([n], [label]) YIELD node
RETURN count(*)
05-12-2021 05:40 PM
We can do this, but it will be a little easier if we leverage APOC Procedures so we can add labels dynamically.
MATCH (n)
WITH n, rand() * 3 as random
WITH n, CASE WHEN random < 1 THEN 'Test' ELSE 'Train' END as label
CALL apoc.create.addLabels([n], [label]) YIELD node
RETURN count(*)
05-12-2021 08:52 PM
That's really a smart way to do this task!
All the sessions of the conference are now available online