Skip to content
Pablo Rodriguez

Vectorization

Vectorization is a technique that makes code both shorter and significantly more efficient by leveraging modern numerical linear algebra libraries and specialized hardware like GPUs.

Consider parameters and features:

  • w: [w_1, w_2, w_3] = [1.0, 2.5, -3.3]
  • b: 4
  • x: [x_1, x_2, x_3] = [10, 20, 30]
  • n: 3 features

Manual calculation for each parameter:

manual.py
f = w[0] * x[0] + w[1] * x[1] + w[2] * x[2] + b

This approach becomes impractical with hundreds or thousands of features.

loop.py
f = 0
for j in range(0, n):
f = f + w[j] * x[j]
f = f + b

Slightly better than manual but still not using vectorization.

vectorized.py
f = np.dot(w, x) + b
NumPy
  • Reduces multiple lines to a single line
  • More readable and maintainable
  • Less prone to coding errors
  • Parallel Processing: NumPy.dot() uses parallel hardware
  • GPU Acceleration: Can leverage graphics processing units
  • Optimized Libraries: Built-in optimizations for mathematical operations
  • t₀: Calculate w[0] * x[0]
  • t₁: Calculate w[1] * x[1]
  • t₂: Calculate w[2] * x[2]
  • Operations performed one after another
  • Single Step: All w[j] * x[j] multiplications happen simultaneously
  • Hardware Optimization: Specialized circuits add results efficiently
  • Significant Speedup: Especially noticeable with large datasets

For 16 parameters (w₁ through w₁₆), gradient descent updates can be vectorized:

manual_updates.py
w[0] = w[0] - 0.1 * d[0]
w[1] = w[1] - 0.1 * d[1]
# ... repeat for all 16 parameters
w[15] = w[15] - 0.1 * d[15]
vectorized_updates.py
w = w - 0.1 * d
  • NumPy Library: Essential for vectorized operations in Python machine learning
  • Hardware Utilization: Automatically uses available parallel processing capabilities
  • Scalability: Critical for handling large datasets efficiently

Vectorization represents one of the most important techniques for implementing efficient machine learning algorithms. Modern libraries like NumPy make it accessible while providing significant performance benefits through parallel hardware utilization.