TUTORIAL
NumPy Tutorial - NumPy Math Operation and Broadcasting
Learn NumPy arithmetic, the difference between elementwise and matrix multiplication, and how broadcasting combines compatible array shapes.
On this page
NumPy arithmetic applies familiar operators to arrays. The key distinction is that * multiplies corresponding elements, while matrix multiplication combines rows and columns. Broadcasting lets compatible shapes participate in one operation without manually copying values. The examples below show the operators, the shape rule, and how to understand a shape-mismatch error.
NumPy arithmetic operations
For arrays with compatible shapes, +, -, *, and / operate element by element. With two 2-by-2 arrays, each result position uses the values at that same position in both inputs. Division uses the usual elementwise quotient, so integer inputs can produce floating-point results. These operators return a result array; they do not change the input arrays in place.
A scalar can also be combined with every element of an array. NumPy treats a scalar as having no dimensions and applies it across the array, which is a simple case of broadcasting. The same compatibility rule applies to addition, subtraction, multiplication, and division.
Elementwise multiplication and matrix multiplication
The * operator is elementwise multiplication: the operands’ shapes must be equal or compatible under broadcasting, and each output element is the product of a pair of corresponding values. It does not compute a matrix product. For matrix multiplication, use the @ operator or np.dot. For two-dimensional arrays, @ combines a row from the left operand with a column from the right; the inner dimensions must agree.
Run this self-contained example to compare the operations and inspect the resulting shapes:
import numpy as np
left = np.array([[1, 2], [3, 4]])
right = np.array([[5, 6], [7, 8]])
print("addition:", left + right)
print("subtraction:", left - right)
print("elementwise:", left * right)
print("division:", left / right)
print("matrix product:", left @ right)
print("np.dot:", np.dot(left, right))
matrix = np.array([[1, 2, 3], [4, 5, 6]])
row = np.array([10, 20, 30])
column = np.array([[100], [200]])
print("row result:", matrix + row)
print("column result:", matrix + column)
print("shapes:", matrix.shape, row.shape, column.shape)
addition: [[ 6 8]
[10 12]]
subtraction: [[-4 -4]
[-4 -4]]
elementwise: [[ 5 12]
[21 32]]
division: [[0.2 0.33333333]
[0.42857143 0.5 ]]
matrix product: [[19 22]
[43 50]]
np.dot: [[19 22]
[43 50]]
row result: [[11 22 33]
[14 25 36]]
column result: [[101 102 103]
[204 205 206]]
shapes: (2, 3) (3,) (2, 1)
The supplied runtime result shows elementwise arithmetic, matrix multiplication, and both broadcast directions. * multiplies corresponding values, while @ and np.dot produce the same matrix product for these two-dimensional inputs.
How NumPy broadcasting works
NumPy compares dimensions from right to left. See the official NumPy broadcasting guide for the full explanation. Two dimensions are compatible when they are equal or when at least one of them is 1. If one shape has fewer dimensions, NumPy treats the missing leading dimensions as 1 for this comparison. The output shape takes the larger size in each compatible position. A dimension of size 1 is repeated conceptually across the matching dimension; NumPy does not require you to write out a manually repeated array.
A scalar has shape (), not (1, 1). It can be combined with an array of any shape because its missing dimensions act like size 1 during broadcasting. For example, adding 1 to a (2, 3) array produces a (2, 3) result with one added to each element.
Broadcasting examples
A one-dimensional array such as np.array([10, 20, 30]) has shape (3,). When added to a (2, 3) array, its dimensions align on the right: 3 matches 3, and the missing leading dimension acts like 1. The row values are therefore added to each row of the matrix.
A column-shaped array such as np.array([[100], [200]]) has shape (2, 1). Against a (2, 3) matrix, the first dimensions match at 2, and the last dimension expands from 1 to 3. Each row’s column value is added across that row. The orientation matters: (3,) describes a row-like sequence for this operation, while (2, 1) explicitly describes a column with two rows.
Handling incompatible shapes
If a dimension pair is unequal and neither size is 1, broadcasting cannot proceed. For example, (2, 3) and (2,) align as (2, 3) and (1, 2): their final dimensions, 3 and 2, are incompatible. Catch the ValueError to keep the script running while displaying NumPy’s diagnostic:
import numpy as np
matrix = np.ones((2, 3))
vector = np.ones((2,))
try:
matrix + vector
except ValueError as error:
print(error)
For these operands, NumPy’s diagnostic is operands could not be broadcast together with shapes (2,3) (2,).
When this happens, inspect the actual shapes with an array’s .shape attribute, then compare dimensions from the right. Check whether you intended a row (3,), a column (3, 1), or a different arrangement; reshape an operand only when that matches the data’s meaning. Do not infer a column shape from a one-dimensional array’s length alone: np.array([10, 11, 12]) has shape (3,), whereas np.array([[10], [11], [12]]) has shape (3, 1).