Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab中分位数的等效python命令_Python_Matlab_Quantile - Fatal编程技术网

matlab中分位数的等效python命令

matlab中分位数的等效python命令,python,matlab,quantile,Python,Matlab,Quantile,我试图用python复制一些Matlab代码。我找不到与Matlab函数分位数完全等效的函数。我发现最接近python的mquantiles Matlab示例: quantile( [ 8.60789925e-05, 1.98989354e-05 , 1.68308882e-04, 1.69379370e-04], 0.8) …给出:0.00016958 python中的相同示例: scipy.stats.mstats.mquantiles( [8.60789925e-05, 1.9898

我试图用python复制一些Matlab代码。我找不到与Matlab函数
分位数
完全等效的函数。我发现最接近python的
mquantiles

Matlab示例:

 quantile( [ 8.60789925e-05, 1.98989354e-05 , 1.68308882e-04, 1.69379370e-04],  0.8)
…给出:
0.00016958

python中的相同示例:

scipy.stats.mstats.mquantiles( [8.60789925e-05, 1.98989354e-05, 1.68308882e-04, 1.69379370e-04], 0.8)
…给出
0.00016912


有人知道如何精确复制Matlab的
分位数
函数吗?

您的输入向量只有4个值,这太少了,无法获得基本分布分位数的良好近似值。这种差异可能是Matlab和SciPy使用不同的启发式算法计算欠采样分布上的分位数的结果。

在“更多关于=>算法”部分下给出了所使用的精确算法。下面是一些python代码,用于对平面数组的单个分位数执行此操作,用于执行部分排序:

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)
matlab代码给出了
0.000169058223599999
;区别在于
3e-20
。(低于机器精度)

有点晚,但:

mquantiles非常灵活。您只需要提供alphap和betap参数。 这里,由于MATLAB进行线性插值,因此需要将参数设置为(0.5,0.5)


编辑:MATLAB说它做线性插值,但它似乎通过分段线性插值来计算分位数,这相当于R中的第5类分位数,以及scipy中的(0.5,0.5)。

为什么要投反对票?如果我的答案有问题,我想知道是什么。
>>> quantile([ 8.60789925e-05, 1.98989354e-05 , 1.68308882e-04, 1.69379370e-04], 0.8)
0.00016905822360000001
In [9]: scipy.stats.mstats.mquantiles( [8.60789925e-05, 1.98989354e-05, 1.68308882e-04, 1.69379370e-04], 0.8, alphap=0.5, betap=0.5)