semaphoreP/codeastro

Day 3 - introducing vectorization as a term

Closed this issue · 2 comments

Under "Some Ways to Speed up Your Code", vectorization might be a good term to introduce. I remember in earlier research projects hearing it thrown around a lot and not knowing what it actually means. It says to replace for and while loops. Perhaps you could give an example of a for loop and how to replace it in a vectorized form with a mask. For example,

import numpy as np
# add 5 to each value in a list that's greater than 6
arr = np.asarray([1, 4, 6, 7, 8, 10, 2])
# first option (much slower for longer arrays) 

for i, elem in enumerate(arr):
   if elem > 6:
      arr[i] += 5
print(arr)
arr_2 = np.asarray([1, 4, 6, 7, 8, 10, 2])

# second option (better) 
mask_arr = arr_2 > 6 
arr_2[mask_arr] += 5
print(arr_2)

Great suggestion

Again, great suggestion! Added this example to the profiler notebook in PR #153