Review: Vectorization and Masking

Module 3: NumPy

Session 3.8: Vectorization & Masking

import numpy as np
Exercise

We run this code. What will c be?

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a > b
  1. [0, 0, 0]

  2. [False, False, False]

  3. The code will cause an error.

Exercise

We run this code. What will c be?

a = np.array([[1], [2], [3]])
b = np.array([4, 5, 6])
c = a > b
  1. [0, 0, 0]

  2. [False, False, False]

  3. The code will cause an error.

This works because of NumPy’s broadcasting rules. When you compare arrays of different shapes, NumPy tries to make their shapes compatible by “broadcasting” them to a common shape.

Exercise

We run this code. What will c be?

a = np.array([1, 2, 3])
b = np.array([4, 5, 6, 7])
c = a > b
  1. [0, 0, 0]

  2. [False, False, False]

  3. The code will cause an error.

The error occurs because the shapes of a and b are not compatible for broadcasting in this case.

Exercise

Given any array arr, how would you filter out all elements greater than 3 without a for loop?

# Example
arr = np.array([1, 2, 3, 4, 5])
# Output: [1, 2, 3]

# Option A
print(arr[arr > 3])

# Option B
print(arr[arr <= 3])

# Option C
print(arr[:3])
Exercise

What will be the output of the following code?

arr = np.array([1, 2, 3, 4, 5])
mask = arr % 2 == 0
print(arr[mask])
  1. [2, 4]

  2. [1, 3, 5]

  3. [0, 2, 4]

Exercise

How would you replace values greater than 3 in an array with the value 10?

arr = np.array([1, 2, 3, 4, 5])

# Option A
arr[>3] = 10

# Option B
for value in arr:
  if value > 3:
    value = 10

# Option C
arr[arr > 3] = 10
# How would we do this with a list?
ls = [1, 2, 3, 4, 5]

for value in ls:
  if value > 3:
    value = 10

print(ls)
Exercise

Which line of code below correctly filters all odd numbers from the array?

arr = np.array([1, 2, 3, 4, 5, 6])

# Option A
evens = arr[arr % 2 != 0]

# Option B
evens = arr[arr == 2]

# Option C
evens = arr[arr != 0]
Exercise

The following code should output the positive odd numbers. In the example, we expect [1, 3]. When we run the code, it raises IndexError: boolean index did not match indexed array along dimension 0. Explain the error(s) and fix this code.

arr = np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4])
mask_odd = arr % 2 != 0
mask_positive = arr[mask_odd] > 0
result = arr[mask_positive]
print(result)
Exercise

The following code should return the elements of the array that are numbers higher than 2. When we run it, it raises UFuncTypeError: ufunc 'greater' did not contain a loop with signature matching types. What is wrong? (No need to fix this)

arr = np.array(["a", 1, "b", 2, "c", 3, "d", 4])
mask_int = arr.dtype == int
mask_positive = arr > 2
mask = mask_int & mask_positive
result = arr[mask]
print(result)
# Remember: "np.array()" will change all elements to be the same type
arr = np.array(["a", 1, "b", 2, "c", 3, "d", 4])

print(arr)
# A list does not change its content's types
ls = ["a", 1, "b", 2, "c", 3, "d", 4]

# This for loop works for a list, but not for an array
for element in ls:
  if type(element) == int:
    if element > 2:
      print(element)

Want More?