Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

# Python numpy.reshape() function Hey, folks! Hope you all are doing well. In this article, we will be understanding the working of Python numpy.reshape() function.

As we all know, Python NumPy module provides us with huge amount of functions to manipulate and deal with mathematical data. Here, we will be unveiling the functioning of Numpy reshape() function.

So, let us get started!

## Working of Python numpy.reshape() function

`Python numpy.reshape() function` enables us to reshape an array i.e. change the dimensions of the array elements. Reshaping an array would help us change the number of data values that reside in a particular dimension.

An important point to note is that the reshape() function retains the size of the array i.e. it makes no change in the number of array elements.

Let us now understand the structure of numpy.reshape() function in the upcoming section.

## Syntax of Python numpy.reshape() function

```array-name.reshape(shape)
```
• `shape`: It is the tuple of integer values, according to which the elements are reshaped.

Let us consider an example to understand the process to pass the shape to the reshape() function.

If we have 16 elements in the input array, so we need to pass such integer values as tuple to the shape parameter that the product of those tuple values equates to the number of elements i.e. 16.

The shape parameter can have the following possibilities:

• [2,8]
• [8,2]
• [4,4]
• [16,1]
• [1,16]
• [4,2,2]

Let us now go through the below examples to have a better understanding about numpy.reshape() function.

## Implementing Python numpy.reshape() with examples

In the below example, we have created an 1-D array of 16 elements using numpy.arange() function.

Further, we have reshaped the dimensions of the array into a 2-D array of 4 elements per dimension using reshape() function.

```import numpy as np

arr = np.arange(16)
print("Array elements: \n", arr)

res = np.arange(16).reshape(4, 4)
print("\nArray reshaped as 4 rows and 4 columns: \n", res)
```

Output:

```Array elements:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Array reshaped as 4 rows and 4 columns:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]
```

Now, we have reshaped and converted the 1-D array into array with 2 elements per dimension.

```import numpy as np

arr = np.arange(16)
print("Array elements: \n", arr)

res = np.arange(16).reshape(4,2,2)
print("\nArray reshaped: \n", res)
```

Output:

```Array elements:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Array reshaped:
[[[ 0  1]
[ 2  3]]

[[ 4  5]
[ 6  7]]

[[ 8  9]
[10 11]]

[[12 13]
[14 15]]]
```

In the below example, we have converted the 2-D array into an array of single dimension by passing value as -1 to the reshape() function.

```import numpy as np

arr = np.array([[1, 2, 3,4], [10, 11, 12,13],[5,6,7,8]])
print("Array elements: \n", arr)

res = arr.reshape(-1)
print("\nArray reshaped as 1-D Array: \n", res)
```

Output:

```Array elements:
[[ 1  2  3  4]
[10 11 12 13]
[ 5  6  7  8]]

Array reshaped as 1-D Array:
[ 1  2  3  4 10 11 12 13  5  6  7  8]
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below in case, you come across any doubt. Happy Learning!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7602629,"math_prob":0.987438,"size":3063,"snap":"2020-45-2020-50","text_gpt3_token_len":904,"char_repetition_ratio":0.17718208,"word_repetition_ratio":0.14051095,"special_character_ratio":0.32778323,"punctuation_ratio":0.14723033,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9906971,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-26T03:45:16Z\",\"WARC-Record-ID\":\"<urn:uuid:e28add19-044b-4c87-916c-3ed14c329ffb>\",\"Content-Length\":\"121739\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4796bb51-c297-457c-bac2-45bf5d4d14b3>\",\"WARC-Concurrent-To\":\"<urn:uuid:faf9d395-4b68-49e0-9d9c-3c6d2336ab5f>\",\"WARC-IP-Address\":\"45.79.77.230\",\"WARC-Target-URI\":\"https://www.askpython.com/python-modules/numpy/python-numpy-reshape-function\",\"WARC-Payload-Digest\":\"sha1:D5ALTZ5JZF2ULIKSFAC6FKCYAQGGTBKT\",\"WARC-Block-Digest\":\"sha1:4F6EADTB72ELALCSHWNZJNAOR4BTFZUQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141186414.7_warc_CC-MAIN-20201126030729-20201126060729-00139.warc.gz\"}"}