Activity 6.6.1. Reading a PCA computation (U6-LO5).
The following code repeats part of the activity βPrincipal directions of a small data setβ.
import numpy as np
np.set_printoptions(precision=3, suppress=True)
X = np.array([
[4.0, 3.0],
[3.0, 4.0],
[0.0, 1.0],
[1.0, 0.0],
])
xbar = X.mean(axis=0)
Z = X - xbar
C = Z.T @ Z / len(X)
eigvals, V = np.linalg.eigh(C)
order = np.argsort(eigvals)[::-1]
eigvals = eigvals[order]
V = V[:, order]
v1 = V[:, 0]
scores1 = Z @ v1
Zhat1 = np.outer(scores1, v1)
R1 = Z - Zhat1
(
xbar,
C,
eigvals,
V,
Zhat1,
np.allclose(R1 @ v1, np.zeros(len(X))),
V.T @ C @ V,
)
Output:
(array([2., 2.]),
array([[2.5, 2. ],
[2. , 2.5]]),
array([4.5, 0.5]),
array([[ 0.707, -0.707],
[ 0.707, 0.707]]),
array([[ 1.5, 1.5],
[ 1.5, 1.5],
[-1.5, -1.5],
[-1.5, -1.5]]),
True,
array([[4.5, 0. ],
[0. , 0.5]]))
-
Why does the code reverse the order returned by
np.linalg.eigh? -
What do the columns of
Vrepresent? -
What geometric condition is checked by
np.allclose(R1 @ v1, np.zeros(len(X)))? -
Another valid numerical output could replace a column of
Vby its negative. What would happen to the corresponding scores and reconstructions? -
Interpret the final diagonal matrix. What fraction of the total centered variation is captured by the first principal direction?
Solution.
The shapes are
\begin{equation*}
X\in\R^{4\times2},
\qquad
\bar{\mathbf{x}}\in\R^2,
\qquad
Z\in\R^{4\times2},
\end{equation*}
\begin{equation*}
C\in\R^{2\times2},
\qquad
V\in\R^{2\times2},
\qquad
\mathbf{t}_1\in\R^4,
\qquad
\widehat Z_1\in\R^{4\times2}.
\end{equation*}
The array
xbar stores the sample mean
\begin{equation*}
\bar{\mathbf{x}}
=
\begin{bmatrix}
2\\
2
\end{bmatrix},
\end{equation*}
while
C stores the covariance matrix
\begin{equation*}
C=
\begin{bmatrix}
5/2 \amp 2\\
2 \amp 5/2
\end{bmatrix}.
\end{equation*}
The command
np.linalg.eigh returns the eigenvalues in increasing order. The reversal places the largest eigenvalue first. The first column of \(V\) is therefore a first principal direction, and the second column is a second principal direction.
The vector
scores1 stores
\begin{equation*}
\mathbf{t}_1=Z\mathbf{v}_1.
\end{equation*}
The rows of
Zhat1 are the projections of the centered data vectors onto
\begin{equation*}
\spans\{\mathbf{v}_1\}.
\end{equation*}
The matrix
R1 stores the corresponding reconstruction residuals.The product
R1 @ v1 computes the dot product of each residual row with \(\mathbf{v}_1\text{.}\) The value True checks that every reconstruction residual is numerically perpendicular to the first principal direction.
Replacing \(\mathbf{v}_1\) by \(-\mathbf{v}_1\) reverses the signs of its scores. However,
\begin{equation*}
(-t_i)(-\mathbf{v}_1)=t_i\mathbf{v}_1,
\end{equation*}
so the projected points and reconstruction residuals do not change. The principal axis is the same.
The final output says that covariance in the principal-coordinate system is
\begin{equation*}
V^TCV
=
\begin{bmatrix}
4.5 \amp 0\\
0 \amp 0.5
\end{bmatrix}.
\end{equation*}
The diagonal entries are the captured variations in the two principal directions. The first direction captures
\begin{equation*}
\frac{4.5}{4.5+0.5}
=
\frac9{10}
\end{equation*}
of the total centered variation.
