Easy Tutorial
❮ Numpy Array From Existing Data Numpy Matrix ❯

NumPy Broadcasting

Broadcasting is the method that NumPy uses to allow arithmetic operations between arrays of different shapes. The arithmetic operations on arrays are usually performed on corresponding elements.

If two arrays, a and b, have the same shape, i.e., a.shape == b.shape, then the result of a * b is the element-wise multiplication of a and b. This requires the same number of dimensions and equal lengths in each dimension.

Example

import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a * b
print(c)

Output:

[ 10  40  90 160]

When the shapes of the two arrays in an operation are different, NumPy automatically triggers the broadcasting mechanism. For example:

Example

import numpy as np

a = np.array([[ 0, 0, 0],
              [10, 10, 10],
              [20, 20, 20],
              [30, 30, 30]])
b = np.array([0, 1, 2])
print(a + b)

Output:

[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]

A 4x3 two-dimensional array added to a one-dimensional array of length 3 is equivalent to repeating array b four times in two dimensions and then performing the operation:

Example

import numpy as np

a = np.array([[ 0, 0, 0],
              [10, 10, 10],
              [20, 20, 20],
              [30, 30, 30]])
b = np.array([1, 2, 3])
bb = np.tile(b, (4, 1))  # Repeat b along each dimension
print(a + bb)

Output:

[[ 1  2  3]
 [11 12 13]
 [21 22 23]
 [31 32 33]]

Broadcasting Rules:

Simple Understanding: For two arrays, compare each dimension (ignore if one array does not have the current dimension) and satisfy:

If these conditions are not met, a "ValueError: frames are not aligned" exception is thrown.

❮ Numpy Array From Existing Data Numpy Matrix ❯