I was trying to reshape a 3D array/tensor arr
of shape (K, M, N) in numpy
(where each (M, N) subarray could be an image for instance) to a 2D of shape (n_rows * M, n_cols * N).
Obviously, I ensure K = n_rows * n_cols
beforehand.
I tried all the possible permutations (after scrolling on similar topics on SO),
for perm in itertools.permutations([0, 1, 2], 3):
test = arr.transpose(perm).reshape((n_rows * M, n_cols * N))
but unsuccessfully so far.
However, using einops
like this,
test = ein.rearrange(arr, '(r c) h w -> (r h) (c w)', r=n_rows, c=n_cols)
it yields the expected result.
Is there a straightforward way to achieve this with numpy?
Deducing from what I think the ein syntax means (new package to me, so unverified whether this is the produced output you expect):
out: