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.

## ValueError: Could Not Broadcast Input Array From Shape (X, X, X) Into Shape (X, X) – Possible Solutions

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.

### Case-1: 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.

### Case-2: 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 ValueError: 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 Articles

- AttributeError: Series object has no attribute reshape
- ModuleNotFoundError: No Module Named sklearn_genetic
- ModuleNotFounderror: No Module Named Pycocotools