Get startedGet started for free

Vectorized operations

1. Vectorized operations

No course on NumPy would be complete without discussing the immense power of vectorized operations. Let's get started!

2. A little help from C

When NumPy sums elements in an array, it does not add each element one by one, but rather adds all of them together at once. How? Recall NumPy's rule that all elements in an array must be the same data type. Because of this rule, NumPy is able to outsource tasks to C, a low-level programming language known for its speed and efficient memory usage. Sound familiar? Delegating tasks to C is a big reason for NumPy's own efficiency!

3. Vectorized operations

Employing the help of optimized C code this way is known as vectorization. This all occurs without the user having to prompt NumPy to vectorize: we've actually been using vectorized operations throughout this course! For example, the dot-sum method uses C under the hood to sum quickly and efficiently.

4. Speed compared to Python

Just how much faster is NumPy than Python? It depends on the task, but the time that NumPy takes to perform a task can be anywhere from ten to a hundred times faster. Plus, using vectorized operations greatly reduces the amount of code we have to write! For example, using Python to add the number three to each element in an array requires writing a for loop. It's a lot of code for an uncomplicated task, and for loops are slow by their very nature, since the loop must be executed for each element one at a time.

5. NumPy syntax

In NumPy, the syntax is more efficient: we use a plus sign and tell NumPy which single number we'd like to add to all array elements. In mathematics, a single number is often referred to as a scalar. NumPy uses this vocabulary as well: we've just added a scalar, three, to each element in our array, leveraging the power of C.

6. Multiplying by a scalar

We can use similar syntax to multiply an array by a scalar.

7. Adding two arrays together

We can also use vectorized operations between arrays of the same shape. Here, when we add two arrays of the same shape together, NumPy will add each element in the first array to the element at the same location in the second array.

8. Multiplying two arrays together

The same principle holds true for multiplying two arrays together, as well as subtracting and dividing operations: the operation will be performed between the elements at corresponding locations in each array.

9. Not just for math

While vectorized operations work extremely well with math and numbers, they are leveraged throughout NumPy; we used them to create Boolean masks and filter arrays back in the last chapter! Here's a Boolean mask indicating where array elements are greater than two, filtered using vectorized syntax.

10. Vectorize Python code!

We can even create our own vectorized functions from Python functions using np-dot-vectorize. For example, let's check whether each string element in an array has a length greater than two. We might expect the NumPy vectorized syntax for this to check whether the len of each element in the array is greater than two. But this doesn't work; instead, the code returns True, because the array has more than two elements. The reason our code behaved this way is that len is a Python function, not a NumPy function, so it can't vectorize. By feeding len without its trailing parentheses to np-dot-vectorize, we can convert it into a vectorized NumPy function! Then, passing the array as an argument yields the results we hoped for.

11. Let's practice!

Let's leverage some vectorized operations!