datahacker.rs@gmail.com

#006B Vectorization and Broadcasting in Python

What is Vectorization?

A vectorization is basically the art of getting rid of explicit for loops whenever possible. With the help of vectorization, operations are applied to whole arrays instead of individual elements.

The rule of thumb to remember is to avoid using explicit loops in your code.

Deep learning algorithms tend to shine when trained on large datasets, so it’s important that your code runs quickly. Otherwise, your code might take a long time to get your result.

Here are some vectorization examples in Python.

The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. The simplest broadcasting example occurs when an array and a scalar value are combined in an operation. For example, if we have a matrix $$\textbf{A}$$ and scalar value $$b$$ then scalar b is being stretched during the arithmetic operation into an array which is the same shape as $$\textbf{A}$$, but that stretch is only conceptual. Numpy uses the original scalar value without making copies, so that broadcasting operations are as memory and computationally efficient as possible. Let’s see some examples.