numpy.reshape(a, newshape, order='C')

Gives a new shape to an array without changing its data.


a : array_like

Array to be reshaped.

newshape : {tuple, int}

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

order : {‘C’, ‘F’}, optional

Determines whether the array data should be viewed as in C (row-major) order or FORTRAN (column-major) order.


reshaped_array : ndarray

This will be a new view object if possible; otherwise, it will be a copy.

See also

Equivalent method.


>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> np.reshape(a, 6, order='F')
array([1, 4, 2, 5, 3, 6])
>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])

Previous topic


Next topic


This Page

Quick search