I have a 3d numpy array of following form:
array([[[ 1., 5., 4.],
[ 1., 5., 4.],
[ 1., 2., 4.]],
[[ 3., 6., 4.],
[ 6., 6., 4.],
[ 6., 6., 4.]]])
Is there a efficient way to convert it to a 2d array of form:
array([[1, 1, 1, 5, 5, 2, 4, 4, 4],
[3, 6, 6, 6, 6, 6, 4, 4, 4]])
Thanks a lot!
reshape() function to convert a 3D array with dimensions (4, 2, 2) to a 2D array with dimensions (4, 4) in Python. In the above code, we first initialize a 3D array arr using numpy. array() function and then convert it into a 2D array newarr with numpy. reshape() function.
Introduction to NumPy 3D array. Arrays in NumPy are the data structures with high performance which are suitable for mathematical operations. The three levels of arrays nested inside one another represent the three-dimensional array in python, where each level represents one dimension.
convert a 1-dimensional array into a 2-dimensional array by adding new axis. a=np. array([10,20,30,40,50,60]) b=a[:,np. newaxis]--it will convert it to two dimension.
In [54]: arr = np.array([[[ 1., 5., 4.],
[ 1., 5., 4.],
[ 1., 2., 4.]],
[[ 3., 6., 4.],
[ 6., 6., 4.],
[ 6., 6., 4.]]])
In [61]: arr.reshape((arr.shape[0], -1), order='F')
Out[61]:
array([[ 1., 1., 1., 5., 5., 2., 4., 4., 4.],
[ 3., 6., 6., 6., 6., 6., 4., 4., 4.]])
The array arr
has shape (2, 3, 3)
. We wish to keep the first axis of length 2, and flatten the two axes of length 3.
If we call arr.reshape(h, w)
then NumPy will attempt to reshape arr
to shape (h, w)
. If we call arr.reshape(h, -1)
then NumPy will replace the -1
with whatever integer is needed for the reshape to make sense -- in this case, arr.size/h
.
Hence,
In [63]: arr.reshape((arr.shape[0], -1))
Out[63]:
array([[ 1., 5., 4., 1., 5., 4., 1., 2., 4.],
[ 3., 6., 4., 6., 6., 4., 6., 6., 4.]])
This is almost what we want, but notice that the values in each subarray, such as
[[ 1., 5., 4.],
[ 1., 5., 4.],
[ 1., 2., 4.]]
are being traversed by marching from left to right before going down to the next row.
We want to march down the rows before going on to the next column.
To achieve that, use order='F'
.
Usually the elements in a NumPy array are visited in C-order
-- where the last index moves fastest. If we visit the elements in F-order
then the first index moves fastest. Since in a 2D array of shape (h, w)
, the first axis is associated with the rows and the last axis the columns, traversing the array in F-order
marches down each row before moving on to the next column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With