Preview

A lesson from Essential Linear Algebra for ML

£29/month

£29/month£29/month or £249/year · 30-day money-back guarantee

Get full access

Our content is best on a larger screen

Vector projection

The vector projection

Explanation

The scalar projection comp⁡w⃗v⃗\operatorname{comp}_{\vec{w}} \vec{v} is a signed length along the line through w⃗\vec{w}, from the origin to the point where a segment from the tip of v⃗\vec{v} meets the line at a right angle. The scalar projection is a number; the vector projection proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v} is a vector: the arrow along that same stretch of the line.

1234−1−2−3−41234−1−2−3−4
v→
w→

The purple arrow is the vector projection projw→v→. It lies along the line through w→, from the origin to the point where the dotted segment from the tip of v→ meets the line.

Drag the head of v→.

v→⋅w→>0, so projw→v→ points in the same direction as w→.
v→⋅w→<0, so projw→v→ points in the opposite direction to w→.
v→⋅w→=0, so v→ is orthogonal to w→ and projw→v→ is the zero vector.

The unit vector w⃗∥w⃗∥\dfrac{\vec{w}}{\|\vec{w}\|} has length 11 and points in the direction of w⃗\vec{w}. The scalar projection comp⁡w⃗v⃗\operatorname{comp}_{\vec{w}} \vec{v} is the signed length we need in that direction. Multiplying the two gives the vector projection:

proj⁡w⃗v⃗=(comp⁡w⃗v⃗)w⃗∥w⃗∥=v⃗⋅w⃗∥w⃗∥ w⃗∥w⃗∥=v⃗⋅w⃗∥w⃗∥2 w⃗.\begin{align*} \operatorname{proj}_{\vec{w}} \vec{v} &= \left(\operatorname{comp}_{\vec{w}} \vec{v}\right) \dfrac{\vec{w}}{\|\vec{w}\|} \\[6pt] &= \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|} \, \dfrac{\vec{w}}{\|\vec{w}\|} \\[6pt] &= \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \, \vec{w}. \end{align*}
The vector projection proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v}

Definition

For v⃗,w⃗∈Rn\vec{v}, \vec{w} \in \mathbb{R}^n with w⃗\vec{w} non-zero, the vector projection of v⃗\vec{v} onto w⃗\vec{w} is

proj⁡w⃗v⃗=v⃗⋅w⃗∥w⃗∥2 w⃗.\operatorname{proj}_{\vec{w}} \vec{v} = \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \, \vec{w}.

It is the vector along the line through w⃗\vec{w} whose signed length is comp⁡w⃗v⃗\operatorname{comp}_{\vec{w}} \vec{v}.

Example

Let v⃗=[−47]\vec{v} = \begin{bmatrix} -4 \\ 7 \end{bmatrix} and w⃗=[2−1]\vec{w} = \begin{bmatrix} 2 \\ -1 \end{bmatrix}.

Find proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v}.

Solution

We start with the dot product, multiplying matching components and adding the results:

v⃗⋅w⃗=(−4)(2)+7(−1)=−8−7=−15.\begin{align*} \vec{v} \cdot \vec{w} &= (-4)(2) + 7(-1) \\ &= -8 - 7 \\ &= -15. \end{align*}

Next we need ∥w⃗∥2\|\vec{w}\|^2, the sum of the squares of the components of w⃗\vec{w}:

∥w⃗∥2=22+(−1)2=4+1=5.\begin{align*} \|\vec{w}\|^2 &= 2^2 + (-1)^2 \\ &= 4 + 1 \\ &= 5. \end{align*}

Dividing the dot product by ∥w⃗∥2\|\vec{w}\|^2 gives the number that multiplies w⃗\vec{w}, and we multiply each component of w⃗\vec{w} by it:

