计算二维数组的百分位数

时间:2014-06-25 16:15:57

标签: python numpy scipy

我有尺寸等级,对于每个尺寸等级,我都测量了数量:

import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
size_class = np.linspace(0,9,10)
counts = norm.pdf(size_class, 5,1) # synthetic data
counts_cumulative_normalised = np.cumsum(counts)/counts.sum() # summing up and normalisation
plt.plot(size_class,counts_cumulative_normalised)
plt.show()

所以,如果我想计算尺寸的百分位数,我必须插入我想要的尺寸。

是否有一个构建函数将这两个向量作为参数并给出我所需的百分位数?

1 个答案:

答案 0 :(得分:1)

如果您不知道数据是否正态分布,并且您希望根据经验累积分布函数获得百分位数,则可以使用插值方法。

In [63]:

plt.plot(size_class,counts_cumulative_normalised)
Out[63]:
[<matplotlib.lines.Line2D at 0x10c72d3d0>]

enter image description here

In [69]:
#what percentile does size 4 correspond to ?
from scipy import interpolate
intp=interpolate.interp1d(size_class, counts_cumulative_normalised, kind='cubic')
intp(4)
Out[69]:
array(0.300529305241782)

我知道你只是提供了一个合成数据,但是注意到你的方式低估了累积分布函数,因为你只需要几个样本点,看看这个比较:

plt.plot(size_class,counts_cumulative_normalised)
plt.plot(size_class,norm.cdf(size_class, 5, 1))

enter image description here

相关问题