将数组复制到 NumPy 中另一个数组的一部分
Copy array into part of another array in NumPy
我想将较小的数组 A 复制到较大的数组 B 中,如下所示:
执行此操作的明显方法是计算 A 的哪一部分适合 B,并仅将这部分复制到目标数组 B 的同样预先计算的部分。这是一项乏味且容易出错的任务,尤其是在更高维度中。有没有更简单(即无需计算所有指标)的方法来实现这一目标?
澄清一下——通常情况下必须:
在 X 轴上:计算 A 中有多少元素适合 B 部分,从 A 大小中减去它,重新计算 B 中它们适合的起点和终点(索引),重新计算 A 中的索引。在第二维重复(可能更多)。插入 looong 切片公式,这是不可读的,然后一定不要错过这个过程中的任何东西。这就是我所说的 乏味且容易出错 的意思。是可以的,但是有没有更好的方法呢?
您可以利用 NumPy 数组算法来处理 N 维,就像处理 1 维一样。此外,计算 N 维的切片可以用
调用 map(slice, start_indices, end_indices)
,一旦生成这些切片,实际的复制就变成了一行:B[B_slices] = A[A_slices]
:
import numpy as np
def copy_from(A, B, A_start, B_start, B_end):
"""
A_start is the index with respect to A of the upper left corner of the overlap
B_start is the index with respect to B of the upper left corner of the overlap
B_end is the index of with respect to B of the lower right corner of the overlap
"""
A_start, B_start, B_end = map(np.asarray, [A_start, B_start, B_end])
shape = B_end - B_start
B_slices = tuple(map(slice, B_start, B_end + 1))
A_slices = tuple(map(slice, A_start, A_start + shape + 1))
B[B_slices] = A[A_slices]
A = np.zeros((21,15))
B = np.ones((16,15))
A_start = [11, 5]
B_start = [6, 0]
B_end = [15, 9]
copy_from(A, B, A_start, B_start, B_end)
print(B)
产量
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]]
我想将较小的数组 A 复制到较大的数组 B 中,如下所示:
澄清一下——通常情况下必须:
在 X 轴上:计算 A 中有多少元素适合 B 部分,从 A 大小中减去它,重新计算 B 中它们适合的起点和终点(索引),重新计算 A 中的索引。在第二维重复(可能更多)。插入 looong 切片公式,这是不可读的,然后一定不要错过这个过程中的任何东西。这就是我所说的 乏味且容易出错 的意思。是可以的,但是有没有更好的方法呢?
您可以利用 NumPy 数组算法来处理 N 维,就像处理 1 维一样。此外,计算 N 维的切片可以用
调用 map(slice, start_indices, end_indices)
,一旦生成这些切片,实际的复制就变成了一行:B[B_slices] = A[A_slices]
:
import numpy as np
def copy_from(A, B, A_start, B_start, B_end):
"""
A_start is the index with respect to A of the upper left corner of the overlap
B_start is the index with respect to B of the upper left corner of the overlap
B_end is the index of with respect to B of the lower right corner of the overlap
"""
A_start, B_start, B_end = map(np.asarray, [A_start, B_start, B_end])
shape = B_end - B_start
B_slices = tuple(map(slice, B_start, B_end + 1))
A_slices = tuple(map(slice, A_start, A_start + shape + 1))
B[B_slices] = A[A_slices]
A = np.zeros((21,15))
B = np.ones((16,15))
A_start = [11, 5]
B_start = [6, 0]
B_end = [15, 9]
copy_from(A, B, A_start, B_start, B_end)
print(B)
产量
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]]