reshpae numpy array code example

Example 1: numpy reshape

np.reshape(a, (2, 3)) # C-like index ordering
array([[0, 1, 2],
       [3, 4, 5]])
np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape
array([[0, 1, 2],
       [3, 4, 5]])
np.reshape(a, (2, 3), order='F') # Fortran-like index ordering
array([[0, 4, 3],
       [2, 1, 5]])
np.reshape(np.ravel(a, order='F'), (2, 3), order='F')
array([[0, 4, 3],
       [2, 1, 5]])

Example 2: convert np shape (a,) to (a,1)

# numpy array/vector (n,) dimension -> (n,1) dimension conversion
a  = np.arange(3)         # a.shape  = (3,)
b  = a.reshape((3,1))     # b.shape  = (3,1)
c  = b.reshape((3,))      # c.shape  = (3,)

c2 = b.reshape((-1,1))    # c2.shape = (3,)