# The numpy.reshape() function – All you need to get started!

Filed Under: NumPy

Hey, readers! Today, we will be having a look at one of the most interesting function of Python NumPy module – numpy.reshape() function.

So, let us begin!

## What is numpy.reshape() function?

Python NumPy module is useful in performing mathematical and scientific operations on the data. NumPy module deals with the data in the form of Arrays.

The `numpy.reshape() function` enables the user to change the dimensions of the array within which the elements reside. That is, we can reshape the data to any dimension using the reshape() function.

Moreover, it allows the programmers to alter the number of elements that would be structured across a particular dimension.

Let us now focus on the syntax of reshape() function in the below section.

## Syntax of reshape() function

Have a look at the below syntax!

```array.reshape(shape)
```
• array — The data structure to be reshaped (always an array!)
• shape — Integer tuple values that decide the dimension of the new array

The reshape() function does not alter the elements of the array. It only changes the dimensions of the array i.e. the schema/structure.

Now, let us try to visualize the changes in the dimension using the reshape() function through an example:

Let us consider an array arr = {1,2,3,4,5,6} having dimension 1×6. This array can be re-framed into the following forms:

3×2 dimension:

2×3 dimension:

6×1 dimension:

Let us now implement the concept of reshape() function through some examples as shown below.

## Examples of reshape() function

In the below example, we have created a 1-D numpy array using the function `numpy.array()`. Further, we have changed the dimensions of the array to 2×2.

```import numpy as np

a = np.array([1, 2, 3,4])
print("Elements of the array before reshaping: \n", a)

reshape = a.reshape(2,2)
print("\n Array elements after reshaping: \n", reshape)
```

Output:

```Elements of the array before reshaping:
[1 2 3 4]

Array elements after reshaping:
[[1 2]
[3 4]]
```

Now, we have created a 2 Dimensional Array and have changed the dimension of the array to a 1-D array by providing -1 as the argument to the reshape() function.

```import numpy as np

a = np.array([[1, 2, 3,4],[2,4,6,8]])
print("Elements of the array before reshaping: \n", a)

reshape = a.reshape(-1)
print("\n Array elements after reshaping: \n", reshape)
```

Output:

```Elements of the array before reshaping:
[[1 2 3 4]
[2 4 6 8]]

Array elements after reshaping:
[1 2 3 4 2 4 6 8]
```

Here, we have created an array using numpy.arange() function. Then, we have changed the dimension of the array to 2×3 i.e. 2 rows and 3 columns.

```import numpy as np

a = np.arange(6)
print("Elements of the array before reshaping: \n", a)

reshape = a.reshape(2,3)
print("\n Array elements after reshaping: \n", reshape)
```

Output:

```Elements of the array before reshaping:
[0 1 2 3 4 5]

Array elements after reshaping:
[[0 1 2]
[3 4 5]]
```

## Conclusion

By this, we have come to the end of this topic. Hope this article helps in understanding the concept well.

Feel free to comment below, in case you come across any question.

Till then, Happy Learning!!

## References

close
Generic selectors
Exact matches only
Search in title
Search in content