cancel
Showing results forΒ
Did you mean:Β

## Calculating Quantiles in cypher

Node Clone

I wanted to calculate quantile values over a list of integers. In python, there is Numpy library which does that as follows:

import numpy
list = [4, 6, 1, 3, 10, 5]
q3 = numpy.quantile(list, 0.75) #5.75
q2 = numpy.quantile(list, 0.50) #4.5
q1 = numpy.quantile(list, 0.25) #3.25

How would I be able to do this in cypher?

1 ACCEPTED SOLUTION

Hi,

in my case, the list is the different values of annot.value returned by a query. annot is a node that has a property named 'value' with a float type.

``````match (:Search{searchId:"4e69-9ebd-b02e1b0c0d49"})-[:FIRST]->(:SearchSession)-[:ANNOTATION]->(annot:FigureAnnotation)
RETURN apoc.agg.percentiles(annot.value, [0.25, 0.5, 0.75, 1.0]) AS percentiles;``````

perhaps you can make a similar query?

5 REPLIES 5

Hi,

Node Clone

Yes, I've come across that, but I'm not sure how to use them for a list ....

Hi,

in my case, the list is the different values of annot.value returned by a query. annot is a node that has a property named 'value' with a float type.

``````match (:Search{searchId:"4e69-9ebd-b02e1b0c0d49"})-[:FIRST]->(:SearchSession)-[:ANNOTATION]->(annot:FigureAnnotation)
RETURN apoc.agg.percentiles(annot.value, [0.25, 0.5, 0.75, 1.0]) AS percentiles;``````

perhaps you can make a similar query?

Node Clone

Thanks, I did it this way and it worked ...

``````WITH [4, 6, 1, 3, 10, 5] as list
UNWIND list as l
RETURN apoc.agg.percentiles(l, [0.25, 0.5, 0.75, 1.0]) AS percentiles``````
Ninja

There are also the cypher aggregate functions if you donβt want to use the apoc library.

https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-percentilecont

Nodes 2022

NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online

Neo4j Resources