proj⁡w⃗v⃗=v⃗⋅w⃗∥w⃗∥2 w⃗=−155[2−1]=−3[2−1]=[−63].\begin{align*} \operatorname{proj}_{\vec{w}} \vec{v} &= \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \, \vec{w} \\[6pt] &= \dfrac{-15}{5} \begin{bmatrix} 2 \\ -1 \end{bmatrix} \\[6pt] &= -3 \begin{bmatrix} 2 \\ -1 \end{bmatrix} \\[6pt] &= \begin{bmatrix} -6 \\ 3 \end{bmatrix}. \end{align*}

Therefore proj⁡w⃗v⃗=[−63]\operatorname{proj}_{\vec{w}} \vec{v} = \begin{bmatrix} -6 \\ 3 \end{bmatrix}.

The diagram shows proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v} as the purple arrow. Since proj⁡w⃗v⃗=−3w⃗\operatorname{proj}_{\vec{w}} \vec{v} = -3\vec{w}, it points in the opposite direction to w⃗\vec{w} and is three times as long as w⃗\vec{w}.

246−2−4−6246−2−4−6
v→
w→

Practice questions

5 questions

Let v⃗=[5−10]\vec{v} = \begin{bmatrix} 5 \\ -10 \end{bmatrix} and w⃗=[4−3]\vec{w} = \begin{bmatrix} 4 \\ -3 \end{bmatrix}.

Find proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v}.

Select the correct answer:

+ 4 more questions · unlocked with a subscription

Splitting a vector into its projection and an orthogonal part

Explanation

Of all the vectors along the line through w⃗\vec{w}, which is the best approximation to v⃗\vec{v}? It turns out to be the vector projection proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v}. But how good an approximation is it?

1234−11234−1
v→
w→

Step 1: The projection

The purple arrow is projw→v→, which lies along the line through w→:

projw→v→=1020[42]=[21].

Step 2: From projw→v→ to v→

The green arrow starts at the tip of projw→v→ and ends at the tip of v→, so

projw→v→ + green arrow = v→.

Subtracting projw→v→ from both sides, the green arrow is v→−projw→v→:

[13]−[21]=[−12].

Step 3: A right angle

v→−projw→v→ is orthogonal to w→, since its dot product with w→ is 0:

[−12]⋅[42]=−4+4=0.

Step 4: How good is the approximation?

The length of the green arrow measures how good the approximation is: it is the distance from the tip of v→ to the line through w→:

‖v→−projw→v→‖=(−1)2+22=5.

This holds for every v⃗\vec{v} and non-zero w⃗\vec{w} in Rn\mathbb{R}^n: the vector v⃗−proj⁡w⃗v⃗\vec{v} - \operatorname{proj}_{\vec{w}} \vec{v} always has dot product 00 with w⃗\vec{w}. This is because proj⁡w⃗v⃗\operatorname{proj}_{\vec{w}} \vec{v} ends at the point where a segment from the tip of v⃗\vec{v} meets the line through w⃗\vec{w} at a right angle, and v⃗−proj⁡w⃗v⃗\vec{v} - \operatorname{proj}_{\vec{w}} \vec{v} lies along that segment.

Splitting v⃗\vec{v} into its projection onto w⃗\vec{w} and a part orthogonal to w⃗\vec{w}

Theorem

For v⃗,w⃗∈Rn\vec{v}, \vec{w} \in \mathbb{R}^n with w⃗\vec{w} non-zero,

v⃗=proj⁡w⃗v⃗⏟a multiple of w⃗+(v⃗−proj⁡w⃗v⃗)⏟orthogonal to w⃗.\vec{v} = \underbrace{\operatorname{proj}_{\vec{w}} \vec{v}}_{\text{a multiple of } \vec{w}} + \underbrace{\left(\vec{v} - \operatorname{proj}_{\vec{w}} \vec{v}\right)}_{\text{orthogonal to } \vec{w}}.

The length ∥v⃗−proj⁡w⃗v⃗∥\|\vec{v} - \operatorname{proj}_{\vec{w}} \vec{v}\| is the distance from the tip of v⃗\vec{v} to the line through w⃗\vec{w}, measured at a right angle to the line.

ML Context

