Python

How to Use Python NumPy reshape() Function

NumPy library has many functions to work with the multi-dimensional array. reshape () function is one of them that is used to change the shape of any existing array without changing the data. The shape defines the total number of elements in each dimension.  The array’s dimension can be added or removed, and the number of elements in each dimension can be modified by using the reshape() function. The one-dimensional array can be converted into a multi-dimensional array, but the multi-dimensional array can’t be converted into a one-dimensional array by this function. How to reshape() function works and its uses are explained in this tutorial.

Syntax

The syntax of the reshape() function is given below.

np_array numpy.reshape(np_array, new_shape, order='C') 

This function can take three arguments. The first and second arguments are mandatory, and the third argument is optional. A NumPy array is the value of the first argument (np_array) that will be reshaped. The shape of the array is set as the second argument (new_shape) value that can be an integer or a tuple of integers. The array’s order is set by the third argument (order) value used to define the element’s position of the reshaped array. The third argument’s value can be ‘C‘ or ‘F‘ or ‘A.’ The order value ‘C‘ is used for C-style index ordering where the last axis index change faster and the first axis index change slower. The order value ‘F‘ is used for Fortran-style index ordering where the first axis index change faster and the last axis index change slower. Both ‘C‘ and ‘F‘ orders don’t use memory. The order value, ‘A‘ works like ‘F,’ but it uses memory.

Use of reshape() function:

You have to install the NumPy library before practicing the examples of this tutorial. Different uses of the reshape() function have shown in the part of this tutorial.

Example-1: Convert one-dimensional array to two-dimensional array

The following example shows the reshape() function to convert a one-dimensional NumPy array into a two-dimensional NumPy array. arange() function is used in the script to create a one-dimensional array of 10 elements. The first reshape() function is used to convert the one-dimensional array into the two-dimensional array of 2 rows and 5 columns. Here, the reshape() function is called by using the module name, np. The second reshape() function is used to convert the one-dimensional array into the two-dimensional array of 5 rows and 2 columns. Here, the reshape() function Is called by using the NumPy array named np_array.

# Import NumPy

import numpy as np

# Create a NumPy array of range values

np_array = np.arange(10)

#  Print the NumPy array values

print("The values of NumPy array : \n", np_array)

# Reshape the array with 2 rows and 5 columns

new_array = np.reshape(np_array, (2, 5))

# Print the reshaped values

print("\nThe reshaped array with 2 rows and 5 columns : \n", new_array)

# Reshape array with 5 rows and 2 columns

new_array = np_array.reshape(5, 2)

# Print the reshaped values

print("\nThe reshaped array with 5 rows and 2 columns : \n", new_array)

Output:

The following output will appear after executing the above script. The first output shows the main array. The second and third output shows the reshaped array.

Example-2: Convert one-dimensional array to three-dimensional array

The following example shows the reshape() function to convert a one-dimensional NumPy array into a three-dimensional NumPy array. array() function is used in the script to create a one-dimensional array of 12 elements. reshape() function is used to convert the created one-dimensional array into the three-dimensional array. Here, the reshape() function Is called by using the NumPy array named np_array.

# Import NumPy

import numpy as np

# Create a NumPy array using list

np_array = np.array([7, 3, 9, 11, 4, 23, 71, 2, 32, 6, 16, 2])

#  Print the NumPy array values

print("The values of NumPy array : \n", np_array)

# Create a three-dimensional array from a one-dimensional array

new_array = np_array.reshape(2, 2, 3)

# Print the reshaped values

print("\nThe reshaped 3D array values are : \n", new_array)

Output:

The following output will appear after executing the above script. The first output shows the main array. The second output shows the reshaped array.

Example-3: Reshape NumPy array based on ordering

The following example shows the reshape() function to convert a one-dimensional NumPy array into a two-dimensional NumPy array with different types of orders. arange() function is used in the script to create a one-dimensional array of 15 elements. The first reshape() function is used to create a two-dimensional array of 3 rows and 5 columns with C-style ordering. The second reshape() function is used to create a two-dimensional array of 3 rows and 5 columns with Fortran-style ordering.

# Import NumPy

import numpy as np

# Create a NumPy array of range values

np_array = np.arange(15)

#  Print the NumPy array values

print("The values of NumPy array : \n", np_array)

# Reshape the array based on C-style ordering

new_array1 = np.reshape(np_array, (3, 5), order='C')

# Print the reshaped values

print("\nThe reshaped 2D array values based with C-style ordering are : \n", new_array1)

# Reshape the array based on Fortran-style ordering

new_array2 = np.reshape(np_array, (3, 5), order='F')

# Print the reshaped values

print("\nThe reshaped 2D array values-based with Fortran-style ordering are : \n", new_array2)

Output:

The following output will appear after executing the above script. The first output shows the main array of values. The second output shows the array values with row-based ordering. The third output shows the array values with column-based ordering.

Conclusion

The ways of converting the array from one shape to another shape by using the reshape() function have been described in this tutorial. The purpose of using the reshape() function will be cleared after practicing the examples of this tutorial, and the readers will be able to use this function in their python script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.