The Geometric Structure of Vector Spaces
local graph
The Geometric Structure of Vector Spaces
Let’s revisit the Iris dataset introduced in the previous chapter! I want to test your intuition. I plotted the petal widths against the petal lengths while hiding the class labels in Figure 2.1:
import matplotlib.pyplot as pltfrom sklearn.datasets import load_iris
# Load the iris datasetiris = load_iris()data = iris.data
# Extract petal length (3rd column) and petal width (4th column)petal_length = data[:, 2]petal_width = data[:, 3]
with plt.style.context("seaborn-v0_8"): # Create the scatter plot plt.figure(figsize=(7, 7)) plt.scatter(petal_length, petal_width, color='indigo', alpha=0.8, edgecolor='none', s=70, marker='o') plt.xlabel('petal length (cm)') plt.ylabel('petal width (cm)') plt.show()
Figure 2.1: The “petal width” and “petal length” features of the Iris dataset
Even without knowing any labels, we can intuitively point out that there are probably at least two classes. Can you summarize your reasoning in a single sentence?
There are many valid arguments, but the most prevalent one is that the two clusters are far away from each other. As this example illustrates, the concept of distance plays an essential role in machine learning. In this chapter, we will translate the notion of distance into the language of mathematics and put it into the context of vector spaces.
Bookmark
2.1 Norms and distances
Section titled “2.1 Norms and distances”Previously, we saw that vectors are essentially arrows, starting from the null vector. In addition to their direction, vectors also have magnitude. For example, as we have learned in high school mathematics, the magnitude in the Euclidean plane is defined by

while we can calculate the distance between x and y as

(The function ∥⋅∥ simply denotes the magnitude of a vector.)

Figure 2.2: Magnitude in the Euclidean plane
The magnitude formula
can be simply generalized to higher dimensions by

However, just from looking at this formula, it is not clear why it is defined this way. What does the square root of a sum of squares have to do with distance and magnitude? Behind the scenes, it is just the Pythagorean theorem.
Recall that the Pythagorean theorem states that in right triangles, the squared length of the hypotenuse equals the sum of the squared lengths of the other sides, as illustrated by Figure 2.3.

Figure 2.3: The Pythagorean theorem
To put this into an algebraic form, it states that a2 + b2 = c2, when c is the hypotenuse of the right triangle, and a and b are its two other sides. If we apply this to a two-dimensional vector x = (x1,x2), we can see that the Pythagorean theorem gives its magnitude ∥x∥ =
.
This can be generalized to higher dimensions. To see what is happening, we are going to check the three-dimensional case, as illustrated by Figure 1.4. Here, we can apply the Pythagorean theorem twice to obtain the magnitude!

Figure 2.4: The Pythagorean theorem in three dimensions
For each vector x = (x1,x2,x3), we can take a look at the triangle determined by (0,0,0),(x1,0,0), and (x1,x2,0) first. The length of the hypotenuse can be calculated by
. However, the points (0,0,0), (x1,x2,0), and (x1,x2,x3) form a right triangle. Applying the Pythagorean theorem once again, we obtain

which is called the Euclidean norm. This is exactly what is going on in the general n-dimensional case.
The notions of magnitude and distance are critical in machine learning, as we can use them to determine the similarity between data points, measure and control the complexity of neural networks, and much more.
Is the Pythagorean theorem the only viable way to measure magnitude and distance? Certainly not.
Because Manhattan’s street layout is essentially a rectangular grid, its residents are famed for measuring distances in blocks. If something is two blocks to the north and three blocks east, it means that you have to travel two intersections to the north and three to the east to find it. This gives rise to a mathematically perfectly valid notion of measurement called Manhattan distance, defined by

When using the Manhattan distance, the shortest path between two points is not unique.

