NumPy Lesson 8 – Vectorized Computation | Dataplexa

Vectorized Computation in NumPy

One of the most important reasons NumPy is widely used in data science and machine learning is vectorized computation.

Vectorization allows NumPy to perform operations on entire arrays without using explicit Python loops, resulting in faster and cleaner code.


What is Vectorized Computation?

Vectorized computation means applying operations to all elements of an array at once, instead of processing them one by one using loops.

NumPy internally uses optimized C code, which makes vectorized operations significantly faster than standard Python loops.


Traditional Python Loop Example

Let’s start with a simple example using a Python loop.

numbers = [10, 20, 30, 40, 50]
result = []

for n in numbers:
    result.append(n * 2)

print(result)

Output:

[20, 40, 60, 80, 100]

This works, but it becomes slow and verbose when handling large datasets.


Vectorized NumPy Example

Now let’s do the same operation using NumPy.

import numpy as np

arr = np.array([10, 20, 30, 40, 50])
result = arr * 2

print(result)

Output:

[ 20  40  60  80 100]

The operation is simpler, more readable, and much faster.


Why Vectorization is Faster

Vectorized operations are faster because:

  • They avoid Python-level loops
  • They use optimized C implementations internally
  • They reduce interpreter overhead

This difference becomes huge when working with millions of values.


Vectorized Mathematical Operations

All arithmetic operations in NumPy are vectorized by default.

data = np.array([5, 10, 15, 20])

print(data + 3)
print(data * 4)
print(data / 2)

Output:

[ 8 13 18 23]
[20 40 60 80]
[ 2.5  5.   7.5 10. ]

Each operation applies automatically to all elements.


Vectorized Comparisons

Comparison operations also work in a vectorized way.

scores = np.array([35, 60, 75, 90])

passed = scores >= 50
print(passed)

Output:

[False  True  True  True]

These boolean arrays are often used for filtering data.


Vectorized Conditional Logic

You can combine vectorization with conditional logic using NumPy functions.

salary = np.array([30000, 45000, 60000, 80000])

updated_salary = np.where(salary < 50000, salary * 1.10, salary * 1.05)
print(updated_salary)

Output:

[33000. 47250. 63000. 84000.]

This replaces complex if-else loops with a single vectorized statement.


Real-World Example

Suppose you record daily temperatures in Celsius and want to convert them to Fahrenheit.

celsius = np.array([0, 10, 20, 30])
fahrenheit = (celsius * 9/5) + 32

print(fahrenheit)

Output:

[32. 50. 68. 86.]

Vectorization makes unit conversions simple and efficient.


Practice Exercise

Exercise

Create a NumPy array of monthly expenses and:

  • Increase each value by 15%
  • Check which expenses exceed 500
  • Calculate the average expense

Expected Outcome

You should be comfortable applying mathematical and logical operations without using loops.


What’s Next?

In the next lesson, you will learn how to reshape NumPy arrays and change their dimensions using reshape, flatten, and ravel.