-1

I want to calculate percentile values for 10%, 50% and 90%. So the inputs would be a percentile you want to find and an array of values to calculate. How would I do this? It's been a while since stats...

Help in powershell or python would be appreciated.

Edit: Sorry, I meant creating my own function rather than using a pre built function/library

whatisit
  • 45
  • 1
  • 6
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. This is a straightforward browser search, not a Stack Exchange question. – Prune Oct 10 '20 at 05:35

3 Answers3

1

You can do it using numpy in the following way:

import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50)

You can read more about the percentile function in the attached link.

Other option is to use statistics.quantiles this will give you a distribution list of n - 1 cut points separating the n quantile intervals.

Examlpe of use:

from statistics import quantiles

quantiles([1, 2, 3, 4, 5], n=100)
# [0.06, 0.12, 0.18, 0.24, 0.3, 0.36, 0.42, 0.48, 0.54, 0.6, 0.66, 0.72, 0.78, 0.84, 0.9, 0.96, 1.02, 1.08, 1.14, 1.2, 1.26, 1.32, 1.38, 1.44, 1.5, 1.56, 1.62, 1.68, 1.74, 1.8, 1.86, 1.92, 1.98, 2.04, 2.1, 2.16, 2.22, 2.28, 2.34, 2.4, 2.46, 2.52, 2.58, 2.64, 2.7, 2.76, 2.82, 2.88, 2.94, 3.0, 3.06, 3.12, 3.18, 3.24, 3.3, 3.36, 3.42, 3.48, 3.54, 3.6, 3.66, 3.72, 3.78, 3.84, 3.9, 3.96, 4.02, 4.08, 4.14, 4.2, 4.26, 4.32, 4.38, 4.44, 4.5, 4.56, 4.62, 4.68, 4.74, 4.8, 4.86, 4.92, 4.98, 5.04, 5.1, 5.16, 5.22, 5.28, 5.34, 5.4, 5.46, 5.52, 5.58, 5.64, 5.7, 5.76, 5.82, 5.88, 5.94]
quantiles([1, 2, 3, 4, 5], n=100)[49]

Edit

To create your own function please refer to the following link: https://code.activestate.com/recipes/511478-finding-the-percentile-of-the-values/

David
  • 8,113
  • 2
  • 17
  • 36
0

You can find percentile with numpy

import numpy as np
 
arr = [20, 2, 7, 1, 34]
percentile_arr = [10,50,90]   
for i in range(0,len(percentile_arr)):
       percentile = np.percentile(arr, percentile_arr[i])
       print(f"{percentile_arr[i]}th percentile of array is : {percentile}")

Edit

You can find different approaches with and without numpy here

bahdotsh
  • 459
  • 3
  • 17
-1

Hmmm, by an array you meant a list?

If that's so, then you have very good option: for loop

my_values = [...]
result = []
percentage = 0,5

for i in my_values:
    result.append(i*percentage)

the append method of the list result is an way of telling python "hey, I want you to add this thing over here on the list"

Marco Antonio
  • 143
  • 1
  • 14