计算二维数组的百分位数
我有一些大小类别,每个类别都有测量的数量:
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 个回答
1
如果你不确定数据是否符合正态分布,并且想根据经验累积分布函数来获取百分位数,你可以使用插值的方法。
In [63]:
plt.plot(size_class,counts_cumulative_normalised)
Out[63]:
[<matplotlib.lines.Line2D at 0x10c72d3d0>]
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))