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.
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.
2 * 2 = 4
and not 6
.However, we could reshape the array into an array that has 2 rows and 3 columns.
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)
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.
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.
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 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 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.
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.
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.
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)
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.
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 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
.
-1
to infer it.You can learn more about the related topics by checking out the following tutorials: