The .reshape() function rearranges the data in an ndarray into a new shape. The new shape must be compatible with the old one, though an index of -1 can be used to infer one dimension.


numpy.reshape(array, newshape)

Where array is the array to be reshaped, and newshape can be an integer or a tuple representing the size of the new array. If a dimension is -1, that dimension will be inferred from the size of the original array.

If possible, the ndarray returned will be a view of the original ndarray‘s data.


The following example creates an ndarray then uses .reshape() to change its dimensions.

import numpy as np
nd1 = np.array([[1,2,3],[4,5,6]])

This produces the following output:

[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python:NumPy on Codecademy