{"id":6763,"date":"2026-08-11T18:30:35","date_gmt":"2026-08-11T23:30:35","guid":{"rendered":"https:\/\/ykim.synology.me\/wordpress\/?p=6763"},"modified":"2026-11-26T22:38:34","modified_gmt":"2026-11-27T04:38:34","slug":"pca-algorithm","status":"publish","type":"post","link":"https:\/\/ykim.synology.me\/wordpress\/pca-algorithm-6763\/","title":{"rendered":"PCA Algorithm"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/yBarn1\/Images\/main\/parks\/apex-center-entrance.jpg\" alt=\"\" style=\"width:auto;height:500px\"\/><\/figure>\n\n\n<p><div class=\"github-readme-container markdown-body\"><div id=\"file\" class=\"md\" data-path=\"Feature-Engineering\/PCA\/pca-algorithm.md\"><article class=\"markdown-body entry-content container-lg\" itemprop=\"text\"><div class=\"markdown-heading\" dir=\"auto\"><h1 class=\"heading-element\" dir=\"auto\">PCA Algorithm<\/h1><a id=\"user-content-pca-algorithm\" class=\"anchor\" aria-label=\"Permalink: PCA Algorithm\" href=\"#pca-algorithm\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Version 9 | Created: 2026-08-11 | Updated: 2026-11-07 13:46 UTC<\/p>\n<ul dir=\"auto\">\n<li><a href=\"#1-notation\">1. Notation<\/a><\/li>\n<li><a href=\"#2-the-procedure\">2. The Procedure<\/a><\/li>\n<li><a href=\"#3-two-routes-to-the-same-answer\">3. Two Routes To The Same Answer<\/a><\/li>\n<li><a href=\"#4-what-comes-out\">4. What Comes Out<\/a><\/li>\n<li><a href=\"#5-choosing-the-component-count\">5. Choosing The Component Count<\/a><\/li>\n<li><a href=\"#6-applying-it-to-new-data\">6. Applying It To New Data<\/a><\/li>\n<li><a href=\"#7-conventions-and-numerical-details\">7. Conventions And Numerical Details<\/a><\/li>\n<li><a href=\"#appendix-a-terminology\">Appendix A. Terminology<\/a><\/li>\n<\/ul>\n<blockquote>\n<p dir=\"auto\">The other documents in this folder say which variant to reach for. This one says what the plain procedure actually does.\nIt fixes the notation, walks the steps in order, states what each step produces, and lists the conventions that decide whether two implementations agree.<\/p>\n<\/blockquote>\n<p dir=\"auto\">The PCA algorithm is a procedure for moving data expressed in <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p$<\/math-renderer> variables onto <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer> new axes (<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k \\lt p$<\/math-renderer>), ordered so that the information lost by keeping only <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer> of them is as small as possible. As laid out in Table 3 of \u00a74, it concretely produces three things.<\/p>\n<ol dir=\"auto\">\n<li>The loadings <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$V_k$<\/math-renderer> (<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p \\times k$<\/math-renderer>) are the new axes themselves \u2014 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer> orthogonal directions listed from the one along which the data spreads most. Each axis records the combination of the original variables it is built from, which is what answers where the variation in the data mainly comes from.<\/li>\n<li>The scores <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$T$<\/math-renderer> (<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n \\times k$<\/math-renderer>) are the coordinates on the new axes \u2014 each sample moved from <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p$<\/math-renderer> dimensions to <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer>, and the data that regression, classification, or any other downstream model actually receives. This is the product of the dimension reduction.<\/li>\n<li>The explained variance <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer> is the weight of each axis \u2014 the fraction of the total variation each component carries. Since the sum of the discarded <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer> equals the reconstruction error (the last paragraph of \u00a74), it gives a quantitative answer to what is lost by keeping only <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer> components.<\/li>\n<\/ol>\n<p dir=\"auto\">PCA (Principal Component Analysis) is short enough to write out in full, and writing it out settles questions that a library call hides \u2014 what the sign of a component means, why the rank stops at <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n - 1$<\/math-renderer>, which mean gets subtracted from new data, and what is lost when components are dropped.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">1. Notation<\/h2><a id=\"user-content-1-notation\" class=\"anchor\" aria-label=\"Permalink: 1. Notation\" href=\"#1-notation\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Table 1. Symbols used throughout<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Symbol<\/th>\n<th>Shape<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n \\times p$<\/math-renderer><\/td>\n<td>The data matrix, one sample per row and one variable per column<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n$<\/math-renderer><\/td>\n<td>\u2014<\/td>\n<td>The number of samples<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p$<\/math-renderer><\/td>\n<td>\u2014<\/td>\n<td>The number of variables<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer><\/td>\n<td>\u2014<\/td>\n<td>The number of components kept<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\mu$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$1 \\times p$<\/math-renderer><\/td>\n<td>The column means of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X$<\/math-renderer>\n<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\sigma$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$1 \\times p$<\/math-renderer><\/td>\n<td>The column standard deviations of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X$<\/math-renderer>\n<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X_c$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n \\times p$<\/math-renderer><\/td>\n<td>The centered, optionally scaled data<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$V$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p \\times r$<\/math-renderer><\/td>\n<td>The loadings, one principal direction per column<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$T$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n \\times r$<\/math-renderer><\/td>\n<td>The scores, the coordinates of each sample in the new basis<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer><\/td>\n<td>\u2014<\/td>\n<td>The variance explained by component <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$j$<\/math-renderer>\n<\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$r$<\/math-renderer><\/td>\n<td>\u2014<\/td>\n<td>The rank of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X_c$<\/math-renderer>, at most <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\min(n - 1, p)$<\/math-renderer>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\">The loadings are the new axes and the scores are the coordinates on them. The two are easy to confuse, because they come out of the same decomposition and both get called \"the components\" in casual use.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">2. The Procedure<\/h2><a id=\"user-content-2-the-procedure\" class=\"anchor\" aria-label=\"Permalink: 2. The Procedure\" href=\"#2-the-procedure\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\"><math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$$\n\\begin{aligned}\n&amp;\\text{1. center} &amp;&amp; X_c = X - \\mu &amp;&amp; \\mu \\text{ from the training rows only} \\\\\n&amp;\\text{2. scale} &amp;&amp; X_c = X_c \/ \\sigma &amp;&amp; \\text{optional, but the default for mixed units} \\\\\n&amp;\\text{3. decompose} &amp;&amp; X_c = U S V^\\top &amp;&amp; \\text{thin SVD} \\\\\n&amp;\\text{4. fix signs} &amp;&amp; \\text{flip } (u_j, v_j) \\text{ by convention} &amp;&amp; \\text{the pair is only defined up to sign} \\\\\n&amp;\\text{5. variance} &amp;&amp; \\lambda_j = s_j^2 \/ (n - 1) &amp;&amp; \\text{the variance along component } j \\\\\n&amp;\\text{6. truncate} &amp;&amp; \\text{keep the first } k \\text{ columns of } V \\\\\n&amp;\\text{7. project} &amp;&amp; T = X_c V_k &amp;&amp; \\text{the scores, } n \\times k\n\\end{aligned}\n$$<\/math-renderer><\/p>\n<p dir=\"auto\">Step 1 is not optional. Skip the centering and the first component points at the mean of the data rather than at the direction of its variation, because the criterion being maximized becomes the second moment about the origin instead of the variance.<\/p>\n<p dir=\"auto\">Step 2 is a modelling choice. Without scaling, the variable with the largest numerical range dominates, so a table that mixes nanometres with ohms has to be scaled. With scaling the decomposition runs on the correlation matrix instead of the covariance matrix, and the two give different components.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">3. Two Routes To The Same Answer<\/h2><a id=\"user-content-3-two-routes-to-the-same-answer\" class=\"anchor\" aria-label=\"Permalink: 3. Two Routes To The Same Answer\" href=\"#3-two-routes-to-the-same-answer\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">The decomposition in step 3 can be reached by either of two routes, and they agree in exact arithmetic.<\/p>\n<p dir=\"auto\">Table 2. Covariance route against SVD route<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Aspect<\/th>\n<th>Covariance route<\/th>\n<th>SVD route<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>What is formed<\/td>\n<td>\n<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$C = X_c^\\top X_c \/ (n - 1)$<\/math-renderer>, then its eigendecomposition<\/td>\n<td>The SVD of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X_c$<\/math-renderer> directly<\/td>\n<\/tr>\n<tr>\n<td>Loadings<\/td>\n<td>The eigenvectors of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$C$<\/math-renderer>\n<\/td>\n<td>The columns of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$V$<\/math-renderer>\n<\/td>\n<\/tr>\n<tr>\n<td>Variance<\/td>\n<td>The eigenvalues of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$C$<\/math-renderer>\n<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$s_j^2 \/ (n - 1)$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td>Cost<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$O(np^2 + p^3)$<\/math-renderer><\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$O(np \\cdot \\min(n, p))$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td>Conditioning<\/td>\n<td>Forming <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$C$<\/math-renderer> squares the condition number<\/td>\n<td>It works on <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$X_c$<\/math-renderer> as given<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\"><strong>The SVD route is the default, and the reason is numerical.<\/strong> Squaring the condition number means that variables which are nearly collinear \u2014 the normal state of measurement data \u2014 lose roughly half the available digits before the eigensolver ever starts. The covariance route stays useful when <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p$<\/math-renderer> is small enough that <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$C$<\/math-renderer> is cheap and its conditioning is not in question.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">4. What Comes Out<\/h2><a id=\"user-content-4-what-comes-out\" class=\"anchor\" aria-label=\"Permalink: 4. What Comes Out\" href=\"#4-what-comes-out\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Table 3. The three outputs and what each is for<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Output<\/th>\n<th>Shape<\/th>\n<th>Reading<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Loadings <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$V_k$<\/math-renderer>\n<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$p \\times k$<\/math-renderer><\/td>\n<td>How much each original variable contributes to each component<\/td>\n<\/tr>\n<tr>\n<td>Scores <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$T$<\/math-renderer>\n<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n \\times k$<\/math-renderer><\/td>\n<td>Where each sample sits in the reduced space, and the input to any downstream model<\/td>\n<\/tr>\n<tr>\n<td>Explained variance <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer>\n<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer><\/td>\n<td>How much of the total variation each component accounts for<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\">The explained variance ratio is <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j \/ \\sum \\lambda$<\/math-renderer>, where the sum runs over all <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$r$<\/math-renderer> components and not only the <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer> that were kept. Dividing by the sum of the kept ones inflates every figure and hides exactly what truncation discarded.<\/p>\n<p dir=\"auto\">Reconstruction follows the same path backwards. With <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\hat{X} = T V_k^\\top$<\/math-renderer>, undoing the scaling and adding <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\mu$<\/math-renderer> back returns the approximation in the original units, and the average squared error per sample equals the sum of the discarded <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer>. That identity is what makes the explained variance ratio a statement about reconstruction rather than a bare number.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">5. Choosing The Component Count<\/h2><a id=\"user-content-5-choosing-the-component-count\" class=\"anchor\" aria-label=\"Permalink: 5. Choosing The Component Count\" href=\"#5-choosing-the-component-count\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Table 4. Ways to fix <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$k$<\/math-renderer><\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>Rule<\/th>\n<th>Caution<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Cumulative variance<\/td>\n<td>Keep enough components to pass a stated fraction<\/td>\n<td>The fraction is a choice, not a finding<\/td>\n<\/tr>\n<tr>\n<td>Scree<\/td>\n<td>Keep the components before the bend in the eigenvalue plot<\/td>\n<td>The bend is often not sharp<\/td>\n<\/tr>\n<tr>\n<td>Kaiser<\/td>\n<td>On the correlation matrix, keep eigenvalues above one<\/td>\n<td>Crude, and it tends to keep too many<\/td>\n<\/tr>\n<tr>\n<td>Parallel analysis<\/td>\n<td>Keep the components whose eigenvalues exceed those of shuffled data<\/td>\n<td>It needs repeated runs but answers the right question<\/td>\n<\/tr>\n<tr>\n<td>Cross-validation<\/td>\n<td>Keep the count that minimizes held-out reconstruction error<\/td>\n<td>The most defensible, and the most expensive<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\">Parallel analysis compares the eigenvalues against a null in which there is no structure, and cross-validation asks directly which count generalizes. The other three settle the question by a threshold the reader has to choose.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">6. Applying It To New Data<\/h2><a id=\"user-content-6-applying-it-to-new-data\" class=\"anchor\" aria-label=\"Permalink: 6. Applying It To New Data\" href=\"#6-applying-it-to-new-data\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">A fitted PCA is three stored objects: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\mu$<\/math-renderer>, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\sigma$<\/math-renderer>, and <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$V_k$<\/math-renderer>. Transforming a new sample uses those stored values and nothing computed from the new data.<\/p>\n<div class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"# Python\nT_new = ((X_new - mu) \/ sigma) @ V_k          # scores for new rows\nX_hat = (T_new @ V_k.T) * sigma + mu          # back to the original units\"><pre><span class=\"pl-c\"># Python<\/span>\n<span class=\"pl-v\">T_new<\/span> <span class=\"pl-c1\">=<\/span> ((<span class=\"pl-v\">X_new<\/span> <span class=\"pl-c1\">-<\/span> <span class=\"pl-s1\">mu<\/span>) <span class=\"pl-c1\">\/<\/span> <span class=\"pl-s1\">sigma<\/span>) @ <span class=\"pl-v\">V_k<\/span>          <span class=\"pl-c\"># scores for new rows<\/span>\n<span class=\"pl-v\">X_hat<\/span> <span class=\"pl-c1\">=<\/span> (<span class=\"pl-v\">T_new<\/span> @ <span class=\"pl-v\">V_k<\/span>.<span class=\"pl-c1\">T<\/span>) <span class=\"pl-c1\">*<\/span> <span class=\"pl-s1\">sigma<\/span> <span class=\"pl-c1\">+<\/span> <span class=\"pl-s1\">mu<\/span>          <span class=\"pl-c\"># back to the original units<\/span><\/pre><\/div>\n<p dir=\"auto\"><strong>Recomputing <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\mu$<\/math-renderer> or <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\sigma$<\/math-renderer> from the new rows is the standard leakage error.<\/strong> It quietly moves the origin between fit and transform, so the scores of the two sets no longer live in the same coordinate system, and a validation score computed that way is not comparable to a production one.<\/p>\n<p dir=\"auto\">The same rule governs cross-validation. The centering, the scaling, and the decomposition all belong inside each training fold; performing them once on the whole dataset lets the held-out rows influence the axes they are later scored against.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">7. Conventions And Numerical Details<\/h2><a id=\"user-content-7-conventions-and-numerical-details\" class=\"anchor\" aria-label=\"Permalink: 7. Conventions And Numerical Details\" href=\"#7-conventions-and-numerical-details\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Table 5. Details that decide whether two implementations agree<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Detail<\/th>\n<th>Statement<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Sign<\/td>\n<td>\n<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$(u_j, v_j)$<\/math-renderer> and <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$(-u_j, -v_j)$<\/math-renderer> describe the same component, so a convention such as forcing the largest-magnitude entry of <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$v_j$<\/math-renderer> positive is needed for reproducibility<\/td>\n<\/tr>\n<tr>\n<td>Rank<\/td>\n<td>Centering removes one degree of freedom, so <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$r \\le \\min(n - 1, p)$<\/math-renderer> and any further components are numerical noise<\/td>\n<\/tr>\n<tr>\n<td>Denominator<\/td>\n<td>\n<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n - 1$<\/math-renderer> matches the unbiased sample variance; <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$n$<\/math-renderer> appears in some libraries and shifts every <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"ea24e52111c3f74cc716f10739647acc\">$\\lambda_j$<\/math-renderer> by a constant factor<\/td>\n<\/tr>\n<tr>\n<td>Ties<\/td>\n<td>Equal eigenvalues leave the rotation inside that subspace undetermined, so individual loadings are not interpretable there<\/td>\n<\/tr>\n<tr>\n<td>Missing values<\/td>\n<td>The decomposition has no notion of a missing entry, so it must be imputed first or a probabilistic variant used instead<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\">The sign convention matters more than it appears. Two runs that differ only in sign produce loading plots that look mirrored and scores that flip about zero, which reads as a change in the data when it is nothing of the kind.<\/p>\n<hr>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix A. Terminology<\/h2><a id=\"user-content-appendix-a-terminology\" class=\"anchor\" aria-label=\"Permalink: Appendix A. Terminology\" href=\"#appendix-a-terminology\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">The terms below appear in the body without being defined there.<\/p>\n<ul dir=\"auto\">\n<li><strong>Condition number<\/strong> measures how much a matrix amplifies relative error, so a large one means fewer reliable digits in the result.<\/li>\n<li><strong>Correlation matrix<\/strong> is the covariance matrix of the standardized variables.<\/li>\n<li><strong>Covariance matrix<\/strong> holds the pairwise covariances of the variables, with the variances on its diagonal.<\/li>\n<li><strong>Eigendecomposition<\/strong> writes a symmetric matrix as its eigenvectors scaled by its eigenvalues.<\/li>\n<li><strong>Kaiser rule<\/strong> keeps the components whose eigenvalue on the correlation matrix exceeds one.<\/li>\n<li><strong>Leakage<\/strong> is the use of information from held-out data while fitting, which makes a validation estimate optimistic.<\/li>\n<li><strong>Loading<\/strong> is the coefficient of an original variable in a principal component.<\/li>\n<li><strong>Parallel analysis<\/strong> compares the observed eigenvalues against those obtained from data with the same shape but no structure.<\/li>\n<li><strong>Rank<\/strong> is the number of linearly independent directions a matrix spans.<\/li>\n<li><strong>Score<\/strong> is the coordinate of a sample along a principal component.<\/li>\n<li><strong>Scree<\/strong> is the plot of eigenvalues in decreasing order, whose bend is taken as the component count.<\/li>\n<li><strong>SVD<\/strong> decomposes a matrix into left singular vectors, singular values, and right singular vectors.<\/li>\n<li><strong>Thin SVD<\/strong> returns only the singular vectors that correspond to non-zero singular values.<\/li>\n<\/ul>\n<\/article><\/div><\/div><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style='text-align:center' class='yasr-auto-insert-overall'><\/div><div style='text-align:center' class='yasr-auto-insert-visitor'><\/div>","protected":false},"excerpt":{"rendered":"<p>The plain procedure in seven steps: center, scale, decompose by SVD, fix signs, read the variance, truncate, project. Writing it out settles what a library call hides \u2014 what a component&#8217;s sign means, why the rank stops at n\u22121, which mean new data is centred by, and what truncation discards.<\/p>\n","protected":false},"author":4,"featured_media":6764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","yasr_overall_rating":0,"yasr_post_is_review":"","yasr_auto_insert_disabled":"","yasr_review_type":"","fifu_image_url":"","fifu_image_alt":"","iawp_total_views":5,"footnotes":""},"categories":[373,56],"tags":[386,385],"class_list":["post-6763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-feature-engineering-slug","category-data-science-slug","tag-github-hosted","tag-pca"],"yasr_visitor_votes":{"stars_attributes":{"read_only":false,"span_bottom":false},"number_of_votes":1,"sum_votes":3},"jetpack_featured_media_url":"https:\/\/ykim.synology.me\/wordpress\/wp-content\/uploads\/2026\/08\/apex-center-entrance.jpg","_links":{"self":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/6763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/comments?post=6763"}],"version-history":[{"count":3,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/6763\/revisions"}],"predecessor-version":[{"id":7467,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/6763\/revisions\/7467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media\/6764"}],"wp:attachment":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media?parent=6763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/categories?post=6763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/tags?post=6763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}