numpy 如何使用数组对数组进行切片索引?

gal*_*ica 5 python numpy

也许这已经在其他地方提出并解决了,但我还没有找到。假设我们有一个 numpy 数组:

a = np.arange(100).reshape(10,10)
b = np.zeros(a.shape)
start = np.array([1,4,7])   # can be arbitrary but valid values
end = np.array([3,6,9])     # can be arbitrary but valid values
Run Code Online (Sandbox Code Playgroud)

start并且end两者都具有有效值,因此每个切片也对 有效a。我想将子数组的值复制a到 in 中的相应点b

b[:, start:end] = a[:, start:end]   #error
Run Code Online (Sandbox Code Playgroud)

此语法不起作用,但等效于:

b[:, start[0]:end[0]] = a[:, start[0]:end[0]]
b[:, start[1]:end[1]] = a[:, start[1]:end[1]]
b[:, start[2]:end[2]] = a[:, start[2]:end[2]]
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的方法来代替startend数组上的显式 for 循环。

谢谢!

Div*_*kar 5

我们可以使用broadcasting两组比较来创建要编辑的位置掩码startend数组,然后简单地boolean-indexing为矢量化解决方案分配 with -

# Range array for the length of columns
r = np.arange(b.shape[1])

# Broadcasting magic to give us the mask of places
mask = (start[:,None] <= r) & (end[:,None] >= r)

# Boolean-index to select and assign 
b[:len(mask)][mask] = a[:len(mask)][mask]
Run Code Online (Sandbox Code Playgroud)

样品运行 -

In [222]: a = np.arange(50).reshape(5,10)
     ...: b = np.zeros(a.shape,dtype=int)
     ...: start = np.array([1,4,7])
     ...: end = np.array([5,6,9]) # different from sample for variety
     ...: 

# Mask of places to be edited
In [223]: mask = (start[:,None] <= r) & (end[:,None] >= r)

In [225]: print mask
[[False  True  True  True  True  True False False False False]
 [False False False False  True  True  True False False False]
 [False False False False False False False  True  True  True]]

In [226]: b[:len(mask)][mask] = a[:len(mask)][mask]

In [227]: a
Out[227]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]])

In [228]: b
Out[228]: 
array([[ 0,  1,  2,  3,  4,  5,  0,  0,  0,  0],
       [ 0,  0,  0,  0, 14, 15, 16,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0, 27, 28, 29],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0]])
Run Code Online (Sandbox Code Playgroud)