# Reshape function to shape the array

**URL:** <https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333>\
**Category:** Supervised ML: Regression and Classification\
**Created:** [December 23, 2023, 5:13am UTC](https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333 "2023-12-23T05:13:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_Diao](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Dylan\_Diao](https://community.deeplearning.ai/u/Dylan_Diao)\
**Post date:** [December 23, 2023, 5:13am UTC](https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333/1 "2023-12-23T05:13:21Z")

</div>

I didnot understand why the(-1,2) functions same as (3,2)? where did this -1 come from?

`a = np.arange(6).reshape(-1, 2)`  
This line of code first created a _1-D Vector_ of six elements. It then reshaped that vector into a _2-D_ array using the reshape command. This could have been written:  
`a = np.arange(6).reshape(3, 2)`  
To arrive at the same 3 row, 2 column array. The -1 argument tells the routine to compute the number of rows given the size of the array and the number of columns.

---

<div class="post-metadata">

**Author:** ![hackyon](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/hackyon/32/64679_2.png) [@hackyon](https://community.deeplearning.ai/u/hackyon)\
**Post date:** [December 23, 2023, 5:44am UTC](https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333/2 "2023-12-23T05:44:23Z")

</div>

Yup, you are correct about the -1 argument. In this case, the `reshape(-1, 2)` is the same as `reshape(3, 2)`.

According to the [official documentation](https://numpy.org/devdocs/reference/generated/numpy.reshape.html#numpy.reshape), one of the dimensions in `reshape()` can be -1, in which case the routine/method will infer/compute the number automatically.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 23, 2023, 8:36pm UTC](https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333/3 "2023-12-23T20:36:31Z")

</div>

Right! The “-1” on reshape just means “use whatever is left here”. That’s a nice easy way to make the code more general. E.g. if you know you want 2 columns, but the number of rows could be different based on the size of the input, you can use that technique to avoid having to either “hard-code” the number of rows or to do the work of computing what it should be.

---

<div class="post-metadata">

**Author:** ![Dylan\_Diao](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Dylan\_Diao](https://community.deeplearning.ai/u/Dylan_Diao)\
**Post date:** [December 25, 2023, 5:43am UTC](https://community.deeplearning.ai/t/reshape-function-to-shape-the-array/525333/4 "2023-12-25T05:43:28Z")

</div>

aha that’s reasonable! Thank you!
