2013-08-23 67 views
-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] 

如何計算54.5和81.75,81.75和87.200000000000003之間的數值百分比等。如何計算數組間隔百分比

+0

numpy.histogram – user333700

回答

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

更新:

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 

看來percentileofscore是慢了很多。

+0

謝謝你,那工作。 – greggyNapalm

+0

進一步的改進可能是:'np.sum((a> 54.5)&(a <81.75))/ float(a.size)'。 – Daniel

+0

@Ophion是的,但只有15%左右。沒有那麼戲劇性...... –

0

我認爲你正在尋找scipy.stats.percentileofscore

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

減去這些都會給你(54.5和81.75之間分數的百分比)25。

這意味着你可以使用映射反轉np.percentile,然後應用一個移位和一個減法來獲得你在之後的「數組間隔百分比」。

0

做這樣使用環路的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