cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Calculating Quantiles in cypher

kashu94
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?

View solution in original post

5 REPLIES 5

amina
Node Link

Hi,

Please,have a look at this page about apoc.agg.percentiles function: https://neo4j.com/labs/apoc/4.4/overview/apoc.agg/apoc.agg.percentiles/

kashu94
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?

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

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