import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%0A%20%20%20%20return%20PCA%2C%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Note%2027%3A%20Principal%20Component%20Analysis%2C%20Maximum%20Variance%20Projections%2C%20and%20SVD%20Geometry%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B26%20Hotelling%20T-Squared%5D(26_hotelling.py)%20%7C%20Next%20Note%3A%20%5B28%20Factor%20Analysis%5D(28_factor_analysis.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20In%20modern%20machine%20learning%2C%20high-dimensional%20datasets%20(embeddings%2C%20tabular%20feature%20collections%2C%20spectral%20signals%2C%20gene%20expression%20profiles)%20frequently%20contain%20hundreds%20or%20thousands%20of%20features.%20However%2C%20real-world%20data%20is%20rarely%20spread%20uniformly%20across%20all%20dimensions%3B%20it%20concentrates%20along%20an%20underlying%20low-dimensional%20manifold.%0A%0A%20%20%20%20**Principal%20Component%20Analysis%20(PCA)**%20is%20the%20foundational%20linear%20dimensionality%20reduction%20technique%20in%20statistical%20learning%3A%0A%20%20%20%201.%20**The%20Dual%20Formulations%20(Pearson%20%26%20Hotelling)**%3A%0A%20%20%20%20%20%20%20-%20**Maximum%20Variance%20(Hotelling%201933)**%3A%20Find%20orthogonal%20directions%20%24%5Cmathbf%7Bw%7D_1%2C%20%5Cdots%2C%20%5Cmathbf%7Bw%7D_k%24%20onto%20which%20the%20projection%20of%20the%20data%20retains%20maximum%20possible%20variance.%0A%20%20%20%20%20%20%20-%20**Minimum%20Reconstruction%20Error%20(Pearson%201901)**%3A%20Find%20the%20%24k%24-dimensional%20linear%20subspace%20that%20minimizes%20the%20mean%20squared%20Euclidean%20distance%20between%20the%20original%20data%20points%20and%20their%20orthogonal%20projections.%0A%20%20%20%20%20%20%20The%20Eckart-Young-Mirsky%20theorem%20establishes%20that%20both%20objectives%20lead%20to%20the%20exact%20same%20optimal%20subspace%20defined%20by%20the%20leading%20eigenvectors%20of%20the%20covariance%20matrix.%0A%20%20%20%202.%20**Orthogonal%20Decorrelation**%3A%20In%20the%20projected%20principal%20component%20coordinate%20system%20%24%5Cmathbf%7BZ%7D%20%3D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5Cmathbf%7BW%7D%24%2C%20all%20cross-covariances%20between%20distinct%20components%20are%20identically%20zero%20(%24%5Coperatorname%7BCov%7D(Z_i%2C%20Z_j)%20%3D%200%24%20for%20%24i%20%5Cneq%20j%24)%2C%20completely%20eliminating%20multicollinearity%20for%20downstream%20linear%20and%20logistic%20regression%20models.%0A%20%20%20%203.%20**Data%20Compression%20and%20Noise%20Filtering**%3A%20In%20many%20physical%20systems%2C%20small%20eigenvalues%20represent%20isotropic%20background%20noise.%20Truncating%20the%20bottom%20components%20compresses%20data%20while%20actively%20filtering%20out%20unstructured%20high-frequency%20noise.%0A%20%20%20%204.%20**Bridge%20to%20Singular%20Value%20Decomposition%20(SVD)**%3A%20Computing%20PCA%20through%20thin%20SVD%20of%20the%20centered%20data%20matrix%20%24%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%3D%20%5Cmathbf%7BU%7D%5Cboldsymbol%7B%5CSigma%7D%5Cmathbf%7BV%7D%5E%5Ctop%24%20avoids%20forming%20the%20%24d%20%5Ctimes%20d%24%20covariance%20matrix%20%24%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%24%2C%20providing%20numerical%20stability%20and%20optimal%20algorithmic%20performance%20(%24%5Cmathcal%7BO%7D(nd%20%5Cmin(n%2C%20d))%24).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%201.%20Data%20Centering%20and%20the%20Sample%20Covariance%20Matrix%0A%0A%20%20%20%20Consider%20a%20dataset%20of%20%24n%24%20observations%20across%20%24d%24%20features%20represented%20as%20a%20matrix%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20d%7D%24.%20The%20empirical%20sample%20mean%20vector%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7B%5Cmathbf%7Bx%7D%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D1%7D%5En%20%5Cmathbf%7Bx%7D_i%20%5Cin%20%5Cmathbb%7BR%7D%5Ed%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20**mean-centered%20data%20matrix**%20%24%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20d%7D%24%20is%20obtained%20by%20subtracting%20the%20mean%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%3D%20%5Cmathbf%7BX%7D%20-%20%5Cmathbf%7B1%7D_n%20%5Cbar%7B%5Cmathbf%7Bx%7D%7D%5E%5Ctop%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20sample%20covariance%20matrix%20%24%5Cmathbf%7BS%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bd%20%5Ctimes%20d%7D%24%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BS%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%5Cmathbf%7BS%7D%24%20is%20real%2C%20symmetric%20(%24%5Cmathbf%7BS%7D%20%3D%20%5Cmathbf%7BS%7D%5E%5Ctop%24)%2C%20and%20positive%20semi-definite%20(%24%5Cmathbf%7Bw%7D%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D%20%5Cgeq%200%24%20for%20all%20%24%5Cmathbf%7Bw%7D%24).%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20Derivation%20of%20the%20First%20Principal%20Component%20(Lagrangian%20Optimization)%0A%0A%20%20%20%20We%20seek%20a%20unit%20projection%20vector%20%24%5Cmathbf%7Bw%7D_1%20%5Cin%20%5Cmathbb%7BR%7D%5Ed%24%20(%24%5C%7C%5Cmathbf%7Bw%7D_1%5C%7C_2%20%3D%201%24)%20such%20that%20the%20variance%20of%20the%20projected%20scalar%20coordinates%20%24z_%7Bi1%7D%20%3D%20%5Ctilde%7B%5Cmathbf%7Bx%7D%7D_i%5E%5Ctop%20%5Cmathbf%7Bw%7D_1%24%20is%20maximized%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Coperatorname%7BVar%7D(%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5Cmathbf%7Bw%7D_1)%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20(%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5Cmathbf%7Bw%7D_1)%5E%5Ctop%20(%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5Cmathbf%7Bw%7D_1)%20%3D%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cleft(%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5Cright)%20%5Cmathbf%7Bw%7D_1%20%3D%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D_1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20constrained%20optimization%20problem%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmax_%7B%5Cmathbf%7Bw%7D_1%7D%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D_1%20%5Cquad%20%5Ctext%7Bsubject%20to%7D%20%5Cquad%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7Bw%7D_1%20%3D%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Formulating%20the%20Lagrangian%20with%20Lagrange%20multiplier%20%24%5Clambda_1%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathcal%7BL%7D(%5Cmathbf%7Bw%7D_1%2C%20%5Clambda_1)%20%3D%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D_1%20-%20%5Clambda_1%20(%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7Bw%7D_1%20-%201)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Computing%20the%20gradient%20with%20respect%20to%20%24%5Cmathbf%7Bw%7D_1%24%20and%20equating%20to%20zero%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cnabla_%7B%5Cmathbf%7Bw%7D_1%7D%20%5Cmathcal%7BL%7D%20%3D%202%5Cmathbf%7BS%7D%5Cmathbf%7Bw%7D_1%20-%202%5Clambda_1%20%5Cmathbf%7Bw%7D_1%20%3D%20%5Cmathbf%7B0%7D%20%5Cimplies%20%5Cmathbf%7BS%7D%5Cmathbf%7Bw%7D_1%20%3D%20%5Clambda_1%20%5Cmathbf%7Bw%7D_1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20is%20the%20classical%20**matrix%20eigenvalue%20equation**!%20Multiplying%20both%20sides%20on%20the%20left%20by%20%24%5Cmathbf%7Bw%7D_1%5E%5Ctop%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D_1%20%3D%20%5Clambda_1%20%5Cmathbf%7Bw%7D_1%5E%5Ctop%20%5Cmathbf%7Bw%7D_1%20%3D%20%5Clambda_1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Therefore%2C%20the%20maximum%20projected%20variance%20equals%20the%20eigenvalue%20%24%5Clambda_1%24.%20To%20maximize%20variance%2C%20%24%5Cmathbf%7Bw%7D_1%24%20must%20be%20the%20eigenvector%20of%20%24%5Cmathbf%7BS%7D%24%20corresponding%20to%20its%20largest%20eigenvalue%20%24%5Clambda_1%20%3D%20%5Clambda_%7B%5Cmax%7D%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20Subsequent%20Orthogonal%20Principal%20Components%0A%0A%20%20%20%20For%20the%20%24k%24-th%20principal%20component%20(%24k%20%5Cleq%20d%24)%2C%20we%20maximize%20variance%20subject%20to%20unit%20length%20and%20mutual%20orthogonality%20to%20all%20previously%20extracted%20components%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmax_%7B%5Cmathbf%7Bw%7D_k%7D%20%5Cmathbf%7Bw%7D_k%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7Bw%7D_k%20%5Cquad%20%5Ctext%7Bsubject%20to%7D%20%5Cquad%20%5Cmathbf%7Bw%7D_k%5E%5Ctop%20%5Cmathbf%7Bw%7D_k%20%3D%201%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cmathbf%7Bw%7D_k%5E%5Ctop%20%5Cmathbf%7Bw%7D_j%20%3D%200%20%5C%20%5Cforall%20j%20%3C%20k%0A%20%20%20%20%24%24%0A%0A%20%20%20%20By%20the%20Spectral%20Theorem%20for%20symmetric%20matrices%2C%20%24%5Cmathbf%7BS%7D%24%20has%20an%20orthogonal%20eigendecomposition%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BS%7D%20%3D%20%5Cmathbf%7BW%7D%20%5Cboldsymbol%7B%5CLambda%7D%20%5Cmathbf%7BW%7D%5E%5Ctop%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24%5Cmathbf%7BW%7D%20%3D%20%5B%5Cmathbf%7Bw%7D_1%2C%20%5Cmathbf%7Bw%7D_2%2C%20%5Cdots%2C%20%5Cmathbf%7Bw%7D_d%5D%24%20is%20an%20orthonormal%20matrix%20of%20eigenvectors%20(%24%5Cmathbf%7BW%7D%5E%5Ctop%20%5Cmathbf%7BW%7D%20%3D%20%5Cmathbf%7BI%7D_d%24)%2C%20and%20%24%5Cboldsymbol%7B%5CLambda%7D%20%3D%20%5Coperatorname%7Bdiag%7D(%5Clambda_1%2C%20%5Clambda_2%2C%20%5Cdots%2C%20%5Clambda_d)%24%20with%20%24%5Clambda_1%20%5Cgeq%20%5Clambda_2%20%5Cgeq%20%5Cdots%20%5Cgeq%20%5Clambda_d%20%5Cgeq%200%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20Principal%20Component%20Scores%20and%20Decorrelation%0A%0A%20%20%20%20The%20transformed%20coordinates%20%24%5Cmathbf%7BZ%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20d%7D%24%20(called%20**principal%20component%20scores**)%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BZ%7D%20%3D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%5Cmathbf%7BW%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20sample%20covariance%20of%20the%20transformed%20scores%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Coperatorname%7BCov%7D(%5Cmathbf%7BZ%7D)%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BZ%7D%5E%5Ctop%20%5Cmathbf%7BZ%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BW%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%5Cmathbf%7BW%7D%20%3D%20%5Cmathbf%7BW%7D%5E%5Ctop%20%5Cmathbf%7BS%7D%20%5Cmathbf%7BW%7D%20%3D%20%5Cboldsymbol%7B%5CLambda%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20%24%5Cboldsymbol%7B%5CLambda%7D%24%20is%20diagonal%2C%20all%20principal%20components%20are%20**mutually%20uncorrelated**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Coperatorname%7BCov%7D(Z_j%2C%20Z_k)%20%3D%200%20%5Cquad%20%5Cforall%20j%20%5Cneq%20k%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%205.%20Proportion%20of%20Variance%20Explained%20(PVE)%20and%20Scree%20Criterion%0A%0A%20%20%20%20The%20total%20sample%20variance%20is%20the%20trace%20of%20%24%5Cmathbf%7BS%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Coperatorname%7Btr%7D(%5Cmathbf%7BS%7D)%20%3D%20%5Csum_%7Bj%3D1%7D%5Ed%20s_%7Bjj%7D%20%3D%20%5Csum_%7Bj%3D1%7D%5Ed%20%5Clambda_j%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20Proportion%20of%20Variance%20Explained%20(PVE)%20by%20the%20%24k%24-th%20principal%20component%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BPVE%7D_k%20%3D%20%5Cfrac%7B%5Clambda_k%7D%7B%5Csum_%7Bj%3D1%7D%5Ed%20%5Clambda_j%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20cumulative%20proportion%20of%20variance%20explained%20by%20the%20first%20%24K%24%20components%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BCumulative%20PVE%7D_K%20%3D%20%5Cfrac%7B%5Csum_%7Bk%3D1%7D%5EK%20%5Clambda_k%7D%7B%5Csum_%7Bj%3D1%7D%5Ed%20%5Clambda_j%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%206.%20SVD%20Duality%20and%20Reconstruction%0A%0A%20%20%20%20Let%20the%20Singular%20Value%20Decomposition%20of%20the%20centered%20matrix%20be%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%3D%20%5Cmathbf%7BU%7D%20%5Cboldsymbol%7B%5CSigma%7D%20%5Cmathbf%7BV%7D%5E%5Ctop%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Then%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BS%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%5E%5Ctop%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BV%7D%20%5Cboldsymbol%7B%5CSigma%7D%20%5Cmathbf%7BU%7D%5E%5Ctop%20%5Cmathbf%7BU%7D%20%5Cboldsymbol%7B%5CSigma%7D%20%5Cmathbf%7BV%7D%5E%5Ctop%20%3D%20%5Cmathbf%7BV%7D%20%5Cleft(%5Cfrac%7B%5Cboldsymbol%7B%5CSigma%7D%5E2%7D%7Bn%20-%201%7D%5Cright)%20%5Cmathbf%7BV%7D%5E%5Ctop%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Thus%2C%20the%20right%20singular%20vectors%20%24%5Cmathbf%7BV%7D%24%20are%20identically%20the%20principal%20component%20loading%20vectors%20(%24%5Cmathbf%7BW%7D%20%3D%20%5Cmathbf%7BV%7D%24)%2C%20and%20the%20eigenvalues%20satisfy%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clambda_k%20%3D%20%5Cfrac%7B%5Csigma_k%5E2%7D%7Bn%20-%201%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20rank-%24K%24%20low-rank%20reconstruction%20of%20the%20centered%20data%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cmathbf%7BX%7D%7D_K%20%3D%20%5Cmathbf%7BZ%7D_K%20%5Cmathbf%7BW%7D_K%5E%5Ctop%20%3D%20%5Ctilde%7B%5Cmathbf%7BX%7D%7D%20%5Cmathbf%7BW%7D_K%20%5Cmathbf%7BW%7D_K%5E%5Ctop%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Simulation%20Data%3A%20Bivariate%20Elliptical%20Gaussian%20(n%20%3D%20120)%20with%20Strong%20Correlation%0A%20%20%20%20np.random.seed(20250101)%0A%20%20%20%20_n%20%3D%20120%0A%0A%20%20%20%20_mean_true%20%3D%20%5B3.0%2C%202.0%5D%0A%20%20%20%20_cov_true%20%3D%20%5B%5B10.0%2C%208.5%5D%2C%20%5B8.5%2C%2012.0%5D%5D%0A%20%20%20%20raw_samples%20%3D%20np.random.multivariate_normal(_mean_true%2C%20_cov_true%2C%20size%3D_n)%0A%0A%20%20%20%20%23%20Standardize%20data%0A%20%20%20%20x_standardized%20%3D%20(raw_samples%20-%20raw_samples.mean(axis%3D0))%20%2F%20raw_samples.std(axis%3D0%2C%20ddof%3D1)%0A%20%20%20%20df_pca_2d%20%3D%20pd.DataFrame(x_standardized%2C%20columns%3D%5B%22Feature_1%22%2C%20%22Feature_2%22%5D)%0A%20%20%20%20return%20df_pca_2d%2C%20x_standardized%0A%0A%0A%40app.cell%0Adef%20_(PCA%2C%20df_pca_2d%2C%20go%2C%20make_subplots%2C%20np%2C%20x_standardized)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%20Subplot%201%3A%20Standardized%20Data%20Scatter%20%2B%20Principal%20Component%20Loading%20Vectors%20(scaled%20by%202%20*%20sqrt(lambda))%0A%20%20%20%20%23%20Subplot%202%3A%20Projected%20PC%20Space%20(Z1%20vs%20Z2%3A%20Decorrelated%2C%20Aligned%20with%20Axes)%0A%20%20%20%20%23%20Subplot%203%3A%20Scree%20Plot%20%26%20Cumulative%20Explained%20Variance%20Ratio%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D3%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Original%20Space%20%26%20Eigenvector%20Axes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20Decorrelated%20PC%20Score%20Space%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Scree%20Plot%20%26%20Cumulative%20Variance%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.09%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Compute%20PCA%0A%20%20%20%20_pca%20%3D%20PCA()%0A%20%20%20%20_z_scores%20%3D%20_pca.fit_transform(x_standardized)%0A%20%20%20%20_eigenvals%20%3D%20_pca.explained_variance_%0A%20%20%20%20_eigenvecs%20%3D%20_pca.components_.T%20%20%23%20columns%20are%20eigenvectors%0A%20%20%20%20_pve%20%3D%20_pca.explained_variance_ratio_%0A%0A%20%20%20%20%23%20Subplot%201%3A%20Data%20%2B%20Eigenvectors%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_pca_2d%5B%22Feature_1%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Ddf_pca_2d%5B%22Feature_2%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6%2C%20color%3D%22%2364748b%22%2C%20opacity%3D0.7)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Centered%20Samples%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22F1%3A%20%25%7Bx%3A.2f%7D%3Cbr%3EF2%3A%20%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20PC1%20vector%20(scaled%20by%202%20*%20sqrt(lambda_1)%20for%202-sigma%20visualization)%0A%20%20%20%20_scale_pc1%20%3D%202.0%20*%20np.sqrt(_eigenvals%5B0%5D)%0A%20%20%20%20_scale_pc2%20%3D%202.0%20*%20np.sqrt(_eigenvals%5B1%5D)%0A%0A%20%20%20%20_v1%20%3D%20_eigenvecs%5B%3A%2C%200%5D%20*%20_scale_pc1%0A%20%20%20%20_v2%20%3D%20_eigenvecs%5B%3A%2C%201%5D%20*%20_scale_pc2%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20_v1%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_v1%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2306b6d4%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8%2C%20symbol%3D%22arrow-bar-up%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22PC1%20Vector%20(PVE%20%3D%20%7B_pve%5B0%5D*100%3A.1f%7D%25)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20_v2%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_v2%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ec4899%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8%2C%20symbol%3D%22arrow-bar-up%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22PC2%20Vector%20(PVE%20%3D%20%7B_pve%5B1%5D*100%3A.1f%7D%25)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Subplot%202%3A%20Projected%20PC%20Space%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_z_scores%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_z_scores%5B%3A%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6%2C%20color%3D%22%2310b981%22%2C%20opacity%3D0.75)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22PC%20Coordinates%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22PC1%3A%20%25%7Bx%3A.2f%7D%3Cbr%3EPC2%3A%20%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Subplot%203%3A%20Scree%20Plot%0A%20%20%20%20_components%20%3D%20%5B%22PC%201%22%2C%20%22PC%202%22%5D%0A%20%20%20%20_cum_pve%20%3D%20np.cumsum(_pve)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_components%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_pve%20*%20100.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%5B%22%2306b6d4%22%2C%20%22%23ec4899%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Individual%20PVE%20(%25)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bv*100%3A.1f%7D%25%22%20for%20v%20in%20_pve%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22auto%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_components%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_cum_pve%20*%20100.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23f59e0b%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Cumulative%20PVE%20(%25)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20title%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%22Principal%20Component%20Analysis%3A%20Maximum%20Variance%20Axes%2C%20Decorrelation%2C%20and%20PVE%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D0.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xanchor%3D%22center%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20font%3Ddict(size%3D16%2C%20family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22)%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.25%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D75%2C%20b%3D80)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Standardized%20Feature%201%22%2C%20range%3D%5B-3.5%2C%203.5%5D%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Standardized%20Feature%202%22%2C%20range%3D%5B-3.5%2C%203.5%5D%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Principal%20Component%201%20Score%22%2C%20range%3D%5B-3.5%2C%203.5%5D%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Principal%20Component%202%20Score%22%2C%20range%3D%5B-3.5%2C%203.5%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Principal%20Component%22%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Explained%20Variance%20Ratio%20(%25)%22%2C%20range%3D%5B0%2C%20110%5D%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20Below%20we%20implement%20two%20rigorous%2C%20production-grade%20demonstrations%3A%0A%20%20%20%201.%20**Full%20Step-by-Step%20PCA%20from%20Scratch**%3A%20Pure%20NumPy%20implementation%20computing%20sample%20covariance%2C%20eigendecomposition%2C%20eigenvalue%20sorting%2C%20projection%20matrix%2C%20transformed%20scores%2C%20and%20low-rank%20reconstruction%2C%20verified%20against%20%60sklearn.decomposition.PCA%60%20to%20machine%20precision.%0A%20%20%20%202.%20**Multi-Feature%20Dimensionality%20Reduction%20%26%20Reconstruction%20Diagnostics**%3A%20Generating%20a%205-dimensional%20correlated%20physical%20telemetry%20dataset%2C%20performing%20PCA%2C%20and%20evaluating%20the%20reconstruction%20error%20%24%5C%7C%5Cmathbf%7BX%7D%20-%20%5Chat%7B%5Cmathbf%7BX%7D%7D_K%5C%7C_F%24%20as%20a%20function%20of%20retained%20components%20%24K%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%205%5C%7D%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(PCA%2C%20np%2C%20pd%2C%20x_standardized)%3A%0A%20%20%20%20%23%20Example%201%3A%20Full%20Step-by-Step%20PCA%20from%20Scratch%20vs%20Scikit-Learn%0A%20%20%20%20_n%20%3D%20x_standardized.shape%5B0%5D%0A%0A%20%20%20%20%23%201.%20Manual%20Covariance%20Matrix%0A%20%20%20%20_cov_manual%20%3D%20(x_standardized.T%20%40%20x_standardized)%20%2F%20(_n%20-%201)%0A%0A%20%20%20%20%23%202.%20Eigendecomposition%0A%20%20%20%20_eigenvals_raw%2C%20_eigenvecs_raw%20%3D%20np.linalg.eigh(_cov_manual)%0A%0A%20%20%20%20%23%203.%20Sort%20eigenvalues%20descending%0A%20%20%20%20_sort_idx%20%3D%20np.argsort(_eigenvals_raw)%5B%3A%3A-1%5D%0A%20%20%20%20_eigenvals_manual%20%3D%20_eigenvals_raw%5B_sort_idx%5D%0A%20%20%20%20_eigenvecs_manual%20%3D%20_eigenvecs_raw%5B%3A%2C%20_sort_idx%5D%0A%0A%20%20%20%20%23%20Adjust%20sign%20convention%20to%20match%20Scikit-Learn%20(ensure%20positive%20component%20on%20largest%20absolute%20loading)%0A%20%20%20%20for%20_j%20in%20range(_eigenvecs_manual.shape%5B1%5D)%3A%0A%20%20%20%20%20%20%20%20_max_abs_idx%20%3D%20np.argmax(np.abs(_eigenvecs_manual%5B%3A%2C%20_j%5D))%0A%20%20%20%20%20%20%20%20if%20_eigenvecs_manual%5B_max_abs_idx%2C%20_j%5D%20%3C%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_eigenvecs_manual%5B%3A%2C%20_j%5D%20*%3D%20-1.0%0A%0A%20%20%20%20%23%204.%20Transform%20scores%0A%20%20%20%20_scores_manual%20%3D%20x_standardized%20%40%20_eigenvecs_manual%0A%0A%20%20%20%20%23%20Scikit-Learn%20Reference%0A%20%20%20%20_pca_sklearn%20%3D%20PCA().fit(x_standardized)%0A%20%20%20%20_scores_sklearn%20%3D%20_pca_sklearn.transform(x_standardized)%0A%0A%20%20%20%20_summary_rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22Eigenvalue%201%20(lambda_1)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Manual%20Scratch%20Value%22%3A%20f%22%7B_eigenvals_manual%5B0%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20Value%22%3A%20f%22%7B_pca_sklearn.explained_variance_%5B0%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20f%22%7Bnp.abs(_eigenvals_manual%5B0%5D%20-%20_pca_sklearn.explained_variance_%5B0%5D)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22Eigenvalue%202%20(lambda_2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Manual%20Scratch%20Value%22%3A%20f%22%7B_eigenvals_manual%5B1%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20Value%22%3A%20f%22%7B_pca_sklearn.explained_variance_%5B1%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20f%22%7Bnp.abs(_eigenvals_manual%5B1%5D%20-%20_pca_sklearn.explained_variance_%5B1%5D)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22PC1%20Variance%20Explained%20Ratio%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Manual%20Scratch%20Value%22%3A%20f%22%7B_eigenvals_manual%5B0%5D%20%2F%20np.sum(_eigenvals_manual)%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20Value%22%3A%20f%22%7B_pca_sklearn.explained_variance_ratio_%5B0%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20%22Exact%20Match%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22Max%20Score%20Coordinate%20Discrepancy%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Manual%20Scratch%20Value%22%3A%20f%22%7Bnp.max(np.abs(_scores_manual%20-%20_scores_sklearn))%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20Value%22%3A%20%22Reference%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20%22Machine%20Precision%20Match%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20_df_scratch_comparison%20%3D%20pd.DataFrame(_summary_rows)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(PCA%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20Example%202%3A%205-Dimensional%20Telemetry%20Dataset%20Low-Rank%20Reconstruction%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20_n_samples%20%3D%20150%0A%0A%20%20%20%20%23%20Underlying%202-dimensional%20latent%20signal%0A%20%20%20%20_latent_z1%20%3D%20np.random.normal(0%2C%203.0%2C%20_n_samples)%0A%20%20%20%20_latent_z2%20%3D%20np.random.normal(0%2C%201.5%2C%20_n_samples)%0A%0A%20%20%20%20%23%205%20observed%20correlated%20telemetry%20channels%0A%20%20%20%20_f1%20%3D%201.0%20*%20_latent_z1%20%2B%200.2%20*%20_latent_z2%20%2B%20np.random.normal(0%2C%200.4%2C%20_n_samples)%0A%20%20%20%20_f2%20%3D%200.9%20*%20_latent_z1%20-%200.5%20*%20_latent_z2%20%2B%20np.random.normal(0%2C%200.4%2C%20_n_samples)%0A%20%20%20%20_f3%20%3D%20-1.2%20*%20_latent_z1%20%2B%200.8%20*%20_latent_z2%20%2B%20np.random.normal(0%2C%200.4%2C%20_n_samples)%0A%20%20%20%20_f4%20%3D%200.3%20*%20_latent_z1%20%2B%201.4%20*%20_latent_z2%20%2B%20np.random.normal(0%2C%200.4%2C%20_n_samples)%0A%20%20%20%20_f5%20%3D%20-0.5%20*%20_latent_z1%20-%201.1%20*%20_latent_z2%20%2B%20np.random.normal(0%2C%200.4%2C%20_n_samples)%0A%0A%20%20%20%20_X_5d%20%3D%20np.column_stack(%5B_f1%2C%20_f2%2C%20_f3%2C%20_f4%2C%20_f5%5D)%0A%20%20%20%20_X_centered%20%3D%20_X_5d%20-%20np.mean(_X_5d%2C%20axis%3D0)%0A%0A%20%20%20%20_total_frobenius_norm%20%3D%20np.linalg.norm(_X_centered%2C%20ord%3D%22fro%22)%0A%0A%20%20%20%20_pca_5d%20%3D%20PCA().fit(_X_centered)%0A%20%20%20%20_eval_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20_k%20in%20range(1%2C%206)%3A%0A%20%20%20%20%20%20%20%20_scores_k%20%3D%20_pca_5d.transform(_X_centered)%5B%3A%2C%20%3A_k%5D%0A%20%20%20%20%20%20%20%20_components_k%20%3D%20_pca_5d.components_%5B%3A_k%2C%20%3A%5D%0A%20%20%20%20%20%20%20%20_recon_k%20%3D%20_scores_k%20%40%20_components_k%0A%20%20%20%20%20%20%20%20_residual_matrix%20%3D%20_X_centered%20-%20_recon_k%0A%20%20%20%20%20%20%20%20_recon_error%20%3D%20np.linalg.norm(_residual_matrix%2C%20ord%3D%22fro%22)%0A%20%20%20%20%20%20%20%20_pct_error%20%3D%20(_recon_error%20%2F%20_total_frobenius_norm)%20*%20100.0%0A%20%20%20%20%20%20%20%20_cum_var%20%3D%20np.sum(_pca_5d.explained_variance_ratio_%5B%3A_k%5D)%20*%20100.0%0A%0A%20%20%20%20%20%20%20%20_eval_records.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Retained%20Components%20(K)%22%3A%20_k%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Cumulative%20Variance%20Explained%22%3A%20f%22%7B_cum_var%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Frobenius%20Reconstruction%20Error%22%3A%20f%22%7B_recon_error%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Relative%20Error%20(%25)%22%3A%20f%22%7B_pct_error%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Subspace%20Recommendation%22%3A%20%22Optimal%20Low-Rank%20Cutoff%22%20if%20_k%20%3D%3D%202%20else%20(%22Under-represented%22%20if%20_k%20%3D%3D%201%20else%20%22Diminishing%20Returns%22)%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_reconstruction%20%3D%20pd.DataFrame(_eval_records)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b80d38896a186ac565bca02b9620ad1b