Jac*_*019 5 python matlab quantile
我正在尝试将我在matlab中的一些代码复制到python中.我发现matlab中的分位数函数在python中没有"完全"对应的.我发现最接近的是python的mquantiles.例如
对于matlab:
quantile( [ 8.60789925e-05, 1.98989354e-05 , 1.68308882e-04, 1.69379370e-04], 0.8)
Run Code Online (Sandbox Code Playgroud)
得到: 0.00016958
对于python:
scipy.stats.mstats.mquantiles( [8.60789925e-05, 1.98989354e-05, 1.68308882e-04, 1.69379370e-04], 0.8)
Run Code Online (Sandbox Code Playgroud)
给 0.00016912
有谁知道如何完全复制matlab的分位数?非常感谢.
(在更多关于 => 算法部分)的文档quantile
给出了所使用的确切算法。这是一些python代码,它为平面数组的单个分位数执行此操作,使用bottleneck进行部分排序:
import numpy as np
import botteleneck as bn
def quantile(a, prob):
"""
Estimates the prob'th quantile of the values in a data array.
Uses the algorithm of matlab's quantile(), namely:
- Remove any nan values
- Take the sorted data as the (.5/n), (1.5/n), ..., (1-.5/n) quantiles.
- Use linear interpolation for values between (.5/n) and (1 - .5/n).
- Use the minimum or maximum for quantiles outside that range.
See also: scipy.stats.mstats.mquantiles
"""
a = np.asanyarray(a)
a = a[np.logical_not(np.isnan(a))].ravel()
n = a.size
if prob >= 1 - .5/n:
return a.max()
elif prob <= .5 / n:
return a.min()
# find the two bounds we're interpreting between:
# that is, find i such that (i+.5) / n <= prob <= (i+1.5)/n
t = n * prob - .5
i = np.floor(t)
# partial sort so that the ith element is at position i, with bigger ones
# to the right and smaller to the left
a = bn.partsort(a, i)
if i == t: # did we luck out and get an integer index?
return a[i]
else:
# we'll linearly interpolate between this and the next index
smaller = a[i]
larger = a[i+1:].min()
if np.isinf(smaller):
return smaller # avoid inf - inf
return smaller + (larger - smaller) * (t - i)
Run Code Online (Sandbox Code Playgroud)
我只做了单分位数,1d 的情况,因为这就是我所需要的。如果您想要多个分位数,则可能值得进行完整排序;要按轴执行此操作并且知道您没有任何 nan,您需要做的就是向排序添加轴参数并矢量化线性插值位。用 nans 在每个轴上做这件事会有点棘手。
这段代码给出:
>>> quantile([ 8.60789925e-05, 1.98989354e-05 , 1.68308882e-04, 1.69379370e-04], 0.8)
0.00016905822360000001
Run Code Online (Sandbox Code Playgroud)
和matlab代码给出了0.00016905822359999999
; 不同之处在于3e-20
。(小于机器精度)
您的输入向量只有 4 个值,这太少了,无法很好地近似基础分布的分位数。这种差异可能是由于 Matlab 和 SciPy 使用不同的启发式计算欠采样分布的分位数造成的。