datahacker.rs@gmail.com

#006B Vectorization and Broadcasting in Python

#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.
 

For more information about vectorization in NumPy click here.

Broadcasting 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.

For more information about broadcasting in Python look here.

In the next post we will learn about what a neural network is.

More resources on the topic:

Leave a Reply

Your email address will not be published. Required fields are marked *

17 + 5 =