9951 explained code solutions for 126 technologies


python-numpyHow to calculate percentile from Numpy array


import numpy as np
arr = np.array([1,2,3,6,7,8,10,11,12,15,16,17])

p10 = np.percentile(arr, 10)
p50 = np.percentile(arr, 50)
p95 = np.percentile(arr, 95)ctrl + c
import numpy as np

load Numpy module for Python

np.array

declare Numpy array

np.percentile

calculate percentile of a given array on a given level

p10

10% percentile

p50

50% percentile (same as average)

p95

95% percentile


Usage example

import numpy as np
arr = np.array([1,2,3,6,7,8,10,11,12,15,16,17])
val = np.quantile(arr,.05)

p10 = np.percentile(arr, 10)
p50 = np.percentile(arr, 50)
p95 = np.percentile(arr, 95)

print('10%', p10)
print('50%', p50)
print('95%', p95)
output
10% 2.1
50% 9.0
95% 16.45