ValueError: cannot reshape array of size X into shape Y

Borislav Hadzhiev

Last updated: Apr 11, 2024

Reading timeยท4 min

**The NumPy "ValueError: cannot reshape array of size X into shape Y" is raised
when you pass an incorrect new shape to the numpy.reshape() method.**

**To solve the error, make sure the new shape is compatible with the original
shape.**

Here is an example of how the error occurs.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) # โ๏ธ ValueError: cannot reshape array of size 6 into shape (2,2) new_arr = arr.reshape((2, 2))`

We tried to reshape an array of size 6 into shape (2, 2).

The new shape is not compatible with the original shape, so the error is raised.

We can't reshape an array of size 6 into an array that has 2 rows and 2 columns because

`2 * 2 = 4`

and not `6`

.However, we could reshape the array into an array that has 2 rows and 3 columns.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) new_arr = arr.reshape((2, 3)) # [[1 2 3] # [4 5 6]] print(new_arr)`

The code for this article is available on GitHub

As shown in the screenshot, the new array has 2 rows and 3 columns.

The call to the `reshape()`

method succeeds because `2 * 3 = 6`

which is equal
to the size of the array.

You can use the ndarray.size attribute to get the size of the array.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) print(arr.size) # ๐๏ธ 6`

The multiplication of the arguments you pass to the `reshape()`

method has to be
equal to the size of the array.

Therefore, we can also reshape the array to 3 rows and 2 columns.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) print(arr.size) # ๐๏ธ 6 new_arr = arr.reshape((3, 2)) # [[1 2] # [3 4] # [5 6]] print(new_arr)`

The code for this article is available on GitHub

The array in the example has 3 rows and 2 columns.

Since `3 * 2 = 6`

and the size of the array is 6, everything works as expected.

The number of elements must be equal to the product of the number of rows and columns.

The array.reshape method takes a new shape parameter that can be an integer or a tuple of integers.

The new shape parameter has to be compatible with the original shape.

If the supplied parameter is an integer, then the result is a 1-D array of that length.

main.py

`import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.size) # ๐๏ธ 6 print(arr.reshape(6)) # ๐๏ธ [1 2 3 4 5 6]`

The `arr`

variable stores a two-dimensional array that has a `size`

of 6.

We used the `array.reshape()`

method to reshape the 2-D array to a 1-D array of
the same length.

You can also access the `size`

attribute to not have to hard-code the size of
the array.

main.py

`import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.size) # ๐๏ธ 6 print(arr.reshape(arr.size)) # ๐๏ธ [1 2 3 4 5 6]`

`-1`

to infer the shapeWhen using the `numpy.reshape()`

method, you can use a parameter of `-1`

to
infer the shape for one dimension.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) new_array = arr.reshape((2, -1)) # [[1 2 3] # [4 5 6]] print(new_array) print(new_array.shape) # ๐๏ธ (2, 3)`

The code for this article is available on GitHub

When the dimension is `-1`

, the value is inferred from the length of the array
and remaining dimensions.

The array has a length of 6 and we've specified that it should have 2 rows.

Therefore, NumPy can determine that it should have 3 columns.

This is because `6 / 2 = 3`

.

Similarly, if the array has a length of 6 and has 3 rows, then it must have 2 columns.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) new_array = arr.reshape((3, -1)) # [[1 2] # [3 4] # [5 6]] print(new_array) print(new_array.shape) # ๐๏ธ (3, 2)`

The code for this article is available on GitHub

The array has a length of 6, therefore `6 / 3 rows = 2 columns`

.

The number of elements must be equal to the product of the number of rows and columns.

Similarly, dividing the array's length by the number of rows will give you the number of columns.

In other words, if you have a one-dimensional array of shape `(100,)`

, you can
reshape it to `(2, 50)`

, `(50, 2)`

, `(10, 10)`

or `(2, 10, 5)`

because the
product is always equal to `100`

.

If you aren't sure about the value you should specify for one of the dimensions, simply use

`-1`

to infer it.You can learn more about the related topics by checking out the following tutorials:

- How to replace None with NaN in Pandas DataFrame
- FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version
- You are trying to merge on int64 and object columns [Fixed]
- Copy a column from one DataFrame to another in Pandas
- AttributeError: Can only use .str accessor with string values
- ValueError: zero-dimensional arrays cannot be concatenated
- ValueError: zero-dimensional arrays cannot be concatenated
- Object arrays cannot be loaded when allow_pickle=False
- ValueError: Columns must be same length as key [Solved]
- TypeError: Field elements must be 2- or 3-tuples, got 1
- ValueError: Expected 2D array, got 1D array instead [Fixed]
- Count number of non-NaN values in each column of DataFrame
- Pandas: Create new row for each element in List in DataFrame
- ValueError: Cannot merge a Series without a name [Solved]
- ValueError: Shape of passed values is X, indices imply Y