Thread
Broadcasting in NumPy is widely used, yet poorly understood❗️

Today we see:
- What is broadcasting
- Rules to apply broadcasting
- Examples of broadcasting
- Why it's Useful

A Thread 🧵👇
Broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations.

The smaller array is “broadcast” across the larger array, such that the 2 have compatible shapes.

Check this out👇
In the image below, scalar "b" is being stretched into an array with the same shape as "a".

But how do we generalise these things?

continue reading ... 📖
💫 General Rules:

1) Broadcasting starts with the trailing (i.e. rightmost) dimensions and works its way left .

2) Two dimensions are compatible, either when they are equal or one of them is 1.

Check out the examples 👇
Another example to make our understanding concrete. 👊
Try yourself! 🚀 👇
Why use broadcasting❓

Broadcasting provides a means of vectorising array operations so that looping occurs in C instead of Python.

It does this without making needless copies of data and usually leads to efficient algorithm implementations.
Thanks for checking out the thread!

If you enjoyed reading:

1) Find me at @akshay_pachaar for more content like this.

2) Like & RT the tweet below to share this thread with you audience 👇

Mentions
See All