发现在numpy的阵列(行程长度编码)相同值的序列的长度
-
21-08-2019 - |
题
在一个pylab程序(其很可能是一个MATLAB程序以及)我具有表示距离数的numpy的数组:d[t]
是距离在时间t
(和我的数据的时间跨度是len(d)
时间为单位)。
我感兴趣的事件是当该距离是特定阈值之下,并且我想计算这些事件的持续时间。这很容易让有b = d<threshold
boolean数组,问题归结为计算在b
真,只字长度的序列。但我不知道如何(即使用numpy的原语),这样做有效,我使出走阵列和做手工变化检测(即当值从假到真去初始化计数器,增加柜台只要值为True和输出计数器的序列时,值返回FALSE)。但是,这是极其缓慢的。
如何到efficienly检测那种在numpy的阵列序列的?
下面是说明我的问题的一些Python代码:所述第四点需要很长的时间才能显示(如果没有,增加数组的大小)
from pylab import *
threshold = 7
print '.'
d = 10*rand(10000000)
print '.'
b = d<threshold
print '.'
durations=[]
for i in xrange(len(b)):
if b[i] and (i==0 or not b[i-1]):
counter=1
if i>0 and b[i-1] and b[i]:
counter+=1
if (b[i-1] and not b[i]) or i==len(b)-1:
durations.append(counter)
print '.'
解决方案
虽然不是numpy
原语,itertools
函数通常非常快,所以做(当然并测量各种解决方案包括本次)给这一个尝试:
def runs_of_ones(bits):
for bit, group in itertools.groupby(bits):
if bit: yield sum(group)
如果你需要的值在列表中,正好可以利用列表(runs_of_ones(位)),当然,但也许列表理解可能会稍快还是:
def runs_of_ones_list(bits):
return [sum(g) for b, g in itertools.groupby(bits) if b]
移动到 “numpy的天然” 的可能性,那:
def runs_of_ones_array(bits):
# make sure all runs of ones are well-bounded
bounded = numpy.hstack(([0], bits, [0]))
# get 1 at run starts and -1 at run ends
difs = numpy.diff(bounded)
run_starts, = numpy.where(difs > 0)
run_ends, = numpy.where(difs < 0)
return run_ends - run_starts
此外:一定要对对方的基准解决方案在实际适合你的例子
其他提示
对于任何阵列完全numpy的矢量和通用RLE(也适用于字符串,布尔值等等太)。
输出游程长度的元组,开始位置和值。
import numpy as np
def rle(inarray):
""" run length encoding. Partial credit to R rle function.
Multi datatype arrays catered for including non Numpy
returns: tuple (runlengths, startpositions, values) """
ia = np.asarray(inarray) # force numpy
n = len(ia)
if n == 0:
return (None, None, None)
else:
y = np.array(ia[1:] != ia[:-1]) # pairwise unequal (string safe)
i = np.append(np.where(y), n - 1) # must include last element posi
z = np.diff(np.append(-1, i)) # run lengths
p = np.cumsum(np.append(0, z))[:-1] # positions
return(z, p, ia[i])
相当快(I7):
xx = np.random.randint(0, 5, 1000000)
%timeit yy = rle(xx)
100 loops, best of 3: 18.6 ms per loop
多个数据类型:
rle([True, True, True, False, True, False, False])
Out[8]:
(array([3, 1, 1, 2]),
array([0, 3, 4, 5]),
array([ True, False, True, False], dtype=bool))
rle(np.array([5, 4, 4, 4, 4, 0, 0]))
Out[9]: (array([1, 4, 2]), array([0, 1, 5]), array([5, 4, 0]))
rle(["hello", "hello", "my", "friend", "okay", "okay", "bye"])
Out[10]:
(array([2, 1, 1, 2, 1]),
array([0, 2, 3, 4, 6]),
array(['hello', 'my', 'friend', 'okay', 'bye'],
dtype='|S6'))
的结果相同亚历泰利以上:
xx = np.random.randint(0, 2, 20)
xx
Out[60]: array([1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1])
am = runs_of_ones_array(xx)
tb = rle(xx)
am
Out[63]: array([4, 5, 2, 5])
tb[0][tb[2] == 1]
Out[64]: array([4, 5, 2, 5])
%timeit runs_of_ones_array(xx)
10000 loops, best of 3: 28.5 µs per loop
%timeit rle(xx)
10000 loops, best of 3: 38.2 µs per loop
比阿莱克斯稍慢(但仍然非常快),和灵活得多。
下面是使用溶液只阵列:它需要含有的bool的序列的阵列,并且计数的转变的长度
>>> from numpy import array, arange
>>> b = array([0,0,0,1,1,1,0,0,0,1,1,1,1,0,0], dtype=bool)
>>> sw = (b[:-1] ^ b[1:]); print sw
[False False True False False True False False True False False False
True False]
>>> isw = arange(len(sw))[sw]; print isw
[ 2 5 8 12]
>>> lens = isw[1::2] - isw[::2]; print lens
[3 4]
sw
包含真那里有一个开关,isw
转换它们在索引。然后ISW的项都减去成对在lens
。
请注意,如果该序列始于一个1,将计数的0的序列的长度:该可固定在分度,以计算透镜。而且,我还没有测试角箱子长度为1的这样的序列。
,返回开头的所有True
-子阵列的位置和长度完整的功能。
import numpy as np
def count_adjacent_true(arr):
assert len(arr.shape) == 1
assert arr.dtype == np.bool
if arr.size == 0:
return np.empty(0, dtype=int), np.empty(0, dtype=int)
sw = np.insert(arr[1:] ^ arr[:-1], [0, arr.shape[0]-1], values=True)
swi = np.arange(sw.shape[0])[sw]
offset = 0 if arr[0] else 1
lengths = swi[offset+1::2] - swi[offset:-1:2]
return swi[offset:-1:2], lengths
测试时间不同布尔1D阵列(空阵列;单/多元件;偶/奇长度;开始与True
/ False
;仅True
/ False
元素)
以防万一有人想了解(因为你顺便提到MATLAB),这里是解决它在MATLAB一种方式:
threshold = 7;
d = 10*rand(1,100000); % Sample data
b = diff([false (d < threshold) false]);
durations = find(b == -1)-find(b == 1);
我不是太熟悉Python,但也许这将有助于给你一些想法。 =)
durations = []
counter = 0
for bool in b:
if bool:
counter += 1
elif counter > 0:
durations.append(counter)
counter = 0
if counter > 0:
durations.append(counter)