Module 2, topic 15, week 8, Tuesday
Eigenvalues and eigenvectors; vector norms and their role in machine learning
The vectors that a matrix does not turn: eigenvalues, eigenvectors, diagonalization and symmetric matrices, with PCA and PageRank as uses. Then the L1, L2 and max norm and what they do in machine learning. Worked by hand.
Introduction: the last lesson on linear algebra
The third lesson of Module 2, and the last one on linear algebra. The last lesson showed that a matrix moves vectors: it stretches, turns and squeezes the plane. This lesson starts with a simple question that has deep consequences: are there vectors that a matrix does not turn? Such vectors are called eigenvectors, and the factors by which they are stretched are the eigenvalues. They show what a matrix really does, and they are behind Principal Component Analysis, Google's PageRank and many other methods of data science. The second subject of the lesson is the norm of a vector: the different ways to measure how large a vector is. The length of Topic 13 is one norm. Machine learning uses two more of them every day: to measure the error of a model, to decide which data points are neighbours and to keep the weights of a model small.
This guide first builds the idea of an eigenvector with a picture. Then it teaches how to find eigenvalues and eigenvectors by hand, which special cases can happen, the rules worth knowing, how a matrix is taken apart into its eigenvectors (diagonalization), and why symmetric matrices are especially friendly. After the uses of eigenvectors come the norms: the three norms of machine learning, their rules, the distances they give, and their role in machine learning. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.
You need the last two lessons: the dot product, the length of a vector and perpendicular vectors (Topic 13), and a matrix times a vector, the determinant, the inverse and systems of equations (Topic 14). From school you need quadratic equations. As in the whole module, the work is done by hand; Python appears only in one short part near the end.
What you will learn
- 1.What an eigenvector and an eigenvalue are, and what the eigenvalue says about the vector
- 2.How to check an eigenvector, and why an eigenvector is a direction, not one single vector
- 3.Finding eigenvalues with the characteristic equation, the trace and the determinant
- 4.Finding the eigenvectors that belong to an eigenvalue
- 5.The special cases: an eigenvalue 0, a repeated eigenvalue, no real eigenvalues, a 3 x 3 matrix
- 6.The rules for powers, inverses and multiples of a matrix, and the dominant eigenvalue
- 7.Diagonalization: a matrix written with its eigenvectors and eigenvalues, and powers of a matrix
- 8.Symmetric matrices: perpendicular eigenvectors, orthogonal matrices, positive definite matrices
- 9.Where eigenvectors are used: Principal Component Analysis, and the long-run state of a process (PageRank)
- 10.The L1, L2 and max norm of a vector, the rules of a norm, and the distances that come from norms
- 11.Norms in machine learning: the error of a model, regularization (Ridge and Lasso), neighbours, and a first look at norms of matrices and singular values
| Written | Meaning |
|---|---|
| the eigenvalue equation | |
| an eigenvalue (the Greek letter lambda) | |
| an eigenvector | |
| the trace of : the sum of the entries on its main diagonal | |
| the characteristic equation | |
| , | the matrix of the eigenvectors, and the diagonal matrix of the eigenvalues |
| an orthogonal matrix | |
| , , | the L1 norm, the L2 norm and the max norm of the vector |
| the Frobenius norm of the matrix | |
| the absolute value of the number |
1The idea of an eigenvector
1.1Most vectors are turned, a few are not
In the last lesson the matrix turned the unit square into a parallelogram. Look at what it does to three single vectors:
- becomes . The vector points in a new direction: it was turned.
- becomes , which is . The direction is the same; the vector is 3 times as long.
- becomes . It is not changed at all.
Most vectors behave like : the matrix turns them. But on two lines through the origin (the dotted lines of the picture) the vectors keep their direction. These are the special vectors of this matrix.
1.2The definition
A vector that is not the zero vector is an eigenvector of the square matrix if
for some number . The number is the eigenvalue that belongs to . (The word comes from German: "eigen" means "own". The eigenvectors are the matrix's own directions.)
For the matrix above:
- is an eigenvector with the eigenvalue 3, because .
- is an eigenvector with the eigenvalue 1, because .
- is not an eigenvector: is not a multiple of .
Read the equation once more. On the left a matrix multiplies the vector, on the right only a number does. For an eigenvector, the whole matrix acts like one single number.
1.3What the eigenvalue says
| Eigenvalue | What happens to the eigenvector |
|---|---|
| larger than 1 | it is stretched |
| it stays as it is | |
| between 0 and 1 | it shrinks |
| it is sent to the zero vector | |
| negative | it is flipped to the opposite direction (and stretched or shrunk) |
A flipped vector still lies on the same line through the origin. So the exact statement is: an eigenvector stays on its own line.
Choose a matrix and a vector v. Does A v stay on the dotted line of v?
v = (1, 0)
A v = (2 × 1 + 1 × 0, 1 × 1 + 2 × 0) = (2, 1)
A v is not on the line of v: the vector was turned. It is not an eigenvector.
Try all eight vectors for every matrix. How many lines of eigenvectors does each matrix have?
1.4Why this matters
A matrix is a complicated object: a 2 x 2 matrix has 4 numbers, a 1000 x 1000 matrix has a million. Along its eigenvectors it is simple: it only multiplies by a number. Knowing the eigenvectors and the eigenvalues means knowing the simplest description of the matrix. Many methods of data science are built on this:
| Where | What the eigenvectors and eigenvalues give |
|---|---|
| Principal Component Analysis (Topic 44) | the directions in which the data varies most, and how much it varies along each |
| Google's PageRank | the importance of every web page: an eigenvector with the eigenvalue 1 |
| A step that is repeated many times | the largest eigenvalue decides what happens in the long run |
| Clustering with graphs (spectral clustering) | groups of points, found from the eigenvectors of a matrix of similarities |
| Training neural networks | how steep and how flat the loss is in different directions |
1.5Only square matrices
and must have the same number of components, so must be square. All matrices in the first nine parts of this lesson are square.
Try it yourself
Show a hint
Show the solution
Answer: and . The vector is an eigenvector with the eigenvalue 3.
is 3 times : the vector kept its direction. is not a multiple of . Notice that lies on the same line as and has the same eigenvalue.
2Checking an eigenvector, and the first facts
2.1How to check
To test whether a vector is an eigenvector, multiply and compare: must be a multiple of .
is an eigenvector with , and is an eigenvector with . The vector is not an eigenvector: but .
Every component of must be the same multiple of the matching component of . In the second test the first component was multiplied by 6 and the second by 4, so the vector was turned.
2.2An eigenvector is a direction
If is an eigenvector, then every multiple (with not 0) is an eigenvector too, with the same eigenvalue:
For the matrix the vectors , , and are all eigenvectors with the eigenvalue 3. So "the eigenvector" is really a whole line through the origin, and we are free to choose one vector on it. Usually we take the one with small whole numbers, or the one with the length 1.
2.3The zero vector does not count, the eigenvalue zero does
is true for every matrix and every number , so it tells nothing. That is why the zero vector is excluded in the definition. But the number 0 is a perfectly good eigenvalue: it means for a vector that is not zero (Part 5).
2.4Matrices whose eigenvalues can be read off
For a diagonal matrix the eigenvalues are the entries on the diagonal, and the eigenvectors are the standard unit vectors:
For a triangular matrix the eigenvalues are also the entries on the diagonal (Part 3 shows why). Its eigenvectors have to be calculated.
Try it yourself
Show a hint
Show the solution
Answer: is an eigenvector with the eigenvalue 4, is an eigenvector with the eigenvalue , and is not an eigenvector.
is not a multiple of . The eigenvalue flips to the opposite vector.
3Finding the eigenvalues
3.1From the definition to an equation
Bring everything in to one side:
The identity matrix is needed here: is the same as , and a number cannot be subtracted from a matrix. is the matrix with subtracted from every entry of the main diagonal.
is a system of equations with zeros on the right side. The zero vector always solves it, but we are looking for another solution. From the last lesson: a system has exactly one solution when the determinant of its matrix is not 0. We want more than one solution, so the determinant must be 0:
This is the characteristic equation of . Its solutions are the eigenvalues.
3.2The characteristic equation of a 2 x 2 matrix
The number , the sum of the main diagonal, is called the trace of , written . The number is the determinant. So for every 2 x 2 matrix:
3.3Example 1
These are the two eigenvalues that Part 1 found by trying vectors.
3.4Solving the quadratic equation
Two ways, both from school:
- Factoring. Look for two numbers whose sum is the trace and whose product is the determinant. For the sum 4 and the product 3 they are 1 and 3.
- The formula.
3.5Example 2
3.6Two checks that cost nothing
For every square matrix:
For : and . For : and . Use these two checks after every calculation of eigenvalues.
The second check has an important consequence. The determinant is 0 exactly when one of the eigenvalues is 0. So one more statement joins the list of the last lesson (rank smaller than , determinant 0, no inverse): a matrix is singular exactly when 0 is one of its eigenvalues.
3.7Triangular matrices
The product is 0 for and for : the entries of the diagonal. The zero below the diagonal removes the second part of the determinant. The same happens for triangular matrices of every size.
3.8Larger matrices
For an matrix the characteristic equation has the degree , so there are eigenvalues (some may be repeated, and some may not be real numbers, see Part 5). By hand this is practical for 2 x 2 matrices and for 3 x 3 matrices with some zeros. For larger matrices a computer finds the eigenvalues.
Move the sliders: the characteristic equation and the eigenvalues of your matrix.
tr(A) = 2 + 2 = 4
det(A) = 2 × 2 − 1 × 1 = 3
λ² − 4λ + 3 = 0
tr(A)² − 4 det(A) = 16 − 12 = 4
It is positive: two different eigenvalues, λ = 3 and λ = 1.
Check: the sum 3 + 1 is the trace, and the product is the determinant.
Try it yourself
Show a hint
Show the solution
Answer: , , and the eigenvalues are 5 and .
The checks: and .
4Finding the eigenvectors
4.1One small system for every eigenvalue
Put an eigenvalue into and solve the system. Because is singular, its two rows are multiples of each other: both equations say the same thing. One equation is left for two unknowns, so one unknown can be chosen freely. That is exactly the freedom of Part 2: an eigenvector is a whole line.
4.2Example 1
has the eigenvalues 3 and 1.
The two equations are the same equation. For the second eigenvalue:
4.3Example 2
has the eigenvalues 5 and 2.
The check, for the second one: .
4.4The recipe
- Calculate the trace and the determinant, and write the characteristic equation .
- Solve it. Check: the sum of the eigenvalues is the trace, their product is the determinant.
- For every eigenvalue write the matrix , take one of its rows as an equation and choose a simple solution.
- Check every eigenvector: must be .
A warning for step 3: the two rows of must be multiples of each other. If they are not, the only solution is the zero vector, and that means the eigenvalue is wrong. Go back to step 2.
All the eigenvectors of one eigenvalue, together with the zero vector, form the eigenspace of that eigenvalue. In the two examples every eigenspace is a line through the origin.
Try it yourself
Show a hint
Show the solution
Answer: for the eigenvalue 5, and for the eigenvalue (or any multiples of them).
The checks: and .
5Special cases
5.1An eigenvalue 0
The checks: and .
The determinant is 0, so the matrix squeezes the plane flat onto a line (Topic 14). Now both directions have a name: the line of is where everything lands, and is the direction that is squeezed to nothing.
5.2A repeated eigenvalue
The characteristic equation can have one solution twice. Then two things can happen.
Every vector is an eigenvector: this matrix doubles all vectors.
Only the direction . The first matrix has eigenvectors in every direction, the second one only on a single line. A repeated eigenvalue needs this extra look.
5.3No real eigenvalues
No real number solves this equation. is the rotation by 90 degrees of the last lesson. A rotation turns every vector, so no vector keeps its direction, and the calculation agrees with the picture. (With complex numbers the equation has solutions. They are not needed in this course: the symmetric matrices of machine learning always have real eigenvalues, see Part 8.)
The number under the root of the formula decides which case it is:
| Eigenvalues | |
|---|---|
| positive | two different real eigenvalues |
| zero | one eigenvalue, repeated |
| negative | no real eigenvalues |
5.4A 3 x 3 matrix
The determinant of along the first row (Topic 14). The two zeros leave only one part:
The eigenvalues are 2, 1 and 11. The checks: their sum is 14, and the trace is . Their product is 22, and the determinant is .
The eigenvector of :
The third row, , says the same as the second. The check: .
In the same way gives and gives .
Try it yourself
Show a hint
Show the solution
Answer: has two different real eigenvalues, has one repeated eigenvalue, and has no real eigenvalues.
has the eigenvalues 3 and . has the eigenvalue 3 twice. is a rotation by 90 degrees with a stretch by 2, so it turns every vector.
6Rules worth knowing
6.1What happens to the eigenvalues when the matrix changes
Let be an eigenvector of with the eigenvalue . Then is also an eigenvector of the following matrices:
| Matrix | Its eigenvalue | Why |
|---|---|---|
| the same step, times | ||
| (if it exists) | multiply by : | |
One more rule: has the same eigenvalues as , because a matrix and its transpose have the same determinant. Its eigenvectors are in general different.
Example. has the eigenvector with .
6.2Eigenvectors of different eigenvalues
Eigenvectors that belong to different eigenvalues are linearly independent: they never lie on the same line. A 2 x 2 matrix with two different eigenvalues therefore has two independent eigenvectors, and every vector of the plane is a linear combination of them.
6.3Applying a matrix many times
This makes a hard calculation easy. Take with the eigenvectors (eigenvalue 3) and (eigenvalue 1), and the vector :
The matrix multiplies each part by its eigenvalue:
The first result can be checked directly: . The second would need ten matrix multiplications without eigenvectors.
Look at the result: after ten steps the vector points almost exactly in the direction . The part with the largest eigenvalue grows fastest and takes over. The eigenvalue with the largest size is called the dominant eigenvalue, and its eigenvector is where repeated multiplication leads. PageRank (Part 9) is built on this fact.
Try it yourself
Show a hint
Show the solution
Answer: : 27 and . : and . : 7 and 2.
The eigenvectors are the same for all four matrices. A third power keeps the sign of a negative eigenvalue.
7Diagonalization
7.1Everything in two matrices
Take again. Put its eigenvectors into the columns of a matrix , and its eigenvalues, in the same order, on the diagonal of a matrix :
Multiply . Every column of the product is times a column of (Topic 14), and that is an eigenvalue times the column:
So . Multiply both sides from the right by :
This is the diagonalization of : the matrix written with its eigenvectors and eigenvalues.
The check, with the inverse of :
7.2What it means
Read from right to left:
- writes as a combination of the eigenvectors.
- multiplies each part by its eigenvalue.
- puts the parts together again.
It is the calculation of Part 6 ("applying a matrix many times") written with matrices. Seen from its eigenvectors, the matrix is diagonal: a pure stretch along two directions.
7.3Powers of a matrix
A power of a diagonal matrix is easy: only the diagonal entries are raised to the power.
7.4When it is not possible
must have an inverse, so the matrix needs as many independent eigenvectors as it has rows.
- A 2 x 2 matrix with two different eigenvalues can always be diagonalized.
- of Part 5 has only one line of eigenvectors: it cannot be diagonalized.
- A symmetric matrix can always be diagonalized, and in an especially good way. That is the next part.
Try it yourself
Show a hint
Show the solution
Answer: and .
The check without eigenvectors: , the same matrix.
8Symmetric matrices
8.1Why they get a part of their own
The matrices of data science are very often symmetric. A covariance matrix, which says how the features of a data set vary together, is symmetric. For every matrix the product is symmetric (a practice problem of the last lesson). Matrices of distances and of similarities are symmetric. And symmetric matrices have the best eigenvectors there are.
8.2Three facts
For every symmetric matrix:
- All eigenvalues are real numbers. The case "no real eigenvalues" cannot happen.
- Eigenvectors of different eigenvalues are perpendicular.
- It can always be diagonalized, with eigenvectors that are perpendicular to each other and have the length 1.
Together these facts are called the spectral theorem.
Fact 2 with the examples of this lesson: is symmetric, and its eigenvectors have the dot product . is not symmetric, and its eigenvectors are not perpendicular: .
symmetric: [2 1; 1 2]
(1, 1) · (−1, 1) = 0: perpendicular
not symmetric: [4 1; 2 3]
(1, 1) · (−1, 2) = 1: not perpendicular
Why fact 2 is true: for a symmetric matrix . With two eigenvectors and and their eigenvalues and this says . When the two eigenvalues are different, that is only possible with .
8.3Orthonormal vectors and orthogonal matrices
Vectors are called orthonormal when each of them has the length 1 and they are perpendicular to each other. The eigenvectors of become orthonormal when each is divided by its length (Topic 13):
A square matrix whose columns are orthonormal is called an orthogonal matrix. It has a remarkable property:
Its inverse is simply its transpose. The reason: the entries of are the dot products of the columns of , and those are 1 (a column with itself) or 0 (two different columns).
An orthogonal matrix changes no length and no angle: it only turns or mirrors the plane. The rotation of the last lesson is an orthogonal matrix.
8.4The diagonalization of a symmetric matrix
With orthonormal eigenvectors in the columns of , the formula becomes
No inverse has to be calculated. For with :
The picture behind the formula: turns the plane, stretches it along the axes, turns it back. A symmetric matrix stretches the plane along perpendicular directions, and nothing else.
8.5Positive definite matrices: a first look
A symmetric matrix whose eigenvalues are all positive is called positive definite. If they are all positive or zero, it is called positive semidefinite.
There is a second way to say the same: is positive definite when the number is positive for every vector that is not zero. For and :
Both eigenvalues of (3 and 1) are positive, so the result is positive for every . The matrix has the eigenvalues 3 and , so it is not positive definite: for the result is .
You will meet the word in machine learning. Covariance matrices and all matrices of the form are positive semidefinite. And a loss function whose matrix of second derivatives is positive definite has the shape of a bowl, with exactly one lowest point: the best case for training a model.
Try it yourself
Show a hint
Show the solution
Answer: The eigenvalues are 10 and 5, with the eigenvectors and . Both eigenvalues are positive, so the matrix is positive definite.
The dot product is : perpendicular, as a symmetric matrix promises. Both eigenvalues are positive.
9Where eigenvectors are used
9.1Principal Component Analysis
A data set has two features. Its covariance matrix is
On the diagonal are the variances of the two features (how much each of them varies), outside the diagonal is their covariance (how they vary together). How these numbers are calculated from data is a subject of the statistics lessons; here the matrix is given.
The eigenvalues and eigenvectors of :
What they say about the data:
- The data varies most along the direction . The variance in this direction is the eigenvalue, 6.
- Perpendicular to it, along , the variance is only 1. (The two directions are perpendicular because is symmetric.)
- The total variance is , the trace, and also . The first direction carries , about 86 percent, of it.
Now the useful step. Replace every data point by one number: its scalar projection onto the unit vector (Topic 13). Two numbers for every point become one, and 86 percent of the variance is kept. With 100 features the same idea keeps, say, the 10 best directions. This is Principal Component Analysis (Topic 44): the eigenvectors of the covariance matrix are the principal components, and the eigenvalues say how important each one is.
9.2The long run: from two shops to PageRank
Two shops share the customers of a town. Every month 10 percent of the customers of shop 1 change to shop 2, and 20 percent of the customers of shop 2 change to shop 1. If holds the share of shop 1 and the share of shop 2, then next month's shares are :
(Each column adds up to 1: every customer goes somewhere.) Starting with equal shares:
| Month | Share of shop 1 | Share of shop 2 |
|---|---|---|
| 0 | 0.5 | 0.5 |
| 1 | 0.55 | 0.45 |
| 2 | 0.585 | 0.415 |
| 3 | 0.6095 | 0.3905 |
| 5 | 0.6387 | 0.3613 |
| 10 | 0.6620 | 0.3380 |
| 20 | 0.6665 | 0.3335 |
The shares settle down. Where? At a vector that the matrix does not change any more: . That is an eigenvector with the eigenvalue 1.
Shares must add up to 1, so divide by : the long-run shares are , about . The table was on its way there. The other eigenvalue, 0.7, belongs to the part of the starting vector that fades away: it is multiplied by 0.7 every month. Whatever the starting shares are, the result is the same.
Choose the starting share of shop 1, then let the months pass.
Share of shop 1 at the start
Months that have passed
The vertical line marks two thirds, the long-run share of shop 1.
Month 0: shares (0.5000, 0.5000)
Distance to the long-run share: 0.1667. It is multiplied by 0.7, the second eigenvalue, every month.
Change the start: the shares always settle at (0.6667, 0.3333), the eigenvector of the eigenvalue 1.
Such a process, with fixed chances of moving from one state to another, is called a Markov chain. Google's PageRank is the same calculation with web pages instead of shops: a surfer clicks from page to page, and the long-run share of time spent on each page is its rank. It is the eigenvector with the eigenvalue 1 of a matrix with billions of rows, found exactly as in the table: by multiplying again and again.
9.3More places
| Where | The eigen idea |
|---|---|
| Vibrations of a bridge or a guitar string | the eigenvalues are the natural frequencies |
| Stability of a system | all eigenvalues smaller than 1 in size: repeated steps die out |
| Face recognition with "eigenfaces" | the eigenvectors of the covariance matrix of many face images |
| Compressing an image | keeping only the directions with the largest values (see the singular values in Part 11) |
Try it yourself
Show a hint
Show the solution
Answer: The long-run shares are , about .
The check: . The second eigenvalue is , so the shares settle very quickly.
10Vector norms
10.1More than one way to measure size
"How large is this vector?" "How far apart are these two data points?" "How big is the error of the model?" Each of these questions wants one number as an answer. A norm gives it: a norm turns a vector into one number that is never negative, the size of the vector. It is written with double bars: .
You know one norm already: the length of Topic 13. There are others, and each of them answers the question "how large?" in a slightly different way.
10.2The three norms of machine learning
Take .
The L2 norm (Euclidean norm). Square the components, add, take the root. It is the length of Topic 13, where it was written .
The L1 norm (Manhattan norm). Add the absolute values of the components.
The max norm (L∞ norm, "L infinity"). Take the largest absolute value.
| Vector | L1 norm | L2 norm | Max norm |
|---|---|---|---|
| 7 | 5 | 4 | |
| 5 | 3 | 2 | |
| 4 | 2 | 1 | |
| 6 | 6 | 6 |
Two things to notice. For every vector
and the three norms are equal when the vector has only one component that is not 0.
Move the vector. Each shape shows all vectors with the same norm as yours.
- L1 norm (diamond)
- L2 norm (circle)
- max norm (square)
L1 norm: |3| + |−4| = 7
L2 norm: √(9 + 16) = √25 = 5
Max norm: the larger of 3 and 4 = 4
4 ≤ 5 ≤ 7: the max norm is never larger than the L2 norm, and the L2 norm never larger than the L1 norm. On an axis all three are equal.
10.3The family behind them
The three norms belong to one family, the Lp norms:
for The values and give the first two norms. The larger is, the more the largest component counts, and for very large only the largest one is left: that is why the max norm is called L∞. For :
| 1 | 2 | 3 | 4 | 10 | 100 | |
|---|---|---|---|---|---|---|
| 7 | 5 | 4.498 | 4.285 | 4.022 | 4.000 |
Two relatives that you will meet:
- The squared L2 norm, . It has no root, so it is easier to calculate with, and most formulas of machine learning use it.
- The "L0 norm", the number of components that are not 0. It is not a real norm (it breaks rule 2 below), but the name is used when people speak about sparse vectors: vectors with many zeros.
10.4The three rules of every norm
A way of measuring size is called a norm only if it obeys three rules:
| Rule | In symbols | In words |
|---|---|---|
| 1 | , and only for the zero vector | sizes are not negative |
| 2 | a vector 3 times as long has 3 times the size | |
| 3 | the triangle inequality: a detour is never shorter |
A check with the L1 norm, for and :
10.5What "size 1" looks like
Draw all the vectors whose norm is exactly 1. For the L2 norm they form a circle. For the L1 norm they form a square standing on its corner (a diamond), and for the max norm a square.
- L1 norm (diamond)
- L2 norm (circle)
- max norm (square)
Some points to test the picture: lies on all three shapes. lies on the circle (). lies on the diamond (). lies on the square (the largest component is 1). The corners of the diamond lie on the axes. Remember this for Part 11: it is the reason why the L1 norm produces zeros.
10.6Distances
Every norm gives a distance: the distance between and is the norm of their difference, . For and the difference is :
| Distance | Norm | Value |
|---|---|---|
| Euclidean distance | L2 | |
| Manhattan distance | L1 | |
| Chebyshev distance | max |
The Euclidean distance is the straight line. The Manhattan distance is the way of a taxi in a city with a grid of streets: 3 blocks along and 4 blocks up. The Chebyshev distance is the number of moves a king needs on a chess board, where one move can change both coordinates at once.
10.7Normalizing a vector
Dividing a vector by its norm gives a vector with the norm 1.
- Dividing by the L2 norm gives the unit vector of Topic 13: the direction of the vector without its length. becomes .
- Dividing by the L1 norm gives a vector whose absolute values add up to 1. For a vector of counts these are shares: has the L1 norm 10 and becomes . This is how counts are turned into proportions and probabilities.
Try it yourself
Show a hint
Show the solution
Answer: The norms are 14, 10 and 8. The Manhattan distance is 7.
The order is as it must be: max norm, L2 norm, L1 norm.
11Norms in machine learning
11.1Measuring the error of a model
A model predicts four values. The true values are , the predictions are . The vector of the errors is
How good is the model? Every usual answer is a norm of :
| Measure | Formula | Value |
|---|---|---|
| Mean absolute error (MAE) | ||
| Mean squared error (MSE) | ||
| Root mean squared error (RMSE) | ||
| Largest error |
The two main measures treat a large error differently. In the MAE the error 4 is 4 of the 7 points, 57 percent. In the MSE it is 16 of the 21 points, 76 percent: squaring makes large errors count much more. So a model that is trained with the squared L2 norm works hard to avoid single large errors and is sensitive to outliers, and a model trained with the L1 norm is more robust against them. Linear regression (Topic 22) minimizes the MSE.
11.2Keeping the weights small: regularization
A model with very large weights reacts strongly to small changes of its input, and often it has learned the noise of the training data (overfitting, Topic 23). The cure is to make large weights expensive. A penalty is added to the loss:
is a number that we choose: the larger it is, the stronger the penalty. (Many books write for this number. This lesson uses , because is an eigenvalue here. scikit-learn calls it alpha too.)
For the weights and :
The two penalties push the weights in different ways:
- In the weight costs 9, and the weight costs only 0.25. Large weights are punished hard, small ones hardly at all. Ridge makes all weights smaller, the large ones most, but it rarely makes a weight exactly 0.
- In every bit of a weight costs the same, whether the weight is large or small. So it pays to push small weights all the way to 0. Lasso produces weights that are exactly 0: it switches features off, and the model becomes sparse. The corners of the diamond in the picture of Part 10, which lie on the axes, are the geometric reason.
Topic 26 teaches both methods. Their whole difference is the choice of the norm.
11.3Which points are neighbours?
k-Nearest Neighbors (Topics 32 and 33) and k-means (Topic 43) work with distances, and the answer can depend on the norm. Which of and is nearer to the point ?
| Distance | to | to | Nearer |
|---|---|---|---|
| Euclidean (L2) | |||
| Manhattan (L1) | |||
| Chebyshev (max) |
The usual choice is the Euclidean distance. In every norm a feature with large numbers (a salary next to an age) decides the distance almost alone. That is why features are scaled before such methods are used (Topic 24).
11.4Unit vectors everywhere
The cosine similarity of Topic 13 divides a dot product by two L2 norms. In practice the vectors are normalized once, to the L2 norm 1, and stored. Then the cosine similarity is simply the dot product. Word embeddings and the vectors of a vector database are kept in this form.
11.5Gradient clipping
A neural network learns by taking steps along a vector called the gradient (Topic 53). Sometimes this vector becomes far too long, and one wild step destroys what was learned. The remedy: if the norm is larger than a limit, shrink the vector to the limit and keep its direction.
The new vector has the same direction and the norm 5.
11.6Norms of matrices
A matrix has a size too. The most used one is the Frobenius norm: treat the matrix as one long vector and take the L2 norm.
It measures how different two matrices are, for example an image and its compressed version: .
11.7A look ahead: singular values
Here the two halves of the lesson meet. For any matrix , even one that is not square, the matrix is symmetric and positive semidefinite, so its eigenvalues are real and not negative. Their square roots are called the singular values of .
The singular values are and . They are the stretch factors of the matrix: the largest one, 6.71, is the most that can stretch a vector of the length 1. They also give the Frobenius norm: .
Writing a matrix with its singular values is called the singular value decomposition (SVD). It does for every matrix what diagonalization does for symmetric ones. Libraries use it to calculate a PCA, and it is behind recommendation systems and the compression of images. In this course you will use it as a ready tool; what you need to remember is where it comes from: the eigenvalues of .
Try it yourself
Show a hint
Show the solution
Answer: MAE 2, MSE 7.5, largest error 5. The Lasso penalty is 3 and the Ridge penalty is 7.
The single error 5 is 25 of the 30 points of the squared L2 norm: one bad prediction decides the MSE.
12Common mistakes
Accepting the zero vector. is always true and says nothing. An eigenvector is never the zero vector. If solving gives only , the eigenvalue is wrong.
Subtracting from every entry. changes only the main diagonal. The other entries stay as they are.
The wrong signs in the characteristic equation. It is : minus the trace, plus the determinant.
Thinking that there is one right eigenvector. , and are the same answer. Compare directions, not numbers.
Mixing up "eigenvalue 0" and "no eigenvalue". An eigenvalue 0 is a real eigenvalue with real eigenvectors: the matrix is singular. "No real eigenvalues" means that the characteristic equation has no real solution.
Using row operations before looking for eigenvalues. Elimination keeps the solutions of a system, but it changes the eigenvalues. Work with the matrix as it is.
Adding eigenvalues of two matrices. The eigenvalues of are in general not the sums, and those of not the products, of the eigenvalues of and . The rules of Part 6 are about one matrix.
A different order in and . The first column of must belong to the first entry of .
Writing . The diagonalization is , with the inverse at the end.
Expecting perpendicular eigenvectors from every matrix. Only symmetric matrices promise them.
Forgetting the absolute values in the L1 norm. is 7, not . A norm is never negative.
Taking the largest component for the max norm. It is the largest absolute value: .
Mixing up and . The MSE and the Ridge penalty use the square, without the root. The RMSE and a distance use the root.
Adding norms. is in general not . Only "smaller or equal" is true.
| Mistake | What is right |
|---|---|
| the zero vector as an eigenvector | an eigenvector is not the zero vector |
| in every entry | : only the main diagonal |
| " is right, is wrong" | both are the same eigenvector |
| eigenvalues after row operations | row operations change the eigenvalues |
13Solved problems (from the easiest to the hardest)
Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.
Level 1: The basics
Problem 1: Is it an eigenvector?
Which of the vectors , and are eigenvectors of ? Give the eigenvalue where there is one.
Idea. Multiply and compare: must be a multiple of (Part 2).
is an eigenvector with and is an eigenvector with . is not an eigenvector: is not a multiple of .
Why: the matrix is triangular, so its eigenvalues are the diagonal entries 3 and 2 (Part 3): the test found both. For the two components were multiplied by different numbers, 4 and 2, and that is a turn.
Problem 2: Eigenvalues that can be read off
Give the eigenvalues of and of without a long calculation. Which eigenvectors of do you know at once?
Idea. For a diagonal and for a triangular matrix the eigenvalues are the entries of the diagonal (Parts 2 and 3).
The eigenvectors of are and . The check for : and .
Why: the entry 5 above the diagonal has no influence on the eigenvalues of . It does change the eigenvectors: is not an eigenvector of , because .
Problem 3: The characteristic equation
Find the eigenvalues of .
Idea. For a 2 x 2 matrix the characteristic equation is (Part 3).
The check: and .
Why: the determinant is negative, so the two eigenvalues must have different signs: their product is the determinant. One direction is stretched, the other one is flipped.
Problem 4: The eigenvectors
Find the eigenvectors of the matrix of Problem 3.
Idea. For every eigenvalue solve (Part 4).
The checks:
Why: for the choice avoids a fraction; would be the same eigenvector. Subtracting the eigenvalue means adding 1 to the diagonal.
Problem 5: The two checks
A student says that has the eigenvalues 3 and 5. Show without solving an equation that this is wrong. Then find the right eigenvalues.
Idea. The sum of the eigenvalues is the trace, their product is the determinant (Part 3).
The check: and .
Why: the two checks need ten seconds and catch almost every slip. One failed check is enough to know that an answer is wrong.
Problem 6: Three norms
Find the L1 norm, the L2 norm and the max norm of .
Idea. Add the absolute values, or square, add and take the root, or take the largest absolute value (Part 10).
The check: .
Why: the minus sign of disappears in all three norms: in an absolute value, in a square, in an absolute value. The order max norm, L2 norm, L1 norm from small to large is a quick test of the three results.
Problem 7: A distance in three norms
Find the Manhattan, the Euclidean and the Chebyshev distance between and .
Idea. A distance is the norm of the difference (Part 10).
Why: one subtraction, then three norms. The order of the subtraction does not matter for a distance: gives the same three numbers.
Problem 8: Normalizing in two ways
Normalize with the L2 norm, and with the L1 norm.
Idea. Divide the vector by its norm (Part 10).
The checks: and .
Why: the first result is a direction: a unit vector. The second is a list of shares: if counts how often three words appear in a text, the result says that the first word makes up 60 percent.
Level 2: Eigenvalues, eigenvectors and norms at work
Problem 9: A symmetric matrix
Find the eigenvalues and eigenvectors of , and check that the eigenvectors are perpendicular.
Idea. The recipe of Part 4. For a symmetric matrix the eigenvectors of different eigenvalues must be perpendicular (Part 8).
The dot product is : perpendicular.
Why: with the trace 0 the equation has no middle term and is solved by a square root. The eigenvalues 5 and say what the matrix does: it stretches the direction by 5 and flips the perpendicular direction.
Problem 10: A singular matrix
Find the eigenvalues and eigenvectors of . What does the matrix do to the plane?
Idea. A determinant 0 gives an eigenvalue 0 (Parts 3 and 5).
The checks:
Why: the matrix squeezes the plane flat onto the line of and stretches by 5 along that line. Everything in the direction is sent to zero. A determinant 0 always shows up as an eigenvalue 0.
Problem 11: A repeated eigenvalue
Find the eigenvalues and all directions of eigenvectors of . Compare with the matrix .
Idea. When the characteristic equation has one solution twice, look at to see how many directions there are (Part 5).
Only one direction. For the second matrix:
Every vector solves this system: all directions.
Why: the two matrices have the same eigenvalue, the same trace and the same determinant, but not the same eigenvectors. has too few eigenvectors and cannot be diagonalized (Part 7).
Problem 12: No real eigenvalues
Show that has no real eigenvalues. What does that say about the matrix?
Idea. The sign of decides (Part 5).
The number is negative: the equation has no real solution.
Why: no vector keeps its direction. In the last lesson this matrix turned every vector to the left by half a right angle and made it longer: a rotation with a stretch, and a rotation leaves no direction where it was.
Problem 13: Four multiplications at once
has the eigenvector with the eigenvalue 5 and the eigenvector with the eigenvalue 2. Find for without calculating .
Idea. Write as a combination of the eigenvectors. The matrix multiplies each part by its eigenvalue, four times (Part 6).
The check of the first line: and .
Why: the numbers 2 and 1 of the combination come from two small equations, and . After four steps the part of the dominant eigenvalue 5 is already about 78 times as large as the other part (1250 against 16).
Problem 14: Eigenvalues of related matrices
A matrix has the eigenvalues 2 and 5. Give the eigenvalues of , , , and , and the determinant of .
Idea. The rules of Part 6: the eigenvectors stay, the eigenvalues change in a simple way.
Why: none of these answers needs the matrix itself. The determinant of is the product of its eigenvalues. It is also , as the rule of the last lesson says.
Problem 15: A diagonalization
Diagonalize (Problems 3 and 4): give and , and check that .
Idea. The eigenvectors go into the columns of , the eigenvalues in the same order onto the diagonal of (Part 7).
Why: checking avoids the inverse of . The first column of is 4 times the first eigenvector and the second column is times the second eigenvector: exactly what does to them.
Problem 16: The error of a model
The true values are and a model predicts . Find the MAE, the MSE, the RMSE and the largest error.
Idea. All four are norms of the error vector (Part 11).
Why: one bad prediction, the error 6, is 60 percent of the L1 norm but 86 percent of the squared L2 norm (36 of 42). The RMSE has the same unit as the data, so it can be compared with the MAE: here it is clearly larger, a sign that the errors are uneven.
Problem 17: The nearest neighbour
Which of the points and is nearer to : with the Manhattan distance, with the Euclidean distance and with the Chebyshev distance?
Idea. Three norms of the two differences (Parts 10 and 11).
| Difference | L1 | L2 | max |
|---|---|---|---|
| 7 | 4 | ||
| 6 | 6 | 6 |
With the Manhattan distance is nearer (). With the Euclidean distance is nearer (), and with the Chebyshev distance too ().
Why: differs from in one coordinate only, so all its norms are equal. differs in both, and the L1 norm adds the two differences in full. A k-Nearest Neighbors model with the Manhattan distance and one with the Euclidean distance would choose different neighbours here.
Problem 18: Two penalties, two opinions
Two models fit the data equally well. Their weights are and . Which model does a Lasso penalty prefer, and which a Ridge penalty?
Idea. Lasso uses the L1 norm of the weights, Ridge the squared L2 norm. The smaller penalty wins (Part 11).
| Penalty | Smaller | ||
|---|---|---|---|
| L1 norm (Lasso) | 4 | 6 | |
| squared L2 norm (Ridge) | 16 | 12 |
Lasso prefers , Ridge prefers .
Why: the L1 norm likes one large weight and many zeros: a sparse model. The squared L2 norm dislikes a single large weight (16) and prefers the weight to be spread over several features (). The same two models, the opposite verdict.
Level 3: Putting ideas together
Problem 19: A 3 x 3 matrix
Find the eigenvalues of , and an eigenvector of the middle eigenvalue.
Idea. The determinant of along the first row (Part 5), then a system of three equations for the eigenvector.
The eigenvalues are 2, 3 and 5. The check: . For the middle one:
The check: .
Why: the eigenvalues are the diagonal entries although the matrix is not triangular: the zeros made the other parts of the determinant vanish. In one row is zero, as a singular matrix must show somewhere; the two other rows give the eigenvector.
Problem 20: Orthonormal eigenvectors
The symmetric matrix has the eigenvalues 6 and . Find orthonormal eigenvectors, write the matrices and , and check that .
Idea. Find the eigenvectors, divide each by its length, and put them into the columns of (Part 8).
The dot product is .
Why: the two factors of and of multiply to , so no root is left in the calculation. Because is orthogonal, its transpose did the work of the inverse.
Problem 21: A power of a matrix
Find for with the diagonalization of Problem 15.
Idea. , and the power of a diagonal matrix is the power of its entries (Part 7).
Why: three matrix multiplications are replaced by the fourth powers of two numbers. A check with little effort: , and times gives the same matrix.
Problem 22: Principal components
The covariance matrix of a data set with two features is . Find the two principal directions and the share of the variance that the first one carries. Then give the new value of the data point on the first direction.
Idea. The principal directions are the eigenvectors of , the largest eigenvalue first. The new value of a point is its scalar projection onto the unit vector of the direction (Part 9, and Topic 13).
The share of the first direction, about 73 percent of the variance, its unit vector, and the new value of the point are in the three lines above.
Why: the first principal direction belongs to the largest eigenvalue. The second feature has the larger variance (7 against 4), and the direction leans towards it. The new value is a scalar projection, the tool of Topic 13.
Problem 23: The long run
Every year 30 percent of the customers of phone company 1 change to company 2, and 40 percent of the customers of company 2 change to company 1. Write the matrix of the changes and find the shares in the long run.
Idea. The long-run shares are the eigenvector of the eigenvalue 1, scaled so that its components add up to 1 (Part 9).
The first column says where the customers of company 1 go, the second column where the customers of company 2 go.
The check: .
Why: dividing by 7 is an L1 normalization: shares must add up to 1. The second eigenvalue, 0.3, is small, so the shares settle quickly: every year the distance to the final shares shrinks to 30 percent of what it was.
Problem 24: Positive definite or not?
Which of the symmetric matrices and is positive definite? Check your answer with the number for and for .
Idea. A symmetric matrix is positive definite when all its eigenvalues are positive. Then is positive for every vector that is not zero (Part 8).
is positive definite, is not. With :
Why: for a 2 x 2 matrix a negative determinant already decides the question: the product of the eigenvalues is negative, so one of them is. The vector is the eigenvector of with the eigenvalue , and that is exactly where becomes negative.
Problem 25: Frobenius norm and singular values
For find the Frobenius norm and the singular values, and check that the squares of the singular values add up to the square of the Frobenius norm.
Idea. The singular values are the roots of the eigenvalues of (Part 11).
The singular values are and . The check: .
Why: is symmetric, so its eigenvalues are real, and they are never negative, so the roots exist. The sum of the eigenvalues of is its trace, and the trace of is the sum of the squares of all entries of : that is why the check must work.
Problem 26: A matrix from its eigenvectors
A matrix has the eigenvalue 2 with the eigenvector and the eigenvalue 3 with the eigenvector . Find .
Idea. Read the diagonalization backwards: (Part 7).
The checks: and . Also and .
Why: the eigenvalues and the eigenvectors together contain the whole matrix: the diagonalization, read backwards, builds it again. Four checks confirm the result.
14Practice problems
Forty problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. For an eigenvector every multiple of the vector given is right too.
Level 1: The basics
- 1.Is an eigenvector of ? If it is, give the eigenvalue.
Show a hint
Multiply and compare the result with .Show one possible solution
Answer: Yes, with the eigenvalue 2.
Both components were multiplied by 2, so the vector kept its direction.
- 2.Is an eigenvector of ?
Show a hint
Is a multiple of ? Compare the two components.Show one possible solution
Answer: No.
The first component was multiplied by 5 and the second by 3, so the vector was turned. (The eigenvectors of this matrix are with the eigenvalue 2 and with the eigenvalue 3.)
- 3.Give the eigenvalues and the eigenvectors of the diagonal matrix .
Show a hint
For a diagonal matrix nothing has to be calculated.Show one possible solution
Answer: 7 with the eigenvector , and with the eigenvector .
The matrix sends to and to . The eigenvalues of a diagonal matrix are its diagonal entries.
- 4.Give the eigenvalues of .
Show a hint
The matrix is triangular.Show one possible solution
Answer: and 6.
It is 0 for and for : the diagonal entries. Check: the sum is 5, the trace; the product is , the determinant.
- 5.Find the trace and the determinant of , and write its characteristic equation.
Show a hint
The equation is .Show one possible solution
Answer: , , and the equation is .
Minus the trace, plus the determinant: .
- 6.Find the eigenvalues of .
Show a hint
The trace is 3 and the determinant is .Show one possible solution
Answer: 5 and .
Check: and .
- 7.Find the eigenvalues of .
Show a hint
Look for two numbers with the sum 7 and the product 6.Show one possible solution
Answer: 6 and 1.
- 8.A 2 x 2 matrix has the eigenvalues 3 and . What are its trace and its determinant? Is the matrix invertible?
Show a hint
The sum and the product of the eigenvalues.Show one possible solution
Answer: The trace is 2 and the determinant is . The matrix is invertible.
The trace is , the determinant is . The determinant is not 0 (no eigenvalue is 0), so the matrix has an inverse.
- 9.Find the L1 norm, the L2 norm and the max norm of .
Show a hint
Absolute values for the L1 norm and the max norm, squares for the L2 norm.Show one possible solution
Answer: 17, 13 and 12.
- 10.Find the L1 norm, the L2 norm and the max norm of .
Show a hint
Four components, the same three recipes.Show one possible solution
Answer: 9, 5 and 4.
The order is as it must be.
- 11.Find the Manhattan, the Euclidean and the Chebyshev distance between and .
Show a hint
First the difference of the two vectors, then its three norms.Show one possible solution
Answer: 14, 10 and 8.
- 12.For find and .
Show a hint
Rule 2 of a norm: a number comes out of the norm as its absolute value.Show one possible solution
Answer: 12 and 20.
Directly: , with and .
- 13.Four words appear 3, 1, 4 and 2 times in a text. Normalize the vector of counts with the L1 norm.
Show a hint
Divide every component by the sum of the components.Show one possible solution
Answer: .
The L1 norm is . Dividing gives : the share of every word. The shares add up to 1.
Level 2: Eigenvalues, eigenvectors and norms at work
- 14.Find the eigenvectors of . Its eigenvalues are 6 and 1 (Problem 7).
Show a hint
For each eigenvalue subtract it from the main diagonal and solve one equation.Show one possible solution
Answer: for the eigenvalue 6 and for the eigenvalue 1 (or any multiples of them).
Check: , and .
- 15.Find the eigenvalues and the eigenvectors of the symmetric matrix . Are the eigenvectors perpendicular?
Show a hint
The trace is 1 and the determinant is .Show one possible solution
Answer: 3 with , and with . They are perpendicular.
The dot product is , as it must be for a symmetric matrix.
- 16.Find the eigenvalues and the eigenvectors of . Is the matrix invertible?
Show a hint
The determinant is 0.Show one possible solution
Answer: 0 with , and 7 with . The matrix is not invertible.
Check: . An eigenvalue 0 means that the determinant is 0: there is no inverse.
- 17.The matrices and both have the eigenvalue 5 twice. Which of them has eigenvectors in every direction?
Show a hint
Look at and at .Show one possible solution
Answer: . The matrix has only the direction .
Every vector solves . For the first row gives : only the vectors on the line of .
- 18.Which of the matrices and has real eigenvalues? Find them.
Show a hint
Look at the sign of for each matrix.Show one possible solution
Answer: , with the eigenvalues 3 and . has no real eigenvalues.
- 19.A matrix has the eigenvector with the eigenvalue 2 and the eigenvector with the eigenvalue . Find and for .
Show a hint
. The matrix multiplies each part by its eigenvalue.Show one possible solution
Answer: and .
- 20.A matrix has the eigenvalues 4 and . Give the eigenvalues of , of and of .
Show a hint
Square them, take one over them, subtract 3.Show one possible solution
Answer: : 16 and 4. : 0.25 and . : 1 and .
The eigenvectors are the same in all three cases.
- 21.One matrix has the eigenvalues 0 and 3, another one has the eigenvalues 2 and . Which of them is invertible, and what is its determinant?
Show a hint
The determinant is the product of the eigenvalues.Show one possible solution
Answer: The second one. Its determinant is .
First matrix: , so it is singular. Second matrix: , not 0, so it has an inverse.
- 22.Diagonalize (Problems 7 and 14): give and , with the larger eigenvalue first, and check that .
Show a hint
The eigenvectors are the columns of , the eigenvalues stand in the same order in .Show one possible solution
Answer: and .
The two products are equal.
- 23.The true values are and a model predicts . Find the MAE, the MSE and the largest error.
Show a hint
First the error vector , then its norms.Show one possible solution
Answer: MAE 1.5, MSE 3.5, largest error 3.
- 24.Which of the points and is nearer to the origin: with the Manhattan distance, and with the Euclidean distance?
Show a hint
Calculate the L1 norm and the L2 norm of both points.Show one possible solution
Answer: Manhattan: (7 against 8). Euclidean: (about 5.66 against 7).
The two distances disagree about the nearer point.
- 25.A model has the weights . Find the penalty of Lasso and the penalty of Ridge for . How many weights are not 0?
Show a hint
Lasso uses the L1 norm, Ridge the squared L2 norm.Show one possible solution
Answer: 0.7 for Lasso, 2.5 for Ridge. Two weights are not 0.
The weights 3 and are not 0.
- 26.A gradient is clipped to the L2 norm 5. What is the new vector?
Show a hint
Find the L2 norm of . If it is larger than 5, multiply by 5 divided by the norm.Show one possible solution
Answer: .
The new vector has the norm 5 and the direction of .
- 27.Check the triangle inequality in the L1 norm for and : give and .
Show a hint
Add the vectors first for the left side.Show one possible solution
Answer: 4 and 10.
Indeed : the components of and partly cancel each other.
- 28.Find the Frobenius norm of .
Show a hint
Square all four entries, add, take the root.Show one possible solution
Answer: 5.
Level 3: Putting ideas together
- 29.Find the eigenvalues of , and an eigenvector for .
Show a hint
The matrix is triangular. For the eigenvector subtract 3 from the diagonal.Show one possible solution
Answer: 1, 3 and . An eigenvector for 3 is .
The eigenvalues of a triangular matrix are its diagonal entries.
Check: .
- 30.Find the eigenvalues of . Is the matrix invertible?
Show a hint
The determinant of along the first row has only one part.Show one possible solution
Answer: 1, 0 and 5. The matrix is not invertible.
The eigenvalues are 1, 0 and 5. Check: their sum is , the trace. One eigenvalue is 0, so the determinant is 0 and there is no inverse.
- 31.For the symmetric matrix of Problem 15 give orthonormal eigenvectors and the matrices and with .
Show a hint
Divide each eigenvector by its length, .Show one possible solution
Answer: and . and .
Both eigenvectors have the length . The check:
- 32.Find for with the diagonalization of Problem 22.
Show a hint
, and .Show one possible solution
Answer: .
- 33.The covariance matrix of a data set is . Find the first principal direction and the share of the variance that it carries.
Show a hint
The eigenvector of the largest eigenvalue. The share is that eigenvalue divided by the trace.Show one possible solution
Answer: The direction , with , about 93 percent, of the variance.
The second direction is , with the variance 1.
- 34.Every week 20 percent of the viewers of channel 1 change to channel 2, and 30 percent of the viewers of channel 2 change to channel 1. Find the shares of the two channels in the long run.
Show a hint
Write the matrix with columns that add up to 1, and find the eigenvector of the eigenvalue 1.Show one possible solution
Answer: .
Check: . The other eigenvalue is .
- 35.Which of the symmetric matrices , and are positive definite?
Show a hint
Find the eigenvalues of each. All of them must be positive.Show one possible solution
Answer: Only .
has two positive eigenvalues. has a negative one. has an eigenvalue 0, so it is positive semidefinite but not positive definite.
- 36.A matrix has the eigenvalue 4 with the eigenvector and the eigenvalue 1 with the eigenvector . Find the matrix.
Show a hint
, with .Show one possible solution
Answer: .
Check: and .
- 37.Find the singular values of , and check them with the Frobenius norm.
Show a hint
First , then its eigenvalues, then their roots.Show one possible solution
Answer: and .
The singular values are and . Check: .
- 38.Find the three norms of and of . For which kind of vector are all three norms equal?
Show a hint
Calculate the six numbers and compare the two vectors.Show one possible solution
Answer: : 8, 4 and 2. : 8, 8 and 8. The norms are equal for vectors with only one component that is not 0.
When the size sits in one single component, the three norms agree. The more evenly it is spread, the further apart they are.
- 39.Two models fit the data equally well, with the weights and . Which one has the smaller Lasso penalty, and which the smaller Ridge penalty?
Show a hint
Compare the L1 norms, then the squared L2 norms.Show one possible solution
Answer: Lasso: (5 against 6). Ridge: (18 against 25).
Lasso prefers the sparse model, Ridge the model with the weight spread over both features.
- 40.Show that an eigenvector of with the eigenvalue is also an eigenvector of , and give its eigenvalue. What are the eigenvalues of for ?
Show a hint
Apply to and use twice.Show one possible solution
Answer: The eigenvalue is . For the matrix given: 12 and 2.
The matrix has the eigenvalues 3 and 1, so has and . Check:
Its trace is and its determinant is .
15Check yourself
Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.
0 of 12 answered.
16Checking your work with NumPy
This is the only code of the lesson. After you have solved a problem by hand, NumPy can confirm the result.
import numpy as np
B = np.array([[4, 1], [2, 3]])
values, vectors = np.linalg.eig(B)
print(values)
print(vectors)
x = np.array([3, -4])
print(np.linalg.norm(x, 1), np.linalg.norm(x), np.linalg.norm(x, np.inf))Output
[5. 2.] [[ 0.70710678 -0.4472136 ] [ 0.70710678 0.89442719]] 7.0 5.0 4.0
Change it and run: put a matrix of a problem you solved by hand into B, and a vector into x, and compare the results.
The eigenvalues come first: 5 and 2. The eigenvectors are the columns of the second result, and NumPy gives each of them the length 1. The first column is divided by . The second column is divided by : a multiple of our eigenvector, so the same answer. The last line shows the L1 norm, the L2 norm and the max norm of .
One trap: np.linalg.norm(x) without a second value is the L2 norm of a vector. For a matrix the same call gives the Frobenius norm.
| By hand | In NumPy |
|---|---|
| the eigenvalues and eigenvectors of | np.linalg.eig(A) |
| the same for a symmetric matrix | np.linalg.eigh(A) |
np.trace(A) | |
np.linalg.norm(x, 1) | |
np.linalg.norm(x) | |
np.linalg.norm(x, np.inf) | |
np.linalg.norm(A) | |
| the singular values of | np.linalg.svd(A, compute_uv=False) |
17Quick summary
| Idea | Definition or rule |
|---|---|
| Eigenvector and eigenvalue | , with not the zero vector |
| What it means | the matrix only stretches , by the factor |
| Characteristic equation | ; for a 2 x 2 matrix |
| Trace | the sum of the main diagonal = the sum of the eigenvalues |
| Determinant | the product of the eigenvalues |
| Eigenvectors | solve for every eigenvalue |
| Eigenvalue 0 | the matrix is singular |
| Diagonal or triangular matrix | the eigenvalues are the diagonal entries |
| Powers and inverse | has the eigenvalues , has , with the same eigenvectors |
| Dominant eigenvalue | the largest in size; it decides the long run |
| Diagonalization | , and |
| Symmetric matrix | real eigenvalues, perpendicular eigenvectors, |
| Orthogonal matrix | orthonormal columns; and |
| Positive definite matrix | symmetric, with only positive eigenvalues |
| Principal Component Analysis | the eigenvectors of the covariance matrix; share of a direction = its eigenvalue divided by the trace |
| Long-run state | the eigenvector of the eigenvalue 1, with components that add up to 1 |
| L1 norm | the sum of the absolute values |
| L2 norm | the root of the sum of the squares (the length) |
| Max norm | the largest absolute value |
| Rules of a norm | never negative; a number comes out as its absolute value; the triangle inequality |
| Distance | : Manhattan (L1), Euclidean (L2), Chebyshev (max) |
| MAE and MSE | and for the error vector |
| Lasso and Ridge | the penalties and |
| Frobenius norm | the root of the sum of the squares of all entries of a matrix |
| Singular values | the roots of the eigenvalues of |