Search⌘ K
AI Features

Broadcasting in NumPy

Explore how NumPy broadcasting allows operations between arrays of different shapes by automatically adjusting the smaller array. Understand the rules that determine broadcasting compatibility and see practical examples that help you perform vectorized array arithmetic with ease.

What is Broadcasting in NumPy?

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations.

General rules for Broadcasting

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions and works its way forward. Two dimensions of size N are compatible when

  1. they are equal, or
  2. one of them is the
...