Python Forum
np.percentile returns wrong value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
np.percentile returns wrong value?
#1
After getting an unexpected value from scipy.stats.iqr I discovered the error was coming from np.percentile. Here's an example of my issue:

x=[1,1,1,2,2,2,3,3,3,4]

There are 10 values in x, the 25th percentile should be mean of 2nd and 3rd value, both of which are '1', so the result should = 1,

But np.percentile(x, 25) returns 1.5

I get that Python starts counting at 0, but when using percentile it shouldn't just ignore the first value in the list.

Presumably, this relatively basic and common function of NumPy in 2023, doesn't have an "Error", but I truly feel like it's returning the wrong value. Am I crazy? Can someone explain this issue to me?
Reply
#2
Funny, np.percentile(x, 25) returns 1.25 for me.

Python is not ignoring the first element in the list. You are ignoring that the percentile score is a score below which a given percentage k of scores in its frequency distribution falls ("exclusive" definition) or a score at or below which a given percentage falls ("inclusive" definition) (from Wikipedia).

You are looking for the score that is greater than 2.5 scores in x. There are no scores less than 1, so that is not the 25th percentile. There are 3 scores less than 2, so 2 is greater than the 25th percentile.

Picking a percentile score when you have such a small sample size and irregular distribution appears to be a hot topic in the statistics world. Numpy avoids controversy by providing 9 different interpolation methods.

https://numpy.org/doc/stable/reference/g...ntile.html
AceTylercholine likes this post
Reply
#3
i also get 1.25
AceTylercholine likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,576 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,025 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  [Regex] Findall returns wrong number of hits Winfried 8 5,825 Aug-23-2018, 02:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020