Python 强制阵列中非零元素之间的最小间距

Python 强制阵列中非零元素之间的最小间距,python,arrays,algorithm,numpy,poisson,Python,Arrays,Algorithm,Numpy,Poisson,我试图生成一个0和1的数组,其中1之间的间距是泊松分布的 我相信这段代码可以做到: 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) 将numpy导入为np dt=0.05 n_t=500/dt#所需阵列长度 r=100 尖峰序列=(np.random.rand(n_

我试图生成一个0和1的数组,其中1之间的间距是泊松分布的

我相信这段代码可以做到:

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)
将numpy导入为np
dt=0.05
n_t=500/dt#所需阵列长度
r=100
尖峰序列=(np.random.rand(n_t)
但是,我也希望在1之间强制一个最小间距,比如在任意两个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语句中添加更多的条件,但是对于您的情况,有大量的零,我认为它可以工作。

这里是一个相当有效的方法。它的工作原理是(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
将numpy导入为np
def序列(T、dt、速率、最小等待):
p=dt*速率
#最小等待时间正确
如果p:
p=1/(1/p-最小等待时间)
如果p>0.1:
打印(“警告:概率太高,近似值不好”)
n=int(np.ceil(T/dt))
原始时间,=np.where(np.random.random(n)
也许我可以生成一个更短的数组。。。然后在数组元素之间设置零填充?现在这是一个半生不熟的想法……我想到的一个想法是将
1,0,0
而不是仅仅
1
,你可以通过以下方式实现:
spike\u train=([1,0,0]如果n else[0]表示n在np.random.rand(n\u t)
,然后将list@HemersonTacon是 啊我猜你的想法和我很相似。。。。但是在您的例子中,您没有得到所需的长度为
n\u t
的数组。我应该澄清什么是
n\u t
以及其他变量的含义。在您的情况下,实际长度会因运行而异。出现时的2个最小零也应算作分布应接近的总零,对吗?@HemersonTacon是的,这是我尚未找到解决方案的问题。