Broadcasting Illustration

Rumman Ansari   Software Engineer   2023-01-13   90 Share
☰ Table of Contents

Table of Content:


Broadcasting Illustration

Broadcasting Illustration
Figure:

This GIF demonstrates the internal mechanism of broadcasting

Broadcasting
  • NumPy operations are carried out on pairs of arrays (or vectors) on an element-by-element basis. Thus it is important that dimensions of two arrays must be same (or for dot product the inner dimension should match).

  • This constraint is relaxed in Python when one of the matrices is of shape (m x n). The other one has to be of a shape (1 x n) or (m X 1) or just a scalar number.

    • When it is (1 x n) matrix (row vector), then it gets replicated itself column-wise to become (m x n) matrix.

    • When it is (m x 1) matrix (column vector), it gets replicated row-wise to become (m x n) matrix.

    • If it is a scalar number, then it gets converted to (m x n) matrix where each of the element is equal to the scalar number.

  • Broadcasting also works when you want to apply the same function to each of the elements of a matrix or a vector. All you need to do is to just pass the matrix as an argument to the function.

Broadcasting Examples

1. Multiplication of a matrix and a row vector


a = np.array([[10, 10, 10], [20, 20, 20], [30, 30, 30]])

b = np.array([1, 2, 3])

c = a * b

print(c)



output:

[[10 20 30]

 [20 40 60]

 [30 60 90]]

2. Addition of a matrix and a scalar



a = np.array([[10, 10, 10], [20, 20, 20], [30, 30, 30]])

b = 1

c = a + b

print(c)



output:

[[11 11 11]

 [21 21 21]

 [31 31 31]]

3. Element-wise function call


def exp(x, n):

  return x ** n



a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(exp(a, 2))



output:

[[ 1  4  9]

 [16 25 36]

 [49 64 81]]

#Note that each element of array **a** has been raised to power 2
ndarray
  • A vector is an array of rank one (single dimension).

  • A vector or an array of vectors can be represented as numpy.ndarray(n-dimensional array) object using NumPy's array() function.

  • Following are the advantages of representing an array as numpy.ndarray object:

    • It supports broadcasting.

    • It provides lots of built-in functions to perform vectorized operations such as dot product, reshaping, and element-wise product.

  • If an ndarray is of shape (m x 1), it is known as a column vector and if it is of shape (1 x n), it is known as a row vector.

  • If a matrix has one dimension (m x 1) or (1 x m), it is called rank 1 matrix. If it has two dimensions of shape (m x n), where m > 1and n > 1, it's called rank 2 matrix. In general, if a matrix has n dimensions, it's called rank n matrix.