强制数组中非零元素之间的最小间距

时间:2018-12-11 02:48:50

标签: python arrays algorithm numpy poisson

我正在尝试生成一个零和一的数组,其中零之间的间隔是泊松分布的。

我相信这段代码可以解决问题:

import numpy as np

dt = 0.05
n_t = 500 / dt  # desired length of array
r = 100
spike_train = (np.random.rand(n_t) < r * dt).astype(int)

但是,我还想在1与2之间设置一个最小的间距,例如在任何两个1与2之间的最小值。

什么是有效的方法?

2 个答案:

答案 0 :(得分:1)

这是一种相当有效的方法。它的工作原理是(1)首先忽略最小等待时间。 (2)计算事件间时间(3)添加最小等待时间,(4)返回绝对时间,丢弃已移出右端的事件。它可以在不到一秒钟的时间内创建10 ** 7个样本。

import numpy as np

def train(T, dt, rate, min_wait):
    p = dt*rate
    # correct for minimum wait
    if p:
        p = 1 / (1 / p - min_wait) 
    if p > 0.1:
        print("warning: probability too high for approximation to be good")
    n = int(np.ceil(T/dt))
    raw_times, = np.where(np.random.random(n) < p)
    raw_times[1:] += min_wait - raw_times[:-1]
    good_times = raw_times.cumsum()
    cut = good_times.searchsorted(n)
    result = np.zeros(n, int)
    result[good_times[:cut]] = 1
    return result

答案 1 :(得分:0)

我认为这种(不太优雅的)逻辑可以保持分布:

def assure_2zeros(l):

    for idx in range(len(l)):
        # detect the problem when the ones are adjacent
        if l[idx] == 1 and l[idx+1] == 1:
            # check forward for a zero that could be realocated to split the ones
            for i in range(idx+2, len(l)-1):
                # check to not create other problems
                if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
                    del l[i]
                    l.insert(idx+1, 0)
                    break
            # if doesnt found any zero forward, check backward
            else:
                for i in range(idx-1, 0, -1):
                    if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
                        del l[i]
                        l.insert(idx+1, 0)
                        break

        # detects the problem when there are one zero between the ones
        if l[idx] == 1 and l[idx+2] == 1:
            for i in range(idx+3, len(l)-1):
                if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
                    del l[i]
                    l.insert(idx+1, 0)
                    break
            else:
                for i in range(idx-1, 0, -1):
                    if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
                        del l[i]
                        l.insert(idx+1, 0)
                        break

    return l

请注意,当有两个相邻的if时,第一个if将在它们之间插入一个零,然后它将输入第二个if并将第二个零插入。但是,当列表的末尾或开头有两个或一个零时,它将失败。可以在if语句中添加更多条件,但是对于您的情况,有很多零,我认为它可以工作。