Broadcasting

Broadcasting is a powerful feature in NumPy that allows arrays of different shapes to perform mathematical operations together.

It automatically expands the smaller array to match the shape of the larger array without copying data. This makes calculations faster and memory-efficient.

First, import NumPy:

import numpy as np

Simple Example of Broadcasting

Create an array:

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

Add a scalar value:

arr + 5

Output:

[6 7 8 9]

Here, the number 5 is automatically broadcasted to all elements.

Broadcasting with Two Arrays

Example:

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

Output:

[11 22 33]

Element-wise addition happens because both arrays have the same shape.

Broadcasting with Different Shapes

Example:

a = np.array([[1, 2, 3],
[4, 5, 6]])b = np.array([10, 20, 30])a + b

Output:

[[11 22 33]
[14 25 36]]

Here, b is broadcasted across each row of a.

Broadcasting Rules

NumPy follows these rules:

  1. If arrays have different dimensions, the smaller one is expanded.
  2. Dimensions are compatible if:
    • They are equal, or
    • One of them is 1.
  3. If dimensions are not compatible, an error occurs.

Example of incompatible shapes:

a = np.array([[1, 2, 3],
[4, 5, 6]])b = np.array([10, 20])a + b

This will produce a shape mismatch error.

Broadcasting with Column Vector

Example:

a = np.array([[1, 2, 3],
[4, 5, 6]])b = np.array([[10],
[20]])a + b

Output:

[[11 12 13]
[24 25 26]]

Here, b is broadcasted across columns.

Why Broadcasting is Important in Data Analytics

Broadcasting allows you to:

Normalize data
Scale features
Perform fast matrix operations
Apply transformations to entire datasets
Avoid writing loops

Key Takeaway

Broadcasting automatically expands smaller arrays to match larger ones during operations. It makes NumPy powerful, efficient, and ideal for large-scale data analysis.

Home » PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Broadcasting