Figure 2.5: For the Manhattan distance, the shortest path between two points is not unique
Besides the Euclidean and Manhattan distances, there are several other metrics. Once again, we are going to step away from the concrete examples to take an abstract viewpoint.
If we talk about measurements and metrics in general, what are the properties that we expect from all of them? What makes a measurement distance? Essentially, there are three such traits:
- the distance should be nonnegative,
- it should preserve scaling (that is,
for all scalars
), - the distance straight from point
to
is always equal to or smaller than touching any other point
.
These are formalized by the notion of norms.
Definition 7. (Norms)
Let
be a vector space. A function 
on ℝn. The function ∥⋅∥p is called the p-norm. Showing that ∥⋅∥p is indeed a norm is a bit technical. Thus, we won’t go into the details. (The triangle inequality requires some work, but the other two properties are easy to see.)
We have already seen two special cases: the Euclidean norm (p = 2) and the Manhattan norm (p = 1). Both of them frequently appear in machine learning. For instance, the familiar mean squared error is just the scaled Euclidean distance between prediction and ground truth:

As mentioned before, the 2-norm, along with the 1-norm, is commonly used to control the complexity of models during training. To give a concrete example, suppose that we are fitting a polynomial f(x) = ∑ i=0mqixi to the data {(x1,y1),…,(xn,yn)} . To obtain a model that generalizes well to new data, we prefer our models to be as simple as possible. Thus, instead of using the plain mean squared error, we might consider minimizing the loss:

Showing that ∥⋅∥∞ is indeed a norm is a simple task and left to you for practice. (This is perhaps one of the most notorious sentences written in mathematical textbooks, but trust me, this is truly easy. Give it a shot! If you don’t see it, try the special case ℝ2.)
This is called the ∞-norm, and is strongly related to the p-norm that we have just seen. In fact, if we let the value p grow infinitely, ∥x∥p will be very close to ∥x∥∞ , ultimately reaching it at the limit.
Remark 2. (The ∞-norm as the limit of p-norm)
If you are already familiar with convergent sequences and limits, you can see that this is called the ∞-norm because

To show this, consider that

Since
≤ 1 by definition,

holds. Because

we can conclude that

