Reputation: 1219
I have a numpy array with the shape (3,1000,1000) and I have had to change it to (1000,1000,3) for some image processing. I now need to reshape back to (3,1000,1000) preserving the original places, but am unable to figure out how to do it. Here is an example:
import numpy as np
arr1 = np.array([1] * 1000000).reshape(1000,1000)
arr2 = np.array([2] * 1000000).reshape(1000,1000)
arr3 = np.array([3] * 1000000).reshape(1000,1000)
arr_list = list((arr1,arr2,arr3))
arr = np.array(arr_list).reshape(3,1000,1000)
arr_d = np.dstack(arr_list)
arr
is the original shape of the array, while arr_d
is the shape I needed for the image processing.
Essentially, each of the original 2-D matrices in arr
are a column in arr_d
. Now I need to get back from arr_d
to arr
, preserving the values.
Upvotes: 0
Views: 534
Reputation: 1423
I believe it's as easy as transposing arr_d
>>> np.array_equal(arr, arr_d.T)
True
Upvotes: 1