想要制作一个大型数组B,它由一个较小的numpy数组A组成,以不同的方式翻转:
B[0,:,:,:,:] = A
B[1,:,:,:,:] = B[0,:,::-1,:,:]
B[2:4,:,:,:,:] = B[0:2,:,:,::-1,:]
B[4:8,:,:,:,:] = B[0:4,:,:,:,::-1]
有没有办法只在内存中存储A,但保留B的numpy数组的一些功能?我主要对两件事感兴趣:
答案 0 :(得分:0)
我最终做的是创建一个类来返回A的任意反映部分的视图。在返回此视图后,我正在按C进行缩放,以及总和,现在看起来很快。这是没有错误检查:
class SymmetricArray:
'''
Attributes:
A (np.ndarray): an [m, (a,b,c...)] array.
idx (np.ndarray): an [n,] array where idx[n] points to A[idx[n], ...]
to be used.
reflect (np.ndarray): an [n, d] array where every entry is 1 or -1 and
reflect[n, i] indicates whether or not the ith dimension of
A[idx[n], ...] should be reflected, and d = len(A.shape - 1).
'''
def __init__(self, A, idx, reflect):
self.A = np.asarray(A)
self.idx = np.asarray(idx, dtype=int)
self.reflect = np.asarray(reflect, dtype=int)
def __getitem__(self, ii):
'''
Returns:
np.ndarray: appropriately reflected A[idx[ii], ...]
'''
a_mask = [slice(None, None, a) for a in self.reflect[ii, :]]
return self.A[self.idx[ii], ...][a_mask]