Eliminate slow Python loops and convert scalar logic into vectorized routines with np.vectorize().
Vectorization is the practice of replacing explicit Python `for` loops with array expressions that execute as single SIMD vector instructions at compiled C-speed.
A Python for-loop is sending 10,000 individual letters one-by-one with 10,000 postal trips. Vectorization is loading all 10,000 letters onto a supersonic cargo plane in a single flight.
Vectorization pushes array operations down to CPU registers (AVX-512 / NEON) capable of computing 8-16 floating-point numbers simultaneously in 1 clock cycle.
v_fn = np.vectorize(my_function); res = v_fn(arr)res = [];
for x in arr:
res.append(x * 2)res = arr * 2Never write Python loops to iterate through NumPy arrays for basic arithmetic.
Solve the objective below using NumPy vectorized syntax
Given `c = np.array([0, 100])`, convert to Fahrenheit with `(c * 9/5) + 32` and print.
[ 32. 212.]