运行中位数不包括零

时间:2016-06-06 21:35:30

标签: python numpy median

我借了一些代码来计算数组的运行中位数。但对于每个正在运行的数组,我想排除零值。以下是代码:

def RunningMedian(seq, M):
    seq = iter(seq)
    s = []
    m = M // 2

    # Set up list s (to be sorted) and load deque with first window of seq
    s = [item for item in islice(seq,M)]
    d = deque(s)
    # Simple lambda function to handle even/odd window sizes    
    median = lambda : s[m] if bool(M&1) else (s[m-1]+s[m])*0.5
    # Sort it in increasing order and extract the median ("center" of the sorted window)
    s.sort()
    # remove zeros from the array
    s = np.trim_zeros(s)
    print s
    medians = [median()]
    for item in seq:
        old = d.popleft()          # pop oldest from left
        d.append(item)             # push newest in from right
        del s[bisect_left(s, old)] # locate insertion point and then remove old 
        insort(s, item)            # insert newest such that new sort is not required        
        s = np.trim_zeros(s)
        print s
        medians.append(median())
    return medians

我正在测试代码,但它失败了。我的示例是a = np.array([5 2 0 9 4 2 6 8]),我将此函数称为RunningMedian(a,3)。我想要的每个运行框是:     [2,5]     [2,9]     [4,9]     [2,4,9]     [2,4,6]     [2,6,8-]

然而,在我调用上述函数后,它给出:     [2,5]     [2,9]     [4,9]     [2,9]     [2,6]     [2,8]

并且它返回错误的中值。通话中返回的中位数为:[5, 9, 9, 9, 6, 8]

任何人都可以帮我纠正这个问题?谢谢。

2 个答案:

答案 0 :(得分:2)

你的代码的主要问题是在s中使用所用对象的长度丢弃零,这就解释了为什么你最后没有得到3个长度的窗口。

我建议采用另一种方法:为median使用适当的函数并在本地忽略这些零值。这样它更干净,而且你不需要trim_zeros(导入numpy这是非常糟糕的做法)。根据您的功能,我的出现了:

from itertools import islice
from collections import deque
from bisect import bisect_left,insort

def median(s):
    sp = [nz for nz in s if nz!=0]
    print sp
    Mnow = len(sp)
    mnow = Mnow // 2
    return sp[mnow] if bool(Mnow&1) else (sp[mnow-1]+sp[mnow])*0.5

def RunningMedian(seq, M):
    seq = iter(seq)
    s = []
    m = M // 2

    # Set up list s (to be sorted) and load deque with first window of seq
    s = [item for item in islice(seq,M)]
    d = deque(s)
    ## Simple lambda function to handle even/odd window sizes    
    #median = lambda : s[m] if bool(M&1) else (s[m-1]+s[m])*0.5

    # Sort it in increasing order and extract the median ("center" of the sorted window)
    s.sort()
    medians = [median(s)]
    for item in seq:
        old = d.popleft()          # pop oldest from left
        d.append(item)             # push newest in from right
        del s[bisect_left(s, old)] # locate insertion point and then remove old 
        insort(s, item)            # insert newest such that new sort is not required        
        medians.append(median(s))
    return medians

大部分更改都在新median函数中,我将打印件移到那里。我还添加了你的进口。请注意,我会以非常不同的方式解决此问题,而且当前"固定"非常有可能。版本的鸭子胶带味道。

无论如何,它似乎按照您的意愿运作:

>>> a = [5, 2, 0, 9, 4, 2, 6, 8]

>>> RunningMedian(a,3)
[2, 5]
[2, 9]
[4, 9]
[2, 4, 9]
[2, 4, 6]
[2, 6, 8]
[3.5, 5.5, 6.5, 4, 4, 6]

并且你的版本中的中位数的原因是窗口的奇偶校验是由输入窗口宽度M确定的。如果丢弃零,则最终会得到较小(偶数长度)的窗口。在这种情况下,您不需要中间(=第二)元素,但您需要在中间平均两个元素。因此你的错误输出。

答案 1 :(得分:0)

尝试:

[s[s!=0] for s in np.dstack((a[:-2], a[1:-1], a[2:]))[0]]