This is the reason why the ∞-norm is considered a p-norm with p = ∞.
If you are not familiar with taking limits of sequences, don’t worry. We’ll cover everything in detail when studying single-variable calculus.
Example 3. ∞-norms can be generalized for function spaces. Remember C([0,1]), the vector space of functions continuous on [0,1]? We introduced this when talking about examples of vector spaces in Section 1.1.1. There, ∥⋅∥∞ can be defined as
![∥f∥∞ = sup |f (x )|. x∈[0,1]](https://static.packt-cdn.com/products/9781837027873/graphics/media/file108.png)
This norm can be defined on other function spaces, like C(ℝ), the space of continuous real functions. Since the maximum is not guaranteed to exist (as for the sigmoid function in C(ℝ)), the maximum is replaced with supremum. Hence, the ∞-norm is often called the supremum norm.
If you imagine the function as a landscape, the supremum norm is the height of the highest peak or the depth of the deepest trench (whichever is larger in absolute value).

Figure 2.6: The supremum norm
When encountering this norm for the first time, it might seem challenging to understand what this has to do with any notion of magnitude. However, ∥f −g∥∞ is a natural way to measure the distance between two functions f and g, and in general, magnitude is just the distance from 0.

Figure 2.7: The distance between two functions, given by the supremum norm
2.1.1 Defining distances from norms
Section titled “2.1.1 Defining distances from norms”Besides measuring the magnitude of vectors, we are also interested in measuring the distance between them. If you are at the location x in some normed space, how far is y? In normed vector spaces, we can define the distance between any x and y by

This is called the norm-induced metric. Thus, norms measure the distance from the zero vector, and the metric d measures the norm of the difference.
In general, we say that a function d : V ×V → [0,∞) is a metric if the following hold.
Definition 8. (Metrics)
Let
be a vector space and d : V ×V → [0,∞) be a function. d is a metric if the following conditions hold for all x,y,z ∈ V:
- Whenever d(x,y ) = 0 , we have x = y (positive definiteness).
- d (x, y) = d (y, x) (symmetry).
- d (x, z) ≤ d(x,y )+ d(y,z)(triangle inequality).
One of the immediate consequences of the definition is that if x≠y, then d(x,y)>0. (As the positive definiteness gives that d(x,y) = 1 implies x = y.)
Given the properties of norms, we can quickly check that d(x,y) = ∥x−y∥ is indeed a metric. Due to the linear structure of vector spaces, the norm-generated metric is invariant to translation. That is, for any x,y,z ∈V , we have

In other words, it doesn’t matter where you start: the distance only depends on your displacement. This is not true for any metric. Thus, norm-induced metrics are special. In our studies, we only deal with these special cases. Because of this, we won’t even talk about metrics, just norms.
In itself, a vector space is just a skeleton that provides a way to represent data. On top of this, norms define a geometric structure that reveals properties such as magnitude and distance. Both of these are essential in machine learning. For instance, some unsupervised learning algorithms separate data points into clusters based on their mutual distances from each other.
There is yet another way to enhance the geometric structure of vector spaces: inner products, also called dot products. We are going to put this concept under our magnifying glass in the next section.
Bookmark
2.2 Inner products, angles, and lots of reasons to care about them
Section titled “2.2 Inner products, angles, and lots of reasons to care about them”In the previous section, we imbued our vector spaces with norms, measuring the magnitude of vectors and the distance between points. In machine learning, these concepts can be used, for instance, to identify clusters in unlabeled datasets. However, without context, distance is often not enough. Following our geometric intuition, we can aspire to measure the similarity of data points. This is done by the inner product (also known as the dot product).
You can recall the inner product as a quantity that we used to measure the angle between two vectors in high school geometry classes. Given two vectors x = (x1,x2) and y = (y1,y2) from the plane, we defined their inner product by

for which it can be shown that
⟨x,y⟩ = ∥x∥∥y∥ cosα (2.1)
holds, where α is the angle between x and y. (In fact, there are two such angles, but their cosine is equal.) Thus, the angle itself can be extracted by

where arccosx is the inverse of the cosine function. We can use the inner products to determine whether two vectors are orthogonal, as this happens if and only if ⟨x,y⟩ = 0 holds. During our earlier encounters with mathematics, geometric intuition (such as orthogonality) came first, on which we built tools such as the inner product. However, if we zoom out and take an abstract viewpoint, things are exactly the opposite. As we’ll see soon, inner products emerge quite naturally, giving rise to the general concept of orthogonality.
In general, this is the formal definition of an inner product.
Definition 9. (Inner products and inner product spaces)
Let V be a real vector space. The function ⟨⋅,⋅⟩ : V × V → ℝ is called an inner product if the following holds for all x,y, z ∈ V and a ∈ ℝ:
- ⟨ax + y,z⟩ = a⟨x,z⟩ + ⟨y, z⟩ (linearity of the first variable).
- ⟨x,y ⟩ = ⟨y,x ⟩ (symmetry).
- ⟨x,x ⟩ >0 for all
(positive definiteness).
Vector spaces with an inner product are called inner product spaces.
Right off the bat, we can immediately deduce two properties. First,
(2.2)
As a special case, ⟨0,0⟩ = 0. Just like we have seen for norms, a bit more is true: if ⟨x,x⟩ = 0, then x = 0. This follows from positive definiteness and (2.2).
In addition, due to the symmetry and linearity of the first variable, inner products are also linear in the second variable. Because of this, they are called bilinear.
To familiarize ourselves with the concept, let’s see some examples!
Example 1. As usual, the canonical and most prevalent example of inner product spaces is ℝn, where the inner product ⟨⋅,⋅⟩ is defined by

This bilinear function is often called the dot product. Equipped with this, ℝn is called the n-dimensional Euclidean space. This is a central concept in machine learning, as data is most frequently represented in Euclidean spaces. Thus, we are going to explore the structure of this space in great detail throughout this book.
Example 2. Besides Euclidean spaces, there are other inner product spaces that play a significant role in mathematics and machine learning. If you are familiar with integration, in certain function spaces, the bilinear function

defines an inner product space with a very rich and beautiful structure.
The symmetry and linearity of ⟨f,g⟩ is clear. Only the positive definiteness seems to be an issue.
For instance, if f is defined by

then f≠0, but ⟨f,f⟩ = 0. This problem can be circumvented by “overloading” the equality operator and letting f = g if and only if ∫ −∞∞|f(x) −g(x)|2 dx = 0. Even though function spaces such as this play an important role in mathematics and machine learning, their study falls outside of our scope.
2.2.1 The generated norm
Section titled “2.2.1 The generated norm”Recall that the 2-norm in ℝn was defined by ∥x∥2 = (∑ i=1nxi2)1∕2 , which, according to our definition of the inner product there, equals
. This is not a coincidence. Inner products can be used to define norms on vector spaces.
To show exactly how, we need a simple tool: the Cauchy-Schwarz inequality.
Theorem 8. (Cauchy-Schwarz inequality)
Let V be an inner product space. Then, for any x,y ∈ V, the inequality

holds.
Proof. At this point, we don’t know much about the inner product except its core defining properties. So, we are going to use a little trick. For any λ ∈ ℝ, the positive definiteness implies that

On the other hand, because of bilinearity (that is, linearity in both variables) and symmetry, we have
(2.3)
which is a quadratic polynomial in λ. In general, we know that for any quadratic polynomial of the form ax2 + bx + c, the roots are given by the formula

Since

the polynomial defined by (2.3) must have at most one real root. Thus, the discriminant b2 − 4ac is non-positive. Plugging in the coefficients of the polynomial (2.3) into the discriminant formula, we obtain

which completes the proof.
The Cauchy-Schwarz inequality is probably one of the most useful tools in studying inner product spaces. One application we are going to see next is to show how inner products define norms.
Theorem 9. (The norm generated by the inner product)
Let V be an inner product space. Then, the function ∥⋅∥ : V → [0,∞ ) defined by

is a norm on V.
Proof. According to the definition of norms, we have to show that three properties hold: positive definiteness, homogeneity, and the triangle inequality. The first two follow easily from the properties of inner products. The triangle inequality follows from the Cauchy-Schwarz inequality:
∥x + y∥2 = ⟨x + y,x + y⟩
= ∥x∥2 + ∥y∥2 + 2⟨x,y⟩
≤ ∥x∥2 + ∥y∥2 + 2∥x∥∥y∥
= (∥x∥ + ∥y∥)2,
from which the triangle inequality follows.
Thus, inner product spaces are normed spaces as well. They have the proper algebraic and geometric structure that we need to represent, manipulate, and transform data.
Most importantly, Theorem 9 can be reversed! That is, given a norm ∥⋅∥, we can define a matching inner product.
Theorem 10. (The polarization identity)
Let V be an inner product space, and let ∥⋅∥be the norm induced by the inner product. Then,
(2.4)
In other words, one can generate an inner product from a norm, not just the other way around.
Proof. As the inner product is bilinear, we have

from which the polarization identity (2.4) follows.
2.2.2 Orthogonality
Section titled “2.2.2 Orthogonality”In vector spaces other than ℝ2, the concept of enclosed angles is not clear at all. For instance, in spaces where vectors are functions, there is no intuitive way to define the angles between two functions. However, as (2.1) suggests, in the special case ℝ2, these can be generalized.
Definition 10. (Orthogonality of vectors)
Let V be an inner product space, and let x,y ∈V . We say that x and y are orthogonal if

Orthogonality is denoted as x ⊥y.
To illustrate how inner products and orthogonality define geometry on vector spaces, let’s see how the classic Pythagorean theorem looks in this new form. Recall that the “original” version states that in right triangles, a2 + b2 = c2, where c is the length of the hypotenuse, while a and b are the lengths of the other two sides.
In inner product spaces, this generalizes in the following way.
Theorem 11. (The Pythagorean theorem)
Let V be an inner product space, and let x,y ∈V . Then, x and y are orthogonal if and only if
⟨x + y,x + y⟩ = ⟨x,x⟩ + ⟨y,y⟩. (2.5)
Proof. Given the definition of inner products and orthogonality, the proof is straightforward. Due to bilinearity, we have

Since x and y are orthogonal, we have ⟨x,y⟩ = 0. Thus, the equation simplifies to:

This completes the proof.
Why is this the Pythagorean theorem in another form? Because the norm and the inner product is related by ⟨x,x⟩ = ∥x∥2, (11) is equivalent to

which is exactly the famous “
”.
2.2.3 The geometric interpretation of inner products
Section titled “2.2.3 The geometric interpretation of inner products”Looking at the general definition, it is hard to get an insight into the inner product. However, by using the concept of orthogonality, we can visualize what ⟨x,y⟩ represents for any x and y.
Intuitively, any x can be decomposed into the sum of two vectors xo + xp, where xo is orthogonal to y and xp is parallel to it.

Figure 2.8: Decomposition of x into components parallel and orthogonal to y
Let’s make the intuition precise. How can we find xp and xo? Since xp has the same direction as y, it can be written in the form xp = cy for some scalar c ∈ℝ. Because xp and xo sum up to x, we also have xo = x −xp = x −cy.
Since xo is orthogonal to y, the constant c can be determined by solving the equation

By using the bilinearity of the inner product, we can express c from this equation. Thus, we have

So,
(2.6)
We call xp the orthogonal projection of x onto y. This is a common transformation, so we are going to introduce the notation
projy(x) =
y. (2.7)
From this, we can see that the scaling ratio between y and projy(x) can be described by inner products.
So far, we have seen that we can use inner products to define the orthogonality relation between two vectors. Can we use it to measure (and, in some cases, even define) the angle? The answer is yes! In the following, we are going to see how, arriving at the formula (2.1) already familiar from basic geometry.
To build our intuition, let’s select two arbitrary n-dimensional vectors x,y ∈ℝn. The inner product of the sum x + y can be calculated using the bilinearity property.

Figure 2.9: The sum of x and y
With this, we obtain
(2.8)
On the other hand, considering that x, y, and x + y form a triangle, we can use the law of cosines ( https://en.wikipedia.org/wiki/Law_of_cosines) to express ⟨x + y,x + y⟩ = ∥x + y∥2 in a different form.
Here, the law of cosines implies
∥x + y∥2 = ∥x∥2 + ∥y∥2 − 2∥x∥∥y∥
=−cos α . (2.9)
By combining (2.8) and (2.9), we get

That is, in ℝn, the angle enclosed by x and y can be extracted by
α = arccos
. (2.10)

Figure 2.10: The triangle formed by x, y, and x + y
What about vector spaces where the angle between vectors is not defined? We have seen instances of vector spaces (Section 1.1.1) where the elements are polynomials, functions, and other mathematical objects. There, (2.10) can be used to define the angle!
Let’s explore this idea further and see how to use inner products to measure similarity.
Given our geometric interpretation of inner products as orthogonal projections, let’s focus on the case when both x and y have unit norms. In this special case, the orthogonal projection equals

Thus, ⟨x,y⟩ precisely describes the signed magnitude of the orthogonal projection. (It can be negative when
and y have an opposite direction.)
With this in mind, we can see that the inner product is equal to the cosine of the angle enclosed by the two vectors. Let’s draw a picture to illustrate! (Recall that in right triangles, the cosine is the ratio of the length of the adjacent side and the hypotenuse. In this case, the adjacent side has a length of ⟨x,y⟩, while the hypotenuse is of unit length.)

Figure 2.11: The inner product of two unit vectors equals the cosine of their angle
In machine learning, this quantity is frequently used to measure the similarity of two vectors.
Because any vector x can be scaled to unit norm with the transformation x→x∕∥x∥, we define the cosine similarity by
cos(x,y) =
,
. (2.11)
If x and y represent the feature vectors of two data samples, cos(x,y) tells us how much the features move together. Note that because of the scaling, two samples with a high cosine similarity can be far from each other. So, this reveals nothing about their relative positions in the feature space.
2.2.4 Orthogonal and orthonormal bases
Section titled “2.2.4 Orthogonal and orthonormal bases”Through the lens of similarity, orthogonality means that one vector does not contain “information” about the other. We will make this notion more precise when learning about correlation, but there are clear implications regarding the structure of inner product spaces. Recall that during the introduction of basis vectors (Section 1.2), our motivation was to find a minimal set of vectors that can be used to express any other vector. With the introduction of orthogonality, we can go one step further.
Definition 11. (Orthogonal and orthonormal bases)
Let
be a vector space and S = {v1,…,vn } its basis. We say that
is an orthogonal basis if ⟨vi,vj⟩ = 0 whenever
.
Moreover,
is called orthonormal if

In other words,
is orthonormal if, in addition to being orthogonal, each vector has unit norm.
Orthogonal and orthonormal bases are extremely convenient to use. If a basis is orthogonal, we can easily obtain an orthonormal basis by simply scaling its vectors to unit norm. Thus, we’ll use orthonormal basis vectors most of the time.
Why do we love orthonormal bases so much? To see this, let {v1,…,vn} be an arbitrary basis and let
be an arbitrary vector. We know that

but how do we find the coefficients xi? There is a general method involving linear equations that we will see later in Chapter 6 , but if {vi}i=1n is orthonormal, the situation is much simpler.
This is made more precise in the following theorem.
Theorem 12.
Let
be a vector space and
be an orthonormal basis of
. Then, for any
,
x = ∑ i=1 n ⟨x,vi⟩ vi (2.12)
holds.
Proof. Since
form a basis, we can express
as

for some scalars
. By the linearity of the inner product, we obtain

Since
form an orthonormal basis, we have

Thus, the sum reduces to

This proves the result.
Thus, the coefficients can be calculated by taking the inner product. In other words, for orthonormal bases, xj depends only on the j-th basis vector.
As another consequence of the orthonormality, calculating the norm is also easier, as we can always express it in terms of the coefficients. To be more precise, we have
(2.13)
This is called Parseval’s identity. In other words, if x is given in terms of an orthonormal basis, its norm is easy to find. It is not a coincidence that this formula resembles the Euclidean norm so much! (Note that, here, ∥⋅∥ is a general norm.) In fact, the squared Euclidean norm

is just (2.13) using the standard basis.
2.2.5 The Gram-Schmidt orthogonalization process
Section titled “2.2.5 The Gram-Schmidt orthogonalization process”Orthogonal bases are awesome and all, but how do we find them?
There is a general method called the Gram-Schmidt orthogonalization process that solves this problem. The algorithm takes any set of basis vectors {v1,…,vn} and outputs an orthonormal basis {e1,…,en}
such that

that is, the subspaces generated by the first k vectors of both sets match.
How do we do that? The process is straightforward. Let’s focus on finding an orthogonal system first, which we can normalize later to achieve orthonormality. We are going to build our set {e1,…,en} iteratively. It is clear that

is a good choice. Now, our goal is to find e2 such that e2 ⊥e1 and, together, they span the same subspace as {v1,v2}. Remember when we talked about the geometric interpretation of orthogonality in Section 2.2.3? The orthogonal component of v2 with respect to e1 will be a good choice for e2. Thus, let

From the definition, it is clear that e2 ⊥e1, and it is also clear that {e1,e2} spans the same subspace as {v1,v2}.
In the next step, we perform the same process. We project v3 onto the subspace generated by e1 and e2, then define e3 as the difference of v3 and the projection. That is,

With this, we essentially remove the “contributions” of e1 and e2 toward v3, thus obtaining an e3 that is orthogonal to the previous ones.
In general, if we have e1,…,ek, the vector ek+1 can be found by

where
(2.14)
is the generalized orthogonal projection operator, projecting a vector to the subspace generated by {e1,…,ek}.
To check that ek+1 ⊥e1,…,ek , we have
(2.15)
due to the orthogonality of the ei-s and the linearity of the inner product. Since {e1,…,ek} spans the same subspace as {v1,…,vk} and e{k + 1} is a linear combination of vk+1 and e1,…,ek (where the coefficient of vk+1 is nonzero),

also follows. This can be repeated until we run out of vectors and find {e1,…,en}.
For the sake of further reference, mathematical correctness, and a tiny bit of perfectionism, let’s summarize all the above in a single theorem.
Theorem 13. (Gram-Schmidt orthogonalization process)
Let V be an inner product vector space and {v1,…,vn} ⊆ V be a set of linearly independent vectors. Then, there exists an orthonormal set {e1,…,en}⊆V such that

holds for any k = 1,…,n.
As a consequence, we can state that each finite inner product space has an orthonormal basis. We can even construct it explicitly via the Gram-Schmidt process.
Corollary 1. (Existence of orthonormal bases)
Let V be a finite-dimensional inner product space. Then, there exists an orthonormal basis in V .
Going one step further, we can view Theorem 13 and its proof as an algorithm.

Remark 3. (Linearly dependent inputs in the Gram-Schmidt process) What happens if we apply the Gram-Schmidt orthogonalization process to a set of linearly *dependent* vectors?
To get a grip on the problem, let’s consider a simple case of two vectors: {v1,v2 = cv1}, where c is an arbitrary scalar. e1 is chosen to be v1, and e2 is defined by

By expanding the projection term, we obtain

Since v1 = e1 and v2 = cv1, we get that
e2 = cv1 −
v1 = cv1 −cv1= 0
This result generalizes: when the Gram-Schmidt process encounters an input vector that is linearly dependent from the previous ones, a zero vector is produced in the output.
2.2.6 The orthogonal complement
Section titled “2.2.6 The orthogonal complement”Earlier, we saw that given a fixed vector y ∈V , we can decompose any x ∈V as x = xo + xp, where xo is orthogonal to y, while xp is parallel to it. (We used this to provide a geometric motivation for inner products in Section 2.2.3.)
This is an essential tool, and in this section, we will see that an analogue of this decomposition still holds true when y is replaced with an arbitrary subspace S ⊂V . To see this, let’s talk about the orthogonality of subspaces. (If you want to recall the definition of subspaces, check out Definition 5.)
Definition 12. (Orthogonal subspaces)
Let V be an arbitrary inner product space. We say that the subspaces S1,S2 ⊆V are orthogonal if, for every pair of vectors x ∈S1 and y ∈S2, we have ⟨x,y⟩ = 0. This is denoted as S1 ⊥S2.
For example, the x-axis and the y-axis are orthogonal subspaces in ℝ2. (Just as the xy plane and the z-axis in ℝ3.)
Similarly, we can talk about the orthogonality of a vector and a subspace: x is orthogonal to the subspace S, or x ⊥S in symbols, if x is orthogonal to all vectors of S.
One of the most straightforward and essential ways to construct orthogonal subspaces is to take the orthogonal complement.
Definition 13. (Orthogonal complement)
Let V be an arbitrary inner product space and let S ⊆ V be a subspace. The set defined by
(2.16)
is called the orthogonal complement of S.
S⊥ is not just any set; it is a subspace, as we are about to see.
Theorem 14.
Let
be an arbitrary inner product space and
one of its subspaces. Then
- is orthogonal to ,
- is a subspace of ,
- and .
Proof. According to the definition of subspaces, we only have to show that S⊥ is closed with respect to addition and scalar multiplication. As the inner product is bilinear, this is straightforward:

holds for any vectors x,y ∈S⊥, z ∈S, and scalars a,b.
To see that S ∩S⊥ = {0}, let’s take an arbitrary x ∈S ∩S⊥. By the definition of S⊥, we have ⟨x,x⟩ = 0. As the inner product is positive definite per definition, x must be the zero vector 0.
Recall the decomposition of any x ∈V into a parallel and an orthogonal component with respect to a fixed vector y? In terms of subspaces, we can restate this as

that is, V can be written as the direct sum of the vector space spanned by y, and its orthogonal complement. This is an extremely powerful result, as this allows us to decouple x from y. For instance, if we think of vectors as a collection of features (just like the sepal and petal width and length measurements in our favorite Iris dataset), y can represent a certain trait that we want to exclude from our analysis.
With the notion of orthogonal complements, we can make this mathematically precise. We can also be more general. In fact, the decomposition

holds for any subspace S! Let’s see the proof!
Theorem 15.
Let V be an arbitrary finite-dimensional inner product space and S ⊂V its subspace. Then,

holds.
Proof. Let v1,…,vk ∈S be a basis of S. As during the Gram-Schmidt process, we can define the generalized orthogonal projection (3.2.5), given by

Using this, we can decompose any x ∈V as
(2.17)
Since projv1,…,vk(x) is the linear combination of vi-s, it belongs to S. On the other hand, the bilinearity of the inner product gives that x − projv1,…,vk(x) ∈S⊥. Indeed, as we have

the vector x − projv1,…,vk(x) is orthogonal to each vj. Thus, since v1,…,vk is a basis of S, x is also orthogonal to S, hence x − proje1,…,vk(x) ∈S⊥.
The fact that every x ∈V can be decomposed as the sum of a vector from S and a vector from S⊥, as given by (3.2.6), means that V = S + S⊥, which is what we had to prove.
Bookmark
2.3 Summary
Section titled “2.3 Summary”In this chapter, we have learned that, besides the algebraic structure given by addition and scalar multiplication, vectors have a beautiful geometry that rises from the inner product. From the inner product, we have norms; from norms, we have metrics; and from metrics, we have geometry and topology.
Distance, similarity, angles, and orthogonality all arise from the simple concept of inner products. These are all extremely useful in both theory and practice. For instance, inner products give us a way to quantify the similarity of two vectors via the so-called cosine similarity, but they also provide a means to find optimal bases through the notion of orthogonality.
To summarize, we’ve learned what norms and distances are, the definition of the inner product, how inner products give angles and norms, and why all of these are useful in machine learning.
Besides the basic definitions and properties, we’ve encountered our very first algorithm: the Gram-Schmidt process, turning a set of vectors into an orthonormal basis. This is the best kind of basis.
In the next section, we’ll take all that theory and put it into practice, taking our first steps in computational linear algebra. Let’s go!
Bookmark
2.4 Problems
Section titled “2.4 Problems”Problem 1. Let V be a vector space and define the function d : V ×V → [0,∞) by

(a) Show that d is a metric (see Definition 8).
(b) Show that d cannot come from a norm.
Problem 2. Let Sn be the set of all ASCII strings of n character length and define the Hamming distance h(x,y) for any two x,y ∈Sn by the number of corresponding positions where x and y are different.
For instance,

Show that h satisfies the three defining properties of a metric. (Note that Sn is not a vector space so, technically, the Hamming distance is not a metric.)
Problem 3. Let ∥⋅∥ be a norm on the vector space ℝn, and define the mapping f : ℝn →ℝn,

Show that

is a norm on ℝn.
Problem 4. Let a1,…,an>0 be arbitrary positive numbers. Show that

is an inner product, where x = (x1,…,xn) and y = (y1,…,yn).
Problem 5. Let V be a finite-dimensional inner product space, let v1,…,vn ∈V be a basis in V , and define

Show that for any x,y ∈V ,

where x = ∑ i=1nxivi and y = ∑ i=1nyivi.
Problem 6. Let V be a finite-dimensional real inner product space.
(a) Let y ∈V be an arbitrary vector. Show that

is a linear function (that is, f(αu + βv) = αf(u) + βf(v) holds for all u,v ∈V and α,β ∈ℝ).
(b) Let f : V →ℝ an arbitrary linear function. Show that there exists a y ∈V such that

(Note that (b) is the reverse of (a), and a much more interesting result.)
Problem 7. Let V be a real inner product space and let ∥x∥ =
be the generated norm. Show that
(2.18)
This is called the parallelogram law, because if we think of x and y as the two sides determining a parallelogram, (3.4) relates the length of its sides to the length of its diagonals.
Problem 8. Let V be a real inner product space and let u,v ∈V . Show that if

holds for all x ∈V , then u = v.
Problem 9. Apply the Gram-Schmidt process to the input vectors
