In NumPy, if you try to multiply two NumPy arrays with different shapes using *, NumPy will attempt to broadcast the smaller array to the size of the larger array. If the dimensions are incompatible for broadcasting, the interpreter will throw the ValueError: operands could not be broadcast together with shapes.
To solve this error, you can use the dot()
method if you want to multiply two matrices, provided that the number of columns in the first matrix equals the number of rows in the second matrix. Alternatively, if you want to do broadcasting, ensure that the dimensions of the arrays are compatible by reshaping using numpy.reshape()
.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
ValueError: operands could not be broadcast together with shapes
In Python, a value is a piece of information stored within a particular object. We will encounter a ValueError in Python when using a built-in operation or function that receives an argument that is the right type but an inappropriate value. In this specific error, the data we use during the arithmetic operation is the correct type, ndarray, but the ndarrays have the wrong shapes for the arithmetic operation.
Example
Let’s look at an example where we have two NumPy arrays. We will reshape one of the arrays to a 2×2 matrix and the second to a 2×3 matrix.
import numpy as np A = np.array([4, 2, 9, 3]).reshape(2, 2) B = np.array([1, 5, 9, 7, 3, 4]).reshape(2, 3) print(A) print(B)
Let’s run the code to see the matrices:
[[4 2] [9 3]] [[1 5 9] [7 3 4]]
Next, we will try to multiply the two matrices using *.
product = A * B print(product)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 product = A * B 2 print(product) ValueError: operands could not be broadcast together with shapes (2,2) (2,3)
We throw the ValueError because we are using *, which is an arithmetic operation that works only if arrays are of equal shape or if the smaller array can be broadcast to the larger array size.
The general broadcasting rules are: When operating on two arrays, NumPy performs an element-wise comparison of their shapes, starting with the trailing or rightmost dimension and working its way left. Two dimensions are compatible when
- they are equal, or
- one of them is 1
The two matrices do not have the same value for their trailing dimension; matrix A has a trailing dimension of 2, and matrix B has a trailing dimension of 3. Therefore NumPy throws the ValueError.
Solution
The easiest way to multiply two multidimensional NumPy arrays is to use numpy.dot()
. For matrix multiplication, the number of columns in the first matrix must equal the number of rows in the second matrix. Let’s look at the revised code;
product = A.dot(B) print(product)
Let’s run the code to see the result:
[[18 26 44] [30 54 93]]
We successfully multiplied the two arrays and printed the product to the console.
NumPy Broadcasting
We may not want to perform matrix multiplication, in which case it is helpful to understand how to broadcast NumPy arrays correctly.
Broadcasting refers to NumPy’s ability to handle arrays of different shapes during arithmetic operations.
If two arrays are of the same shape, the NumPy can perform arithmetic operations with the two arrays with no issue.
Suppose the two arrays are of different shapes. In that case, element-wise arithmetic operations are not possible. Still, NumPy achieves this using broadcasting, where the smaller array is broadcast to the size of the larger array to enable arithmetic operations.
Let’s look at an example with two arrays of equal shape.
x = np.array([1, 2, 3]) y = np.array([2, 4, 6]) print(x * y)
Because the matrices are of equal shape, we can multiply them together:
[ 2 8 18]
Let’s look at an example where we want to multiply an array of size 1×3 with an array of size 1×1:
x = np.array([1, 2, 3]) y = 3 print(x * y)
In this case, NumPy broadcasts the smaller array to the larger array of 1×3. In other words, [3]
becomes [3, 3, 3]
. Let’s run the code to see the result:
[3 6 9]
Example of NumPy Broadcasting Failing
Let’s look at an example of broadcasting failing. We will try to add two arrays, where one array has the shape 1×2 and the second array has the shape 3×3.
import numpy as np arr1 = np.array([2, 8]).reshape(1,2) arr2 = np.arange(1,7).reshape(2,3) print(arr1) print(arr2)
[[2 8]] [[1 2 3] [4 5 6]]
Next, we will try to add the arrays together:
sum_arr = arr1+ arr2
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [9], in <cell line: 1>() ----> 1 sum_arr = arr1+ arr2 ValueError: operands could not be broadcast together with shapes (1,2) (2,3)
The error occurs because the dimensions are incompatible. The trailing dimension of arr2
is 3, whilst the trailing dimension of arr1
is 2.
Solution
As mentioned previously, NumPy compares the shapes of the two arrays element-by-element. The two dimensions are compatible when they are equal, or one of them is 1. To solve this error, we can reshape arr2
to 3×2 instead of 2×3. This shape will satisfy the broadcasting rules because the trailing dimensions will be equal (both 2), and the next dimension of arr1 from the left is 1. Let’s look at the revised code
import numpy as np arr1 = np.array([2, 8]).reshape(1,2) arr2 = np.arange(1,7).reshape(3,2) print(arr1) print(arr2)
[[2 8]] [[1 2] [3 4] [5 6]]
We can see that the second array is now 3×2. Let’s try to add the two arrays together:
sum_arr = arr1+ arr2 print(sum_arr)
[[ 3 10] [ 5 12] [ 7 14]]
We successfully added the two arrays together. The array arr1
was broadcast to the shape of arr2
to become [[2, 8], [2, 8], [2 8]]
.
Summary
Congratulations on reading to the end of this tutorial! The ValueError: operands could not be broadcast together with shapes occurs if you try to perform an arithmetic operation with two NumPy arrays of different shapes, but the dimensions are incompatible for broadcasting. In the case of matrix multiplication, you can use numpy.dot()
provided that the number of columns of the first matrix is equal to the number of rows in the second matrix. Otherwise, you can consult the rules for broadcasting and ensure that your arrays have compatible dimensions.
For further reading on arithmetic operations with NumPy arrays, go to the articles:
- How to Multiply Two Matrices in Python
- How to Solve Python ValueError: zero-dimensional arrays cannot be concatenated
- How to Solve Python ValueError: matmul: Input Operand Mismatch in Matrix Multiplication
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.