numpy.reshape() in Python

The numpy.reshape() function is available in NumPy package. As the name suggests, reshape means 'changes in shape'. The numpy.reshape() function helps us to get a new shape to an array without changing its data.

Sometimes, we need to reshape the data from wide to long. So in this situation, we have to reshape the array using reshape() function.

Syntax

snippet
numpy.reshape(arr, new_shape, order='C')

Parameters

There are the following parameters of reshape() function:

1) arr: array_like

This is a ndarray. This is the source array which we want to reshape. This parameter is essential and plays a vital role in numpy.reshape() function.

2) new_shape: int or tuple of ints

The shape in which we want to convert our original array should be compatible with the original array. If an integer, the result will be a 1-D array of that length. One shape dimension can be -1. Here, the value is approximated by the length of the array and the remaining dimensions.

3) order: {'C', 'F', 'A'}, optional

These indexes order parameter plays a crucial role in reshape() function. These index orders are used to read the elements of source array and place the elements into the reshaped array using this index order.

  1. The index order 'C' means to read/write the elements which are using a C-like index order where the last axis index is changing fastest, back to the first axis index changing slowest.
  2. The index order 'F' means to read/write the elements which are using the Fortran-like index order, where the last axis index changing slowest and the first axis index changing fastest.
  3. The 'C' and 'F' order take no amount of the memory layout of the underlying array and only refer to the order of indexing.
  4. The index order 'A' means to read/write the elements in Fortran-like index order, when arr is contiguous in memory, otherwise use C-like order.

Returns

This function returns a ndarray. It is a new view object if possible; otherwise, it will be a copy. There is no guarantee of the memory layout of the returned array.

Example 1: C-like index ordering

snippet
import numpy as np
x=np.arange(12)
y=np.reshape(x, (4,3))
x
y

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arrange() function.
  • We have declared the variable 'y' and assigned the returned value of the np.reshape() function.
  • We have passed the array 'x' and the shape in the function.
  • Lastly, we tried to print the value of arr.

In the output, the array has been represented as three rows and four columns.

Example 2: Equivalent to C ravel then C reshape

snippet
import numpy as np
x=np.arange(12)
y=np.reshape(np.ravel(x),(3,4))
x
y

The ravel() function is used for creating a contiguous flattened array. A one-dimensional array that contains the elements of the input, is returned. A copy is made only when it is needed.

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])

Example 3: Fortran-like index ordering

snippet
import numpy as np
x=np.arange(12)
y=np.reshape(x, (4, 3), order='F')
x
y

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arrange() function.
  • We have declared the variable 'y' and assigned the returned value of np.reshape() function.
  • We have passed the array 'x' and the shape and Fortran-like index order in the function.
  • Lastly, we tried to print the value of arr.

In the output, the array has been represented as four rows and three columns.

Example 4: Fortran-like index ordering

snippet
import numpy as np
x=np.arange(12)
y=np.reshape(np.ravel(x, order='F'), (4, 3), order='F')
x
y

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]])

Example 5: The unspecified value is inferred to be 2

snippet
import numpy as np
x=np.arange(12)
y=np.reshape(x, (2, -1))
x
y

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arrange() function.
  • We have declared the variable 'y' and assigned the returned value of the np.reshape() function.
  • We have passed the array 'x' and the shape (unspecified value) in the function.
  • Lastly, we tried to print the value of arr.

In the output, the array has been represented as two rows and five columns.

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]])
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents