← Back to Python

All Topics

Advertisement

Learn/Python/Data Science

NumPy Broadcasting

Topic: NumPy

Advertisement

Introduction

Broadcasting enables NumPy to work with arrays of different shapes during arithmetic operations.

Basic Broadcasting

import numpy as np

# Scalar to array
arr = np.array([1, 2, 3])
print(arr + 5)  # [6, 7, 8]

# Array to 1D
a = np.array([[1], [2], [3]])
b = np.array([1, 2, 3])
print(a + b)
# [[2, 3, 4],
#  [3, 4, 5],
#  [4, 5, 6]]

Broadcasting Rules

  1. Dimensions match from right to left
  2. Size must be 1 or equal
  3. If size is 1, it stretches to match
A = np.ones((3, 4))
B = np.arange(4).reshape(1, 4)
C = np.arange(3).reshape(3, 1)

print(A + B)  # Works
print(A + C)  # Works

Practical Examples

# Normalize columns
X = np.random.randn(100, 5)
X_normalized = (X - X.mean(axis=0)) / X.std(axis=0)

# Outer product
u = np.arange(4)
v = np.arange(3)[:, np.newaxis]
outer = u + v  # 3x4 matrix

Practice Problems

  1. Normalize data using broadcasting
  2. Compute outer products
  3. Add vector to each row/column
  4. Implement batch operations
  5. Create multiplication table

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →