The error ValueError Could Not Broadcast Input Array From Shape (X, X, X) Into Shape (X, X) occurs when you try to perform a broadcast operation on NumPy arrays that have different shapes. The error could be solved by taking the arrays that are compatible with the shape. In this article, we will discuss how we can understand ValueError: Could Not Broadcast Input Array From Shape (X, X, X) Into Shape (X, X) error and solve it using various possible solutions. Moreover, we will also list the possible reasons for getting this error.
Solve Could Not Broadcast Input Array From Shape (X, X, X) Into Shape (X, X)
The error is caused by the broadcast operation when performed on arrays that do not have compatible shapes. In NumPy, broadcasting is a process that helps us to perform various operations on different arrays. The first step of broadcasting, before it applies the operation, it actually converts the given arrays into a compatible shape. If the given arrays are not compatible, then we get ValueError: Could Not Broadcast Input Array From Shape error.

Here we will take various examples which give us the same error and then we will solve them.
ValueError could not broadcast input array from shape (224,224,3) into shape (224,224)
This error occurs when having a 3D array and it was not able to convert into a 2D shape. Let us first take an example and see why we are getting this error.
# importing numpy array
import numpy as np
# creating a list of arrays
a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]
# converting list to array
np.array(a)
Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[5], line 8
5 a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]
7 # converting list to array
----> 8 np.array(a)
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
In this case, the error occurs because the first two arrays in our list are 3D while the third list is just a 2D list and the NumPy was not able to convert the 2D into 3D and vice versa.
The error can be solved, if we use the same dimensional arrays as shown below:
# importing numpy array
import numpy as np
# creating a list of arrays
a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224, 3))]
# converting list to array
np.array(a)
As you can see, now we have arrays with the same dimensions.
ValueError could not broadcast input array from shape (224,224,3) into shape (224,)
Let us take another example and see why we are getting the error.
# creating an array
a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]
np.array(a)
Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[9], line 3
1 # creating an array
2 a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]
----> 3 np.array(a)
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,)
Now let us solve the issue by taking arrays with the same dimensions.
# creating an array
a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224,3))]
np.array(a)
Hopefully, these examples helped you to understand the reason for getting the error.
Summary
In this short article, we discussed the reasons for getting Could Not Broadcast Input Array From Shape (X, X, X) Into Shape (X, X) error and solved the issue by taking various examples. We learned that the error occurs because of having different shapes arrays and performing any operation on them.
Related Error
- ImportError: Cannot Import Name Markup From Jinja2
- FileNotFoundError: [Errno 2] No such file or directory in Python
- Typeerror: String Indices Must be Integers Error in Python
- TypeError: Unsupported Operand Type(s) for -: ‘list’ and ‘list’
- ValueError: You Must Pass a Freq Argument as Current Index has None