Linear regression predicts a target from an input, such as a house's price from its floor area. Over many examples, the targets form a vector y⃗\vec{y} and the inputs a vector x⃗\vec{x}. The simplest model predicts y⃗\vec{y} as cx⃗c\vec{x}, where the number cc is the model's parameter, learned from the examples.

The best value of cc gives the predictions proj⁡x⃗y⃗\operatorname{proj}_{\vec{x}} \vec{y}, and the prediction errors y⃗−proj⁡x⃗y⃗\vec{y} - \operatorname{proj}_{\vec{x}} \vec{y} are orthogonal to x⃗\vec{x}. Models with more inputs work the same way: their prediction errors are orthogonal to every input.

Example

Let v⃗=[55]\vec{v} = \begin{bmatrix} 5 \\ 5 \end{bmatrix} and w⃗=[31]\vec{w} = \begin{bmatrix} 3 \\ 1 \end{bmatrix}.

Write v⃗\vec{v} as a vector along w⃗\vec{w} plus a vector orthogonal to w⃗\vec{w}, and check that the second vector is orthogonal to w⃗\vec{w}.

Solution

The vector along w⃗\vec{w} is the projection

proj⁡w⃗v⃗=v⃗⋅w⃗∥w⃗∥2 w⃗.\operatorname{proj}_{\vec{w}} \vec{v} = \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \, \vec{w}.

Here v⃗⋅w⃗=5(3)+5(1)=20\vec{v} \cdot \vec{w} = 5(3) + 5(1) = 20 and ∥w⃗∥2=32+12=10\|\vec{w}\|^2 = 3^2 + 1^2 = 10, so

proj⁡w⃗v⃗=2010[31]=[62].\begin{align*} \operatorname{proj}_{\vec{w}} \vec{v} &= \dfrac{20}{10} \begin{bmatrix} 3 \\ 1 \end{bmatrix} \\[6pt] &= \begin{bmatrix} 6 \\ 2 \end{bmatrix}. \end{align*}

We subtract the projection from v⃗\vec{v}, component by component, to find the second part:

v⃗−proj⁡w⃗v⃗=[55]−[62]=[−13].\begin{align*} \vec{v} - \operatorname{proj}_{\vec{w}} \vec{v} &= \begin{bmatrix} 5 \\ 5 \end{bmatrix} - \begin{bmatrix} 6 \\ 2 \end{bmatrix} \\[6pt] &= \begin{bmatrix} -1 \\ 3 \end{bmatrix}. \end{align*}

To check that this part is orthogonal to w⃗\vec{w}, we take its dot product with w⃗\vec{w}:

(v⃗−proj⁡w⃗v⃗)⋅w⃗=(−1)(3)+3(1)=−3+3=0.\begin{align*} \left(\vec{v} - \operatorname{proj}_{\vec{w}} \vec{v}\right) \cdot \vec{w} &= (-1)(3) + 3(1) \\ &= -3 + 3 \\ &= 0. \end{align*}

The dot product is 00, so the second part is orthogonal to w⃗\vec{w}. Therefore

[55]=[62]⏟a multiple of w⃗+[−13]⏟orthogonal to w⃗.\begin{bmatrix} 5 \\ 5 \end{bmatrix} = \underbrace{\begin{bmatrix} 6 \\ 2 \end{bmatrix}}_{\text{a multiple of } \vec{w}} + \underbrace{\begin{bmatrix} -1 \\ 3 \end{bmatrix}}_{\text{orthogonal to } \vec{w}}.

Practice questions

4 questions

Let v⃗=[105]\vec{v} = \begin{bmatrix} 10 \\ 5 \end{bmatrix} and w⃗=[34]\vec{w} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}.

Which of these writes v⃗\vec{v} as a vector along w⃗\vec{w} plus a vector orthogonal to w⃗\vec{w}?

Select the correct answer:

+ 3 more questions · unlocked with a subscription

Memory is built by recalling, not by reading.

Every time you pull an answer from memory, the memory gets stronger. Reading doesn't do that. Nodeledge asks you questions on every step of every lesson, then asks again after days, weeks and months.

Get full access

£29/month or £249/year · 30-day money-back guarantee

See the learning path