How do I calculate percentiles with python/numpy?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Question
00:35 Accepted answer (Score 378)
01:00 Answer 2 (Score 88)
01:46 Answer 3 (Score 37)
01:57 Answer 4 (Score 29)
02:50 Thank you
--
Full question
https://stackoverflow.com/questions/2374...
Accepted answer links:
[SciPy Stats]: http://docs.scipy.org/doc/scipy/referenc...
[the percentile function]: http://docs.scipy.org/doc/scipy/referenc...
[is available]: http://docs.scipy.org/doc/numpy/referenc...
[This ticket]: http://projects.scipy.org/numpy/ticket/6...
Answer 2 links:
[a pure-Python implementation of percentile function]: http://code.activestate.com/recipes/5114.../
Answer 4 links:
[quantiles]: https://docs.python.org/3.8/library/stat...
[statistics]: https://docs.python.org/3.8/library/stat...
[quantiles]: https://docs.python.org/3.8/library/stat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #statistics #numpyndarray #percentile
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Question
00:35 Accepted answer (Score 378)
01:00 Answer 2 (Score 88)
01:46 Answer 3 (Score 37)
01:57 Answer 4 (Score 29)
02:50 Thank you
--
Full question
https://stackoverflow.com/questions/2374...
Accepted answer links:
[SciPy Stats]: http://docs.scipy.org/doc/scipy/referenc...
[the percentile function]: http://docs.scipy.org/doc/scipy/referenc...
[is available]: http://docs.scipy.org/doc/numpy/referenc...
[This ticket]: http://projects.scipy.org/numpy/ticket/6...
Answer 2 links:
[a pure-Python implementation of percentile function]: http://code.activestate.com/recipes/5114.../
Answer 4 links:
[quantiles]: https://docs.python.org/3.8/library/stat...
[statistics]: https://docs.python.org/3.8/library/stat...
[quantiles]: https://docs.python.org/3.8/library/stat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #statistics #numpyndarray #percentile
#avk47
ACCEPTED ANSWER
Score 396
NumPy has np.percentile().
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) # return 50th percentile, i.e. median.
>>> print(p)
3.0
SciPy has scipy.stats.scoreatpercentile(), in addition to many other statistical goodies.
ANSWER 2
Score 92
By the way, there is a pure-Python implementation of percentile function, in case one doesn't want to depend on scipy. The function is copied below:
## {{{ http://code.activestate.com/recipes/511478/ (r1)
import math
import functools
def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values.
@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - optional key function to compute value from each element of N.
@return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
# median is 50th percentile.
median = functools.partial(percentile, percent=0.5)
## end of http://code.activestate.com/recipes/511478/ }}}
ANSWER 3
Score 37
import numpy as np
a = [154, 400, 1124, 82, 94, 108]
print np.percentile(a,95) # gives the 95th percentile
ANSWER 4
Score 29
Here's how to do it without numpy, using only python to calculate the percentile.
import math
def percentile(data, perc: int):
size = len(data)
return sorted(data)[int(math.ceil((size * perc) / 100)) - 1]
percentile([10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], 90)
# 9.0
percentile([142, 232, 290, 120, 274, 123, 146, 113, 272, 119, 124, 277, 207], 50)
# 146