Reshape an array in NumPy Reshape an array in NumPy numpy numpy

Reshape an array in NumPy


a = np.arange(18).reshape(9,2)b = a.reshape(3,3,2).swapaxes(0,2)# a: array([[ 0,  1],       [ 2,  3],       [ 4,  5],       [ 6,  7],       [ 8,  9],       [10, 11],       [12, 13],       [14, 15],       [16, 17]])# b:array([[[ 0,  6, 12],        [ 2,  8, 14],        [ 4, 10, 16]],       [[ 1,  7, 13],        [ 3,  9, 15],        [ 5, 11, 17]]])


numpy has a great tool for this task ("numpy.reshape") link to reshape documentation

a = [[ 0  1] [ 2  3] [ 4  5] [ 6  7] [ 8  9] [10 11] [12 13] [14 15] [16 17]]`numpy.reshape(a,(3,3))`

you can also use the "-1" trick

`a = a.reshape(-1,3)`

the "-1" is a wild card that will let the numpy algorithm decide on the number to input when the second dimension is 3

so yes.. this would also work: a = a.reshape(3,-1)

and this: a = a.reshape(-1,2)would do nothing

and this: a = a.reshape(-1,9) would change the shape to (2,9)


There are two possible result rearrangements (following example by @eumiro). Einops package provides a powerful notation to describe such operations non-ambigously

>> a = np.arange(18).reshape(9,2)# this version corresponds to eumiro's answer>> einops.rearrange(a, '(x y) z -> z y x', x=3)array([[[ 0,  6, 12],        [ 2,  8, 14],        [ 4, 10, 16]],       [[ 1,  7, 13],        [ 3,  9, 15],        [ 5, 11, 17]]])# this has the same shape, but order of elements is different (note that each paer was trasnposed)>> einops.rearrange(a, '(x y) z -> z x y', x=3)array([[[ 0,  2,  4],        [ 6,  8, 10],        [12, 14, 16]],       [[ 1,  3,  5],        [ 7,  9, 11],        [13, 15, 17]]])