Numpy 在无for循环的情况下,在批处理数据的每一帧中找到子数组首次出现索引的最佳方法

Numpy 在无for循环的情况下,在批处理数据的每一帧中找到子数组首次出现索引的最佳方法,numpy,for-loop,tensorflow,math,batch-processing,Numpy,For Loop,Tensorflow,Math,Batch Processing,我必须找到每个帧中第一次出现的子数组的索引。数据大小为(batch_size,400)。我需要找到大小为400的每一帧中三个连续出现的索引。 数据->[0 0 1 1 0 1 1 1 1 1 1][0 0 0 1 1 1 1 1 0 1 1 1][0 1 1 1 1 1 1 1][0 1 1 1 1][/code> 输出应为[3 4 1] 本机解决方案使用for循环,但由于数据量大,因此非常耗时 在快速高效的numpy或tensorflow中,没有简单的numpy解决方案。但是,如果您真的需要速

我必须找到每个帧中第一次出现的子数组的索引。数据大小为(batch_size,400)。我需要找到大小为400的每一帧中三个连续出现的索引。 数据->
[0 0 1 1 0 1 1 1 1 1 1][0 0 0 1 1 1 1 1 0 1 1 1][0 1 1 1 1 1 1 1][0 1 1 1 1][/code>

输出应为
[3 4 1]

本机解决方案使用for循环,但由于数据量大,因此非常耗时


在快速高效的
numpy
tensorflow
中,没有简单的numpy解决方案。但是,如果您真的需要速度,您可以使用以下方法:

函数
find_first
的作用与for循环基本相同。但是由于您使用的是numba,因此该方法是编译的,因此速度要快得多。 然后您只需使用
np将该方法应用于每个批次。沿\u轴应用\u

import numpy as np
from numba import jit


@jit(nopython=True)
def find_first(seq, arr):
    """return the index of the first occurence of item in arr"""
    for i in range(len(arr)-2):
        if np.all(seq == arr[i:i+3]):
            return i
    return -1

# construct test array
test = np.round(np.random.random((64,400)))

# this will give you the array of indices
np.apply_along_axis(lambda m: find_first(np.array([1,1,1]), m), axis=1, arr = test)

我修改了方法,因为没有简单的numpy解决方案。但是,如果您真的需要速度,您可以使用以下方法:

函数
find_first
的作用与for循环基本相同。但是由于您使用的是numba,因此该方法是编译的,因此速度要快得多。 然后您只需使用
np将该方法应用于每个批次。沿\u轴应用\u

import numpy as np
from numba import jit


@jit(nopython=True)
def find_first(seq, arr):
    """return the index of the first occurence of item in arr"""
    for i in range(len(arr)-2):
        if np.all(seq == arr[i:i+3]):
            return i
    return -1

# construct test array
test = np.round(np.random.random((64,400)))

# this will give you the array of indices
np.apply_along_axis(lambda m: find_first(np.array([1,1,1]), m), axis=1, arr = test)
我修改了

的方法,因为这样的(111)循环模式(带计数的)可能是最快的方法。对于这样的(111)循环模式(带计数的)可能是最快的方法。