SunMiracle
SunMiracle

Reputation: 3

Python: Numpy percentile confusing results

When using the Python Numpy to calculate quantile/percentile, the results are a little bit weird as below. I am quite confused about this, could anyone explain why?

import numpy as np

x = range(1,1031)
x = np.array(x)

np.percentile(x,1,interpolation='lower')
# 11
np.percentile(x,1,interpolation='higher')
# 12

np.percentile(x,0.972,interpolation='lower')
# 11
np.percentile(x,0.972,interpolation='higher')
# 12

np.percentile(x,0.971,interpolation='lower')
# 10
np.percentile(x,0.971,interpolation='higher')
# 11

I expect the lower and higher 1% quantiles of the numbers from 1 to 1030 to be 10 and 11, but the actual results are 11 and 12.

Upvotes: 0

Views: 439

Answers (1)

Imperishable Night
Imperishable Night

Reputation: 1533

Imagine, for a while, that your array were x = range(1,1002) (i.e. 1 to 1001, inclusive). Then 1 is the 0% percentile, and 1001 is 100%. The median would be 501, and it should be the 50% percentile. From this pattern, you can probably interpolate to get that p% percentile should be 10p+1. In particular, the 1% percentile should be 11.

Now, since your actual array is range(1,1031), shouldn't the 1% percentile be slightly higher?

Upvotes: 1

Related Questions