import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20METADATA%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22pca%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Principal%20Component%20Analysis%22%2C%0A%20%20%20%20%20%20%20%20%22types%22%3A%20%5B%22algorithm%22%5D%2C%0A%20%20%20%20%20%20%20%20%22families%22%3A%20%5B%22dimensionality_reduction%22%2C%20%22linear%22%5D%2C%0A%20%20%20%20%20%20%20%20%22tasks%22%3A%20%5B%22dimensionality_reduction%22%2C%20%22representation%22%5D%2C%0A%20%20%20%20%20%20%20%20%22data%22%3A%20%5B%22tabular%22%5D%2C%0A%20%20%20%20%20%20%20%20%22learning%22%3A%20%5B%22unsupervised%22%5D%2C%0A%20%20%20%20%20%20%20%20%22capacity%22%3A%20%22parametric%22%2C%0A%20%20%20%20%20%20%20%20%22mechanisms%22%3A%20%5B%22linear_projection%22%2C%20%22variance_maximization%22%2C%20%22matrix_factorization%22%5D%2C%0A%20%20%20%20%20%20%20%20%22properties%22%3A%20%5B%22interpretable%22%2C%20%22representation_learning%22%5D%2C%0A%20%20%20%20%20%20%20%20%22constraints%22%3A%20%5B%22requires_scaling%22%2C%20%22sensitive_to_outliers%22%5D%2C%0A%20%20%20%20%20%20%20%20%22difficulty%22%3A%20%22beginner%22%2C%0A%20%20%20%20%20%20%20%20%22status%22%3A%20%22complete%22%2C%0A%20%20%20%20%20%20%20%20%22explainability%22%3A%20%22medium%22%2C%0A%20%20%20%20%20%20%20%20%22training_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22inference_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22data_appetite%22%3A%20%22low%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20mo.md(f%22%23%20%7BMETADATA%5B'name'%5D%7D%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%23%23%20In%20one%20sentence%0A%0A%20%20%20%20PCA%20rotates%20data%20onto%20new%20perpendicular%20axes%20ordered%20by%20how%20much%20variance%20they%20capture.%0A%0A%20%20%20%20%23%23%20Mental%20model%0A%0A%20%20%20%20Imagine%20a%20long%20diagonal%20cloud%20of%20points.%20The%20original%20horizontal%20and%20vertical%20axes%20are%20awkward.%20PCA%20rotates%20the%20coordinate%20system%20so%20the%20first%20axis%20runs%20along%20the%20cloud%20and%20the%20second%20captures%20the%20smaller%20sideways%20spread.%0A%0A%20%20%20%20Keeping%20only%20the%20first%20axis%20compresses%20the%20data%20while%20losing%20as%20little%20squared%20reconstruction%20information%20as%20possible%20under%20a%20linear%20projection.%0A%0A%20%20%20%20%23%23%20Input%20and%20output%0A%0A%20%20%20%20-%20**Input%3A**%20numerical%20features.%0A%20%20%20%20-%20**Output%3A**%20component%20coordinates%2C%20loadings%2C%20and%20explained%20variance.%0A%20%20%20%20-%20**Learns%3A**%20orthogonal%20directions%20of%20decreasing%20variance.%0A%0A%20%20%20%20%23%23%20How%20it%20works%0A%0A%20%20%20%20After%20centering%20the%20data%2C%20PCA%20finds%20a%20first%20unit%20direction%20with%20maximum%20projected%20variance.%20Each%20following%20direction%20is%20perpendicular%20to%20earlier%20ones%20and%20captures%20as%20much%20remaining%20variance%20as%20possible.%0A%0A%20%20%20%20This%20can%20be%20computed%20through%20eigendecomposition%20of%20a%20covariance%20matrix%20or%2C%20more%20stably%2C%20singular%20value%20decomposition%20of%20the%20centered%20data%20matrix.%0A%0A%20%20%20%20Variance%20is%20not%20the%20same%20as%20usefulness.%20A%20low-variance%20direction%20can%20carry%20the%20label%20signal%3B%20a%20high-variance%20direction%20can%20be%20nuisance.%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%23%23%20A%20practical%20example%0A%0A%20%20%20%20Correlated%20sensor%20measurements%20may%20describe%20a%20smaller%20number%20of%20operating%20modes.%20PCA%20can%20compress%20them%20before%20visualization%20or%20modeling%20and%20reveal%20which%20sensors%20move%20together.%20The%20components%20are%20mathematical%20mixtures%2C%20so%20attach%20semantic%20names%20carefully.%0A%0A%20%20%20%20%23%23%20When%20to%20use%20it%0A%0A%20%20%20%20-%20Features%20are%20numerous%20and%20correlated.%0A%20%20%20%20-%20You%20need%20a%20fast%20linear%20compression%20baseline.%0A%20%20%20%20-%20Visualization%20in%20two%20or%20three%20dimensions%20is%20useful.%0A%20%20%20%20-%20Denoising%20through%20removal%20of%20low-variance%20directions%20is%20plausible.%0A%20%20%20%20-%20A%20downstream%20model%20suffers%20from%20redundant%20dimensions.%0A%0A%20%20%20%20%23%23%20When%20to%20avoid%20it%0A%0A%20%20%20%20-%20Important%20structure%20is%20nonlinear.%0A%20%20%20%20-%20Raw%20feature%20interpretability%20must%20be%20preserved.%0A%20%20%20%20-%20Features%20use%20incomparable%20scales%20and%20cannot%20be%20standardized%20meaningfully.%0A%20%20%20%20-%20Rare%20low-variance%20behavior%20is%20operationally%20critical.%0A%20%20%20%20-%20Missing%20values%20are%20common%20and%20not%20handled%20first.%0A%0A%20%20%20%20%23%23%20Data%20preparation%0A%0A%20%20%20%20Center%20features.%20Standardize%20when%20units%20or%20scales%20differ%20and%20each%20feature%20should%20initially%20receive%20comparable%20weight.%20Do%20not%20standardize%20blindly%20when%20absolute%20variance%20has%20physical%20meaning.%20Fit%20preprocessing%20and%20PCA%20only%20on%20the%20training%20fold%20to%20prevent%20leakage.%0A%0A%20%20%20%20%23%23%20Important%20controls%0A%0A%20%20%20%20%7C%20Control%20%7C%20Role%20%7C%0A%20%20%20%20%7C---------%7C------%7C%0A%20%20%20%20%7C%20%60n_components%60%20%7C%20Fixed%20count%20or%20variance%20fraction%20%7C%0A%20%20%20%20%7C%20%60whiten%60%20%7C%20Rescales%20components%20to%20unit%20variance%20%7C%0A%20%20%20%20%7C%20%60solver%60%20%7C%20Affects%20speed%20and%20numerical%20strategy%20%7C%0A%0A%20%20%20%20Choose%20components%20from%20reconstruction%2C%20downstream%20validation%2C%20and%20operational%20simplicity%20%E2%80%94%20not%20only%20a%20visually%20pleasing%20explained-variance%20threshold.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Notebook%20%E2%80%94%20rotate%2C%20compress%2C%20and%20question%20variance%0A%0A%20%20%20%20**Question%3A**%20what%20exactly%20is%20preserved%20by%20the%20first%20principal%20component%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%20%20%20%20from%20sklearn.preprocessing%20import%20StandardScaler%0A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(3)%0A%20%20%20%20base%20%3D%20rng.normal(size%3D500)%0A%20%20%20%20X%20%3D%20np.column_stack(%5B3%20*%20base%20%2B%20rng.normal(0%2C%200.4%2C%20500)%2C%20base%20%2B%20rng.normal(0%2C%200.2%2C%20500)%5D)%0A%20%20%20%20pca%20%3D%20PCA().fit(X)%0A%20%20%20%20Z%20%3D%20pca.transform(X)%0A%20%20%20%20print(%22explained%20variance%20ratio%3A%22%2C%20np.round(pca.explained_variance_ratio_%2C%203))%0A%20%20%20%20print(%22component%20directions%3A%5Cn%22%2C%20np.round(pca.components_%2C%203))%0A%20%20%20%20return%20PCA%2C%20X%2C%20Z%2C%20np%2C%20pca%2C%20plt%2C%20rng%0A%0A%0A%40app.cell%0Adef%20_(X%2C%20Z%2C%20np%2C%20pca%2C%20plt)%3A%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%204))%0A%20%20%20%20axes%5B0%5D.scatter(X%5B%3A%2C%200%5D%2C%20X%5B%3A%2C%201%5D%2C%20s%3D10%2C%20alpha%3D0.4)%0A%20%20%20%20center%20%3D%20pca.mean_%0A%20%20%20%20for%20variance%2C%20direction%20in%20zip(pca.explained_variance_%2C%20pca.components_)%3A%0A%20%20%20%20%20%20%20%20endpoint%20%3D%20center%20%2B%20direction%20*%20np.sqrt(variance)%20*%202%0A%20%20%20%20%20%20%20%20axes%5B0%5D.plot(%5Bcenter%5B0%5D%2C%20endpoint%5B0%5D%5D%2C%20%5Bcenter%5B1%5D%2C%20endpoint%5B1%5D%5D%2C%20linewidth%3D3)%0A%20%20%20%20axes%5B0%5D.set_title(%22Original%20coordinates%22)%0A%20%20%20%20axes%5B1%5D.scatter(Z%5B%3A%2C%200%5D%2C%20Z%5B%3A%2C%201%5D%2C%20s%3D10%2C%20alpha%3D0.4)%0A%20%20%20%20axes%5B1%5D.axhline(0%2C%20color%3D%22grey%22%2C%20linewidth%3D1)%0A%20%20%20%20axes%5B1%5D.set_title(%22Principal-component%20coordinates%22)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(PCA%2C%20np%2C%20rng)%3A%0A%20%20%20%20%23%20Low%20variance%20can%20still%20contain%20a%20target%20signal.%0A%20%20%20%20X_demo%20%3D%20np.column_stack(%5Brng.normal(0%2C%2020%2C%20600)%2C%20rng.normal(0%2C%200.4%2C%20600)%5D)%0A%20%20%20%20target%20%3D%20X_demo%5B%3A%2C%201%5D%20%3E%200%0A%20%20%20%20component%20%3D%20PCA(n_components%3D1).fit_transform(X_demo)%5B%3A%2C%200%5D%0A%20%20%20%20correlation%20%3D%20np.corrcoef(component%2C%20target.astype(float))%5B0%2C%201%5D%0A%20%20%20%20print(f%22correlation%20between%20retained%20high-variance%20component%20and%20target%3A%20%7Bcorrelation%3A.3f%7D%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%20Evaluation%20and%20diagnosis%0A%0A%20%20%20%20Plot%20cumulative%20explained%20variance%2C%20reconstruction%20error%2C%20and%20component%20loadings.%20Test%20the%20downstream%20task%20with%20and%20without%20PCA.%20Check%20component%20stability%20across%20time%20or%20resamples.%0A%0A%20%20%20%20A%20two-dimensional%20plot%20is%20a%20projection%2C%20not%20proof%20of%20natural%20clusters.%20Apparent%20separation%20can%20be%20created%20or%20erased%20by%20projection%20choices.%0A%0A%20%20%20%20%23%23%20Cost%20profile%0A%0A%20%20%20%20Training%20can%20be%20expensive%20when%20both%20samples%20and%20features%20are%20large%2C%20but%20randomized%20and%20incremental%20solvers%20help.%20Transforming%20new%20examples%20is%20a%20fast%20matrix%20multiplication.%0A%0A%20%20%20%20%23%23%20Related%20models%0A%0A%20%20%20%20-%20**K-Means**%20creates%20cluster%20assignments%20and%20is%20sometimes%20applied%20after%20PCA.%0A%20%20%20%20-%20Autoencoders%20learn%20nonlinear%20compression.%0A%20%20%20%20-%20t-SNE%20and%20UMAP%20emphasize%20local%20neighborhood%20visualization%20rather%20than%20global%20linear%20variance.%0A%20%20%20%20-%20**Linear%20Regression**%20can%20benefit%20when%20correlated%20features%20destabilize%20unregularized%20coefficients%2C%20though%20Ridge%20is%20often%20easier%20to%20interpret.%0A%0A%20%20%20%20%23%23%20Practical%20takeaway%0A%0A%20%20%20%20PCA%20is%20a%20precise%20answer%20to%20%22Which%20linear%20directions%20contain%20variance%3F%22%20Do%20not%20silently%20replace%20that%20question%20with%20%22Which%20information%20matters%3F%22%0A%20%20%20%20%22%22%22)%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
83217dd80b7437ec74962c7792afc569