Block 16: Vectorized Operations & Broadcasting
Apply operations to entire arrays without loops.
Concepts
- Element-wise arithmetic: +, -, *, /, **
- Broadcasting: scalar to array, (1,n) to (m,n)
- Universal functions (ufuncs): np.sqrt, np.log, np.exp
- Understanding shape compatibility rules
Code Examples
See exercise below.
Exercise
Convert an array of Celsius temps to Fahrenheit vectorized. Given a 3×4 matrix of exam scores, subtract each student's mean (row-wise) using broadcasting.
Homework
Draw (or write) the shape diagram for adding a (3,1) array to a (1,4) array. What shape results? Thursday