Notes for Machine Learning Specialization: Vectorization
This is a note for the Machine Learning Specialization.
From:
P22 and P23
Here are our parameters and features:
$$
\vec{w} =
\left[
\begin{matrix}
w_1 & w_2 & w_3
\end{matrix}
\right]
$$
$$
b \text{ is a number}
$$
$$
\vec{x} =
\left[
\begin{matrix}
x_1 & x_2 & x_3
\end{matrix}
\right]
$$
So here $n=3$.
In linear algebra, the index or the counting starts from 1.
In Python code, the index starts from 0.
1 | w = np.array([1.0, 2.5, -3.3]) |
Without vectorization $f_{\vec{w}, b}=w_1x_1+w_2x_2+w_3x_3+b$
1 | f = w[0] * x[0] + |
Without vectorization $f_{\vec{w}, b}=\sum_{j=1}^nw_jx_j$ + b
1 | f = 0 |
Vectorization $f_{\vec{w}, b}=\vec{w}\cdot\vec{x} + b$
1 | f = np.dot(w, x) + b |
Vectorization's benefits:
- Shorter code
- Faster running (parallel hardware)
P24
Ex.
P55
For loops vs. vectorization
1 | # For loops |
1 | # Vectorization |