BANA409BANA409

Vector Notation

In machine learning, vector notation is often used to describe various definitions and algorithm in a more concise way.

Use the following Latex, we can specify row vectors (which are just numpy array):

a=[1,3,5]\vec{a}=[1, 3, -5] b=[4,2,1]\vec{b}=[4, -2, -1]
# row vectors are simply numpy arrays
import numpy as np

a = np.array([1, 3, -5])
b = np.array([4, -2, -1])

Note that the arrow vector notation above is the default style in Latex.

Boldface style for the vector notation is used more often in Machine learning, as follows:

a=[1,3,5]\renewcommand{\vec}[1]{\mathbf{#1}} \vec{a}=[1, 3, -5] b=[4,2,1]\renewcommand{\vec}[1]{\mathbf{#1}} \vec{b}=[4, -2, -1]

We will use the boldface style for vectors in this course. You can change the default \vec style by using $\renewcommand{\vec}[1]{\mathbf{#1}}$ command. Or just use \mathbf for vectors.

Linear Algebra and Column Vector

You need to know the basic linear algebra to understand various vector/matrix calculations, such as dot product, transpose, etc.

In machine learning, vectors are often represented as column vectors instead of row vectors. A column vector's transpose is a row vector and vice versa.

a=[135]\mathbf{a} = \begin{bmatrix} 1 \\3 \\-5 \end{bmatrix} b=[421]\mathbf{b} = \begin{bmatrix} 4 \\-2 \\-1 \end{bmatrix}

The dot product can be written as a matrix multiplication:

ab=ab=[1,3,5][421]=3\mathbf{a} \cdot \mathbf{b} = \mathbf{a}^\top \mathbf{b} = [ 1, 3, -5] \begin{bmatrix} 4 \\ -2 \\ -1 \end{bmatrix} = 3
# row vector/1D array has shape (n,)
# column vector/2D numpy array has shape (n, 1)
 
# a and b are 1D array
a = np.array([1, 3, -5])
b = np.array([4, -2, -1])
print(a, b, a.shape, b.shape)

print('*' * 50)
# reshape a and b to be column vectors
a = a.reshape(3, 1)
b = b.reshape(3, 1)
print(a)
print(b)
print(a.shape, b.shape)

print('*' * 50)
# dot product
np.dot(a.T, b)

Notation Notes

We use the following notations:

  • m is the number of instances in the training dataset, e.g., m = 16512 for the housing dataset after splitting
  • x(i)\mathbf x^{(i)} is a vector of all the feature values of the ithi^{th} instance in the dataset and yiy^{i} is the corresponding target value

Recall the first two rows of data:

  • first row of training data: 452600=θ0+θ1322+θ28.3252+θ341.0452600 = \theta_0 + \theta_1 * 322 + \theta_2 * 8.3252 + \theta_3 * 41.0
  • second row of training data: 358500=θ0+θ12401+θ28.3014+θ321.0358500 = \theta_0 + \theta_1 * 2401 + \theta_2 * 8.3014 + \theta_3 * 21.0

The column vector representations of θ\theta is:

θ=[θ0θ1θ2θ3]\mathbf{\theta} = \begin{bmatrix} \theta_0 \\ \theta_1 \\ \theta_2 \\ \theta_3 \end{bmatrix}

The column vector representations of the first row of feature values x(1)\mathbf x^{(1)} is (note 1 is added for the error term θ0\theta_0):

x(1)=[13228.325241.0]\mathbf{x}^{(1)} = \begin{bmatrix} 1 \\ 322 \\ 8.3252 \\ 41.0 \end{bmatrix}

The target value is y1=452600y^1 = 452600

so the vectorized notation for the first row is:

y(1)=θx(1)y^{(1)} = \mathbf{\theta}^\top \mathbf{x}^{(1)}

Given the followings:

(x(1))=[1,322,8.3252,41.0](\mathbf{x}^{(1)})^\top= [1, 322, 8.3252, 41.0] (x(2))=[1,2401,8.3014,21.0](\mathbf{x}^{(2)})^\top= [1, 2401, 8.3014, 21.0] X=[(x(1))(x(2))(x(m))]=[1,322,8.3252,41.01,2401,8.3014,21.0]\mathbf{X} = \begin{bmatrix} (\mathbf{x}^{(1)})^\top \\ (\mathbf{x}^{(2)})^\top \\ \vdots \\ (\mathbf{x}^{(m)})^\top \end{bmatrix} = \begin{bmatrix} 1, 322, 8.3252, 41.0 \\ 1, 2401, 8.3014, 21.0 \\ \vdots \\ \vdots \end{bmatrix} y=[y(1)y(2)y(m)]=[452600358500]\mathbf{y} = \begin{bmatrix} y^{(1)} \\ y^{(2)} \\ \vdots \\ y^{(m)} \end{bmatrix} = \begin{bmatrix} 452600 \\ 358500 \\ \vdots \\ \vdots \end{bmatrix}

Now, we can put all rows together in vectorized form:

y=θX\mathbf{y} = \mathbf{\theta}^\top \mathbf{X}

On this page