-2
>>> import numpy as np
>>> standart_perc = [50, 75, 80, 85, 90, 95, 98, 99, 100]
>>> a = np.arange(110)
>>> np.percentile(a, standart_perc)
[54.5, 81.75, 87.200000000000003, 92.649999999999991, 98.100000000000009, 103.55, 106.81999999999999, 107.91, 109.0]

How to calc percentage of values between 54.5 and 81.75, 81.75 and 87.200000000000003, etc .. ?

4

3 回答 3

3
a[(a > 54.5) & (a <  81.75)].size / float(a.size)

Update:

In [6]: a = np.random.randint(1, 110, 1000000)
In [7]: %%timeit
        percentileofscore(a, 81.75) - percentileofscore(a, 54.5)
1 loops, best of 3: 373 ms per loop
In [8]: %%timeit
        a[(a > 54.5) & (a <  81.75)].size / float(a.size)
10 loops, best of 3: 20.5 ms per loop

It seems that percentileofscore is a lot slower.

于 2013-08-23T10:02:49.507 回答
0

I think you are looking for scipy.stats.percentileofscore:

percentileofscore(a, 54.5) == 50.
percentileofscore(a, 81.75) == 75.

subtracting these will give you 25 (the percentage of scores between 54.5 and 81.75).

This means you can invert np.percentile using map, and then apply a shift and a subtract to get the "percentage if array in interval" you're after.

于 2013-08-23T10:14:39.747 回答
0

Do like this using while loop in Python:

>>> a
[54.5, 81.75, 87.2, 92.64999999999999, 98.10000000000001, 103.55, 106.82, 107.91, 109.0]
>>> i = 0
>>> while i < len(a)-1:
...     print a[i]/a[i+1]*100
...     i = i+1
... 
66.6666666667
93.75
94.1176470588
94.4444444444
94.7368421053
96.9387755102
98.9898989899
99.0
于 2013-08-23T10:38:18.793 回答