Fix: ValueError: operands could not be broadcast together with shapes [Python numpy]

Python numpy Code:

import numpy as np

array1 = np.array([10, 22, 12])
array2 = np.array([[4, 2], [16, 22]])

result = array1 + array2

Error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[51], line 6
      3 array1 = np.array([10, 22, 12])
      4 array2 = np.array([[4, 2], [16, 22]])
----> 6 result = array1 + array2

ValueError: operands could not be broadcast together with shapes (3,) (2,2) 

Reason for the Error:

    As we are trying to perform operations on arrays (which can be lists as well) that are of incompatible shapes.

Fix:

    We need to make sure that the shapes of the arrays you are performing operations on are compatible. If they are not, we reshape or transpose them to make the operation valid.

ValueError- operands could not be broadcast together with shapes

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!