Projections are used all over the place. For example in linear regression.
Example: Solving for Linear Regression Coefficients with OLS
In linear regression, we model data as follows:
y=XΞ²+Ο΅
Where:
y is the target variable
X is the matrix of features
Ξ² are the coefficients we want to find
Ο΅ is the error term
We can solve for the coefficients Ξ² using OLS by minimizing the squared error between predictions and actual values. Letβs solve this with a simple dataset.
This gives us the equation y=β0.82+1.85x1β+0.15x2β, which minimizes the squared error for our data points.
We can verify this is a good fit:
When x1β=1, x2β=2: yβ1.48 (actual: 2.1)
When x1β=2, x2β=4: yβ3.78 (actual: 3.8)
When x1β=3, x2β=5: yβ5.93 (actual: 6.2)
When x1β=4, x2β=4: yβ7.63 (actual: 7.9)
One small but important point: this is not how linear model coefficients are actually solved when fitting on real training data. In practice, libraries like scikit-learn use numerical methods like gradient descent or QR decomposition, which are much faster and more stable.
In terms of speed, computing the matrix inverse like we saw above is O(nΒ³) time complexity for an nΓn matrix, making it impractical for large datasets.
In terms of stability, even tiny perturbations in the input data can cause large swings in the computed inverse, making the solution unreliable for practical applications. This instability is due to floating-point arithmetic limitations and rounding errors that get amplified during matrix operations. This is particularly problematic when computing inverses of matrices that are close to singular (meaning some features are highly correlated or redundant), as small rounding errors in the computation can lead to drastically different results.
Riiiight then. Computers make everything tricky, donβt they. Letβs return to the perfect world of linear algebra.
Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors are crazy cool linear algebra constructs that have all sorts of applications including quantum mechanics.
An eigenvector is a vector that, when transformed by a matrix, only gets scaled (stretched or compressed) without changing direction.
For a matrix A, if vector v is an eigenvector with eigenvalue Ξ», then:
Av=Ξ»v
Letβs hold up for a second and think about this. Are we saying that A=Ξ»? No we are not.
Think about what we just talked about - how eigenvectors behave with respect to a matrix multiplication: the result is a vector that points in the same direction, but which has been scaled up or down. (In general this is certainly not the case for matrix multiplicationβjust look at the examples from earlier in this lesson.)
So with this in mind, we can equivalently write the matrix multiplication Av as a scalar multiplication Ξ»v, i.e. multiplying each value in the eigenvector v by some constant Ξ».
Letβs work through a simple example with a 2Γ2 matrix:
A=[31β13β]
To find eigenvalues, we solve what is known as the characteristic equation:
det(AβΞ»I)=0
Again, please excuse me for pulling this out of thin air. But just bear with me.
For our matrix:
det[3βΞ»1β13βΞ»β]=(3βΞ»)(3βΞ»)β1=0
(3βΞ»)2β1=0
Ξ»2β6Ξ»+8=0
Solving this quadratic equation like we learned back in grade 8:
So whatβs the significance of all this? Well, weβll look at a concrete example of using eigenvectors for ML in the PCA example below. For now, consider that the matrix A could represent any kind of transformation on 2D data.
How could that look? Letβs take a vector v=[91β] and repeatedly apply our transformation matrix A:
First application:
Av=[31β13β][91β]=[2812β]
Second application:
A2v=[31β13β][2812β]=[9664β]
Third application:
A3v=[31β13β][9664β]=[352256β]
Notice how the vector is not only getting longer, but its direction is gradually aligning more and more with the eigenvector v1β=[11β]. We can see this by looking at the ratios:
Original vector: 91β=0.111
After one application: 2812β=0.429
After two applications: 9664β=0.667
After three applications: 352256β=0.727
The ratio is approaching 1, which means the vector is converging towards the direction of v1β, our eigenvector with the largest eigenvalue!
The eigenvectors we found (v1β and v2β) tell us the βnaturalβ directions in which A operates, and their eigenvalues (4 and 2) tell us the strength of that operation in each direction.
This decomposition into eigenvectors helps us understand exactly how A transforms spaceβit stretches space by a factor of 4 in the direction [11β] and by a factor of 2 in the direction [1β1β].
Change of Basis
What we just did - expressing a vector in terms of eigenvectors - is an example of a change of basis.
Instead of using the standard basis vectors [10β] and [01β], we expressed our vector in terms of the eigenvectors.
The eigenvectors form whatβs called an eigenbasis. When we use this basis, the transformation A becomes incredibly simpleβit just scales each component by its corresponding eigenvalue. Letβs see this:
Original basis:
A[20β]=[31β13β][20β]=[62β]
Eigenbasis:
[20β]=1v1β+1v2β
A(1v1β+1v2β)=4(1v1β)+2(1v2β)=[62β]
In the eigenbasis, the transformation matrix becomes diagonal:
A=[40β02β]
This is why eigenvectors are so powerfulβthey reveal a coordinate system where complex transformations become simple scaling operations. The change of basis transforms our problem into a form thatβs much easier to understand and work with.
Example: Principal Component Analysis (PCA)
PCA is a dimensionality reduction technique that finds the directions (principal components) along which data has maximum variance, allowing us to represent high-dimensional data using fewer dimensions while preserving the most important patterns and relationships.
Letβs work through PCA on a small dataset with 3 samples and 4 features:
X=β183β294β745β428ββ
Step 1: Center the data by subtracting the mean of each feature:
Mean values: ΞΌ1β=4, ΞΌ2β=5, ΞΌ3β=316β, ΞΌ4β=314β
Remember our earlier discussion about change of basis?
PCA is finding a new coordinate system where:
The axes are orthogonal (perpendicular to each other).
The first axis points in the direction of maximum variance.
Each subsequent axis points in the direction of maximum remaining variance.
The eigenvalues tell us how much variance is explained by each direction.
When we drop the components with small eigenvalues, weβre effectively removing the directions in which our data varies the least, thus preserving the most important patterns in the data.
This is why PCA is so powerful for problems that benefit from dimensionality reduction, like feature extraction, data visualization, and noise reduction.
Conclusion
That was totally awesome, wasnβt it?
How sad is it that I studied Physics for 6 years without having a clear conceptual understanding of what eigenvectors and eigenvalues are?
Just for fun, check this out. Itβs the time-independent SchrΓΆdinger equation:
H^Ο=EΟ
Where:
H^ is the Hamiltonian operator,
Ο is the wavefunction,
E is the energy.
Look familiar? This is an eigenvalue equation! The wavefunctionsΟ are eigenvectors of H^ with eigenvaluesE.
But I digressβ¦
Iβd love to keep talking about linear algebra, but weβve got too many other important topics to cover. So itβs time to leave linear algebra behindβalthough (crucially) not forgotten.
Key Takeaways
π’ Matrix multiplication transforms vectors and matrices in predictable ways
π Linear transformations like projections map data into new spaces
π Eigenvectors reveal natural directions of transformations
π Change of basis helps simplify complex transformations
π‘ These concepts enable powerful techniques like PCA and linear regression
Next: Probability and Statistics
Next up, weβll learn about the most important concepts in probability and statistics for ML/AI.
Comments (0)
No comments yet.