Vectorization is a technique that:

  1. Makes your code shorter and easier to read
  2. Makes your code run much faster

It allows you to take advantage of:

Example Setup

Parameters and features with n = 3:

Vector notation:

$$ \vec{w} = \begin{bmatrix} w_1 \\ w_2 \\ w_3 \end{bmatrix}, \quad \vec{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} $$

Or more compactly:

$$ \vec{w} = [w_1, w_2, w_3], \quad \vec{x} = [x_1, x_2, x_3] $$

In Python (using NumPy):

import numpy as np

w = np.array([1.0, 2.5, -3.3])
b = 4
x = np.array([10, 20, 30])

n = w.shape[0]  # number of features

Note on indexing:

Math notation Python notation
$w_1$ (index starts at 1) w[0](index starts at 0)
$w_2$ w[1]
$w_3$ w[2]

Three Ways to Compute $f = \vec{w} \cdot \vec{x} + b$

Method 1: Without Vectorization (Manual)

Math idea:

$$ f = w_1 x_1 + w_2 x_2 + w_3 x_3 + b $$