sciPy stats.scoreatpercentile() function | Python

scipy.stats.scoreatpercentile(a, score, kind='rank') function helps us to calculate the score at a given percentile of the input array.

The score at percentile = 50 is the median. If the desired quantile lies between two data points, we interpolate between them, according to the value of interpolation.

Parameters :
arr : [array_like] input array.
per : [array_like] Percentile at which we need the score.
limit : [tuple] the lower and upper limits within which to compute the percentile.
axis : [int] axis along which we need to calculate the score.



Results : Score at Percentile relative to the array element.

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# scoreatpercentile
from scipy import stats
import numpy as np 
  
# 1D array  
arr = [20, 2, 7, 1, 7, 7, 34, 3]
  
print("arr : ", arr)  
  
print ("\nScore at 50th percentile : "
       stats.scoreatpercentile(arr, 50))
  
print ("\nScore at 90th percentile : "
       stats.scoreatpercentile(arr, 90))
  
print ("\nScore at 10th percentile : "
       stats.scoreatpercentile(arr, 10))
  
print ("\nScore at 100th percentile : "
       stats.scoreatpercentile(arr, 100))
  
print ("\nScore at 30th percentile : "
       stats.scoreatpercentile(arr, 30))

chevron_right


Output:

arr :  [20, 2, 7, 1, 7, 7, 34, 3]

Score at 50th percentile :  7.0

Score at 90th percentile :  24.2

Score at 10th percentile :  1.7

Score at 100th percentile :  34.0

Score at 30th percentile :  3.4

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# scoreatpercentile
from scipy import stats
import numpy as np 
  
arr = [[14, 17, 12, 33, 44],   
       [15, 6, 27, 8, 19],  
       [23, 2, 54, 1, 4, ]] 
  
print("arr : ", arr)  
  
print ("\nScore at 50th percentile : "
       stats.scoreatpercentile(arr, 50))
  
print ("\nScore at 50th percentile : "
       stats.scoreatpercentile(arr, 50, axis = 1))
  
print ("\nScore at 50th percentile : "
       stats.scoreatpercentile(arr, 50, axis = 0))

chevron_right


Output:

arr :  [[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4]]

Score at 50th percentile :  15.0

Score at 50th percentile :  [ 17.  15.   4.]

Score at 50th percentile :  [ 15.   6.  27.   8.  19.]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.