NumPy - reshape() function

The NumPy reshape() function is used to give a new shape to an array without changing its data. The syntax for using this function is given below:

Syntax

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

Parameters

 a Required. Specify the array to be reshaped. newshape Required. Specify int or tuple of ints. 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 Optional. Specify order. Read the elements of the array using this index order, and place the elements into the reshaped array using this index order. 'C' - read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. 'F' - read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. 'A' - read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. Note: 'C' and 'F' options take no account of the memory layout of the underlying array, and only refer to the order of indexing.

Return Value

Returns ndarray. This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.

Example: reshape() an array

In the example below, the reshape() function is used to reshape an array using default parameters.

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print("Original Array:")
print(arr)

#reshaping array from (2,3) -> (3,2)
print("\nReshaped Array:")
print(np.reshape(arr, (3,2)))

#flatten the array
print("\nFlattened Array:")
print(np.reshape(arr, -1))

The output of the above code will be:

Original Array:
[[1 2 3]
[4 5 6]]

Reshaped Array:
[[1 2]
[3 4]
[5 6]]

Flattened Array:
[1 2 3 4 5 6]

Example: reshape() with C-like index ordering

By default reshape function uses C-like ordering. A C-like ordering is equivalent to first raveling the array then inserting the elements into the new array using C-like index order. Consider the example below.

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print("Original Array:")
print(arr)

#reshaping array from (2,3) -> (3,2)
print("\nReshaped Array:")
print(np.reshape(arr, (3,2), order='C'))

#raveling the initial array
ravelarr = np.ravel(arr, order='C')
print("\nRaveled Array:")
print(ravelarr)

#reshaping the ravel array
print("\nReshaped Array from raveled array:")
print(np.reshape(ravelarr, (3,2), order='C'))

The output of the above code will be:

Original Array:
[[1 2 3]
[4 5 6]]

Reshaped Array:
[[1 2]
[3 4]
[5 6]]

Raveled Array:
[1 2 3 4 5 6]

Reshaped Array from raveled array:
[[1 2]
[3 4]
[5 6]]

Example: reshape() with F-like index ordering

A F-like ordering is equivalent to first raveling the array then inserting the elements into the new array using F-like index order. Consider the example below.

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print("Original Array:")
print(arr)

#reshaping array from (2,3) -> (3,2)
print("\nReshaped Array:")
print(np.reshape(arr, (3,2), order='F'))

#raveling the initial array
ravelarr = np.ravel(arr, order='F')
print("\nRaveled Array:")
print(ravelarr)

#reshaping the ravel array
print("\nReshaped Array from raveled array:")
print(np.reshape(ravelarr, (3,2), order='F'))

The output of the above code will be:

Original Array:
[[1 2 3]
[4 5 6]]

Reshaped Array:
[[1 5]
[4 3]
[2 6]]

Raveled Array:
[1 4 2 5 3 6]

Reshaped Array from raveled array:
[[1 5]
[4 3]
[2 6]]

❮ NumPy - Array Manipulation

5