Step-by-step calculation of Numpy percentile manually:
Step-1: Find length
x = [1,2,3,4,5,6,7,8,9,10]
l = len(x)
# Output --> 10
Step-2: Subtract 1
to get distance from first to last item in x
# n = (length - 1)
# n = (10-1)
# Output --> 9
Step-3: Multiply n
by quantile, here 25th percentile or 0.25 quantile or 1st quartile
n * 0.25
# Therefore, (9 * 0.25)
# Output --> 2.25
# So, fraction is 0.25 part of 2.25
# m = 0.25
Step-4: Now get final answer
For Linear:
# i + (j - i) * m
# Here, think i and j as values at indices
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, for '2.25':
# value at index immediately before 2.25, is at index=2 so, i=3
# value at index immediately after 2.25, is at index=3 so, i=4
# and fractions
3 + (4 - 3)*0.25
# Output --> 3.25
For Lower:
# Here, based on output from Step-3
# Because, it is '2.25',
# Find a number a index lower than 2.25
# So, lower index is '2'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=2 we have '3'
# Output --> 3
For Higher:
# Here, based on output from Step-3
# Because, it is '2.25',
# Find a number a index higher than 2.25
# So, higher index is '3'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=3 we have '4'
# Output --> 4
For Nearest:
# Here, based on output from Step-3
# Because, it is '2.25',
# Find a number a index nearest to 2.25
# So, nearest index is '2'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=2 we have '3'
# Output --> 3
For Midpoint:
# Here, based on output from Step-3
# (i + j)/2
# Here, think i and j as values at indices
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, for '2.25'
# value at index immediately before 2.25, is at index=2 so, i=3
# value at index immediately after 2.25, is at index=3 so, i=4
(3+4)/2
# Output --> 3.5
Code in Python:
x = np.array([1,2,3,4,5,6,7,8,9,10])
print("linear:", np.percentile(x, 25, interpolation='linear'))
print("lower:", np.percentile(x, 25, interpolation='lower'))
print("higher:", np.percentile(x, 25, interpolation='higher'))
print("nearest:", np.percentile(x, 25, interpolation='nearest'))
print("midpoint:", np.percentile(x, 25, interpolation='midpoint'))
Output:
linear: 3.25
lower: 3
higher: 4
nearest: 3
midpoint: 3.5