列表中事件间隔之间的频率
Frequency between intervals of events in a list
我有一个记录事件发生的列表:
a = [0,0,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1]
1 显示给定时间步内发生的事件,0 是未观察到事件的时间步。
我有兴趣估计 python 事件之间间隔的统计数据。也就是说,我记录了次次无事件(0)的统计数据:例如
- mean of interval of events: (2+4+3)/3 = 3
- max duration of an interval of no-event: 4
有什么建议我应该怎么做?
非常感谢
这是一种实现您所需的方法。当然有更有效的方法来做到这一点,但这很简单:
a = [0,0,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1]
a = map(str,a) # Convert numbers to strings
a = ''.join(a) # Concatenate all of the strings
a = a.split('1') # Use the handy split function to find the zeros
a = filter(lambda x: len(x) > 0, a) # select the zeros only
a = map(len,a) # convert zero sequences to lengths
print a
这是结果
[2, 2, 4, 3]
interval_zeros = [len(list(group)) for z,group in itertools.groupby(data) if z== 0]
print sum(interval_zeros)/float(len(interval_zeros))
我有一个记录事件发生的列表:
a = [0,0,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1]
1 显示给定时间步内发生的事件,0 是未观察到事件的时间步。
我有兴趣估计 python 事件之间间隔的统计数据。也就是说,我记录了次次无事件(0)的统计数据:例如
- mean of interval of events: (2+4+3)/3 = 3
- max duration of an interval of no-event: 4
有什么建议我应该怎么做?
非常感谢
这是一种实现您所需的方法。当然有更有效的方法来做到这一点,但这很简单:
a = [0,0,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1]
a = map(str,a) # Convert numbers to strings
a = ''.join(a) # Concatenate all of the strings
a = a.split('1') # Use the handy split function to find the zeros
a = filter(lambda x: len(x) > 0, a) # select the zeros only
a = map(len,a) # convert zero sequences to lengths
print a
这是结果
[2, 2, 4, 3]
interval_zeros = [len(list(group)) for z,group in itertools.groupby(data) if z== 0]
print sum(interval_zeros)/float(len(interval_zeros))