简体   繁体   English

Numpy索引:第一个(变化的)2d数组中每行的元素数

[英]Numpy indexing: first (varying) number of elements from each row in 2d array

(short version of my question: In numpy, is there an elegant way of emulating tf.sequence_mask from tensorflow?) (我的问题的简短版本:在numpy中,是否有一种从tensorflow模拟tf.sequence_mask的优雅方法?)

I have a 2d array a (each row represents a sequence of different length). 我有一个2d数组a (每行代表一个不同长度的序列)。 Next, there is a 1d array b (representing sequence lengths). 接下来,存在1d阵列b (表示序列长度)。 Is there an elegant way to get a (flattened) array that would contain only such elements of a that belong to the sequences as specified by their length b : 有一种优雅的方式来获得一个(扁平)阵列将包含只有这样的元件a由它们的长度指定属于该序列b

a = np.array([
    [1, 2, 3, 2, 1],  # I want just [:3] from this row
    [4, 5, 5, 5, 1],  # [:2] from this row
    [6, 7, 8, 9, 0]   # [:4] from this row
])
b = np.array([3,2,4])  # 3 elements from the 1st row, 2 from the 2nd, 4 from the 4th row

the desired result: 期望的结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

By elegant way I mean something that avoids loops. 通过elegant way我的意思是避免循环。

Use broadcasting to create a mask of the same shape as the 2D array and then simply mask and extract valid elements - 使用broadcasting创建与2D阵列相同形状的蒙版,然后简单地掩盖和提取有效元素 -

a[b[:,None] > np.arange(a.shape[1])]

Sample run - 样品运行 -

In [360]: a
Out[360]: 
array([[1, 2, 3, 2, 1],
       [4, 5, 5, 5, 1],
       [6, 7, 8, 9, 0]])

In [361]: b
Out[361]: array([3, 2, 4])

In [362]: a[b[:,None] > np.arange(a.shape[1])]
Out[362]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM