An array may have one or more dimensions for storing the data and sometimes we need to manipulate or change the structure of our data, i.e., the dimension of the array containing data, for this purpose, NumPy provided us with numpy.reshape() function for reshaping an array.
The most common use case of reshaping the arrays is to make an array compatible with others so that it can be possible to perform matrix multiplication and operations such as element-wise operations.
In this article, we will understand Python numpy.reshape() function with multiple examples. Let’s first look at this function’s syntax, parameters and return value.
Also Read: Numpy Broadcasting (With Examples)
Introduction to numpy.reshape() Function
The numpy.reshape() function in NumPy is used to change the shape (dimensions) of an existing array without modifying its data. It allows us to create a new view of the array with a different shape. Reshaping is a fundamental operation in numerical computing, as it enables us to prepare data for various computations and analyses.
Syntax:
array.reshape(shape)
Parameters:
- array: The original array that we want to reshape.
- shape: The new shape (dimensions) we want to give to the array. It can be specified as a tuple or as separate integer arguments.
Return:
Array which got reshaped without changing the data.
NumPy Array Reshaping
Let’s see how we can use this function to reshape the Numpy array. For reshaping an array we must first have to create an array.
Creating a 1D Array for Reshaping
Let’s see an example of creating a 1D array for reshaping later.
Example:
We are creating a 1D array with 12 elements in it.
import numpy as np
np1= np.array([1,2,3,4,5,6,7,8,9,10,11,12])
print(np1)
print(np1.shape)
Here we first imported the NumPy as np, then we created a 1D array and saved it into a variable np1 and after that, we printed the np1 and shape of the np1 where we got shape as (12, ) means 12 rows and 0 columns which is a 1D array.
Output:
The array np1 is 1D. We can arrange the same data contained in np1 with a different number of dimensions such as (3,4), (4,3), (6,2), (12,1), (2,3,2), etc.
Let us now understand how we can use numpy.reshape() with some examples to reshape the above 1D array np1.
Reshaping 1D Array to 2D Array
Here we will reshape the above 1D array np1 with 12 elements into the 2-D array.
The dimensions we will be taking for our 2D array will be 3 rows and 4 columns.
Example:
np2 = np1.reshape(3,4)
print(np2)
print(np2.shape)
In the above code, we have first provided the dimension of the newly reshaped array which is to have 3 rows and 4 columns. Then we printed the np2 array with its shape and we got the 2D array with its shape means dimension as (3,4) which means our new 2D array contains 3 rows and 4 columns as expected.
Output:
Reshaping 1D Array to 3D Array
Here we will reshape the above 1D array np1 into a 3D array.
Example:
np3 = np1.reshape(2,3,2)
print(np3)
print(np3.shape)
In the above code, we provided the shape or the dimension of the new array as (2,3,2) in the reshape() function which means the new array should be a 3D array as we provided 3 dimensions and then we saved the result into the np3 variable. Lastly, we printed the np3 variable with its shape and we got a 3D array with its shape as (2,3,2) as expected.
Output:
Flattening the Array
Converting a multidimensional array into a 1D array is called Flattening and it can be done by providing -1 into the reshape() function.
Example:
np4 = np3.reshape(-1)
print(np4)
print(np4.shape)
In the above code, we provided -1 into np3.reshape() means it will reshape the np3 array which is a 3D array into the 1D array and we saved this result into the np4 variable. Lastly, we printed the np4 array with its shape and we got a 1D array with 12 elements and the shape as (12, ) means 12 rows and 0 columns. So we came where we started from, we got our 1D array back.
Output:
Summary
In this tutorial, we have discussed numpy.reshape() function provided by Python’s NumPy library and explored reshaping a 1D array into a 2D or 3D array and also flattening the arrays using numpy.reshape() function with examples. After reading this tutorial, we hope you can easily reshape an array in Python.
Reference
https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape