Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 3d Numpy array to 2d

Tags:

python

numpy

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!

like image 548
ascetic652 Avatar asked May 28 '16 15:05

ascetic652


People also ask

How do I convert a 3D numpy array to 2D?

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.

What is 3D numpy array?

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.

How do you convert a one dimensional array to a two dimensional array in Python?

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.


1 Answers

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.

like image 136
unutbu Avatar answered Oct 07 '22 14:10

unutbu