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.cluster%20import%20FeatureAgglomeration%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%20%20%20%20from%20sklearn.metrics%20import%20r2_score%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%20%20%20%20from%20sklearn.preprocessing%20import%20StandardScaler%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20FeatureAgglomeration%2C%0A%20%20%20%20%20%20%20%20LinearRegression%2C%0A%20%20%20%20%20%20%20%20PCA%2C%0A%20%20%20%20%20%20%20%20StandardScaler%2C%0A%20%20%20%20%20%20%20%20go%2C%0A%20%20%20%20%20%20%20%20make_subplots%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20r2_score%2C%0A%20%20%20%20%20%20%20%20train_test_split%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%5B%E2%86%90%2039%20Permutation%20Importance%5D(39_permutation_importance.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B41%20Pseudo%20R-squared%20%E2%86%92%5D(41_pseudo_r2.py)%0A%0A%20%20%20%20%23%20Dimensionality%20Reduction%3A%20Principal%20Component%20Analysis%20vs%20Feature%20Agglomeration%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%20High-dimensional%20datasets%20frequently%20suffer%20from%20the%20curse%20of%20dimensionality%20and%20severe%20multi-collinearity.%20When%20predictor%20features%20are%20strongly%20correlated%2C%20standard%20linear%20models%20become%20ill-conditioned%2C%20coefficient%20variances%20explode%2C%20and%20tree-based%20ensembles%20split%20arbitrarily%20among%20redundant%20signals.%0A%0A%20%20%20%20To%20compress%20feature%20spaces%20while%20preserving%20essential%20information%2C%20machine%20learning%20practitioners%20rely%20on%20two%20fundamentally%20distinct%20paradigms%3A%0A%0A%20%20%20%20%23%23%23%23%201.%20Feature%20Projection%3A%20Principal%20Component%20Analysis%20(PCA)%0A%20%20%20%20PCA%20finds%20orthogonal%20linear%20combinations%20of%20all%20original%20features%20that%20sequentially%20maximize%20variance%20along%20principal%20axes.%0A%20%20%20%20-%20**Strength**%3A%20Mathematically%20optimal%20for%20linear%20reconstruction%20error%20under%20a%20given%20latent%20dimensionality%20%24k%24.%0A%20%20%20%20-%20**Weakness**%3A%20**Loss%20of%20Interpretability**.%20Every%20principal%20component%20is%20a%20linear%20mixture%20involving%20all%20%24p%24%20original%20features.%20In%20clinical%2C%20regulatory%2C%20or%20operational%20domains%2C%20a%20stakeholder%20cannot%20act%20on%20%220.41%20Blood%20Pressure%20-%200.38%20Age%20%2B%200.52%20Cholesterol%22.%20Furthermore%2C%20all%20original%20sensors%20must%20still%20be%20collected%20and%20processed%20at%20test%20time.%0A%0A%20%20%20%20%23%23%23%23%202.%20Feature%20Grouping%3A%20Feature%20Agglomeration%0A%20%20%20%20Feature%20Agglomeration%20treats%20the%20features%20themselves%20as%20entities%20in%20an%20observation%20space%20and%20performs%20bottom-up%20hierarchical%20clustering%20directly%20on%20the%20columns%20of%20the%20dataset.%0A%20%20%20%20-%20**Strength**%3A%20**Preserves%20Physical%20Interpretability**.%20Correlated%20features%20are%20partitioned%20into%20disjoint%20clusters%20(e.g.%2C%20grouping%20all%20temperature%20sensors%20together%20and%20all%20pressure%20sensors%20together)%20and%20replaced%20by%20an%20aggregate%20summary%20statistic%20(such%20as%20their%20mean%20or%20median).%0A%20%20%20%20-%20**Strength**%3A%20**True%20Feature%20Pruning**.%20Once%20feature%20clusters%20are%20established%2C%20redundant%20sensors%20can%20be%20permanently%20removed%20from%20data%20collection%20pipelines.%0A%20%20%20%20-%20**Trade-Off**%3A%20Because%20it%20restricts%20transformations%20to%20simple%20averaging%20within%20disjoint%20subsets%20rather%20than%20arbitrary%20continuous%20rotations%2C%20it%20retains%20slightly%20less%20variance%20than%20PCA%20for%20an%20equivalent%20number%20of%20reduced%20dimensions%20%24k%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%5Bb%5D%20Mathematical%20Foundations%20and%20Algorithmic%20Mechanics%0A%0A%20%20%20%20%23%23%23%201.%20Data%20Representation%0A%0A%20%20%20%20Let%20%24X%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20p%7D%24%20denote%20a%20centered%20and%20standardized%20data%20matrix%20with%20%24N%24%20observations%20and%20%24p%24%20features%3A%0A%0A%20%20%20%20%24%24%5Cmathbb%7BE%7D%5Bx_%7B%5Ccdot%2C%20j%7D%5D%20%3D%200%2C%20%5Cquad%20%5Coperatorname%7BVar%7D(x_%7B%5Ccdot%2C%20j%7D)%20%3D%201%20%5Cquad%20%5Cforall%20j%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20p%5C%7D%24%24%0A%0A%20%20%20%20The%20sample%20correlation%20matrix%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24R%20%3D%20%5Cfrac%7B1%7D%7BN%20-%201%7D%20X%5E%5Ctop%20X%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bp%20%5Ctimes%20p%7D%24%24%0A%0A%20%20%20%20%23%23%23%202.%20Principal%20Component%20Analysis%20(PCA)%0A%0A%20%20%20%20PCA%20performs%20an%20eigendecomposition%20of%20the%20covariance%20%2F%20correlation%20matrix%20%24R%20%3D%20V%20%5CLambda%20V%5E%5Ctop%24%2C%20or%20equivalently%20the%20Singular%20Value%20Decomposition%20(SVD)%20of%20the%20data%20matrix%3A%0A%0A%20%20%20%20%24%24X%20%3D%20U%20%5CSigma%20V%5E%5Ctop%24%24%0A%0A%20%20%20%20where%20%24V%20%3D%20%5Bv_1%2C%20v_2%2C%20%5Cdots%2C%20v_p%5D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bp%20%5Ctimes%20p%7D%24%20is%20an%20orthogonal%20matrix%20whose%20columns%20are%20the%20eigenvectors%20(loadings)%2C%20and%20%24%5CSigma%20%3D%20%5Coperatorname%7Bdiag%7D(%5Csigma_1%2C%20%5Cdots%2C%20%5Csigma_p)%24%20contains%20singular%20values%20(%24%5Clambda_j%20%3D%20%5Cfrac%7B%5Csigma_j%5E2%7D%7BN-1%7D%24).%0A%0A%20%20%20%20The%20projection%20onto%20the%20top%20%24k%24%20principal%20components%20is%3A%0A%0A%20%20%20%20%24%24Z_%7B%5Ctext%7BPCA%7D%7D%20%3D%20X%20V_k%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20k%7D%2C%20%5Cquad%20%5Ctext%7Bwhere%20%7D%20z_%7Bi%2C%20m%7D%20%3D%20%5Csum_%7Bj%3D1%7D%5Ep%20v_%7Bj%2C%20m%7D%20x_%7Bi%2C%20j%7D%24%24%0A%0A%20%20%20%20Each%20latent%20coordinate%20%24z_%7Bi%2C%20m%7D%24%20requires%20knowledge%20of%20every%20original%20feature%20%24x_%7Bi%2C%20j%7D%24.%0A%0A%20%20%20%20%23%23%23%203.%20Feature%20Agglomeration%20(Hierarchical%20Feature%20Pooling)%0A%0A%20%20%20%20Feature%20Agglomeration%20transposes%20the%20learning%20problem%3A%20each%20feature%20%24j%24%20is%20treated%20as%20an%20observation%20vector%20%24f_j%20%3D%20(x_%7B1%2C%20j%7D%2C%20x_%7B2%2C%20j%7D%2C%20%5Cdots%2C%20x_%7BN%2C%20j%7D)%5E%5Ctop%20%5Cin%20%5Cmathbb%7BR%7D%5EN%24.%0A%0A%20%20%20%20%23%23%23%23%20Feature%20Metric%20Space%0A%20%20%20%20The%20squared%20Euclidean%20distance%20between%20two%20standardized%20feature%20vectors%20%24f_j%24%20and%20%24f_l%24%20is%20directly%20proportional%20to%20their%20Pearson%20correlation%20coefficient%20%24r_%7Bjl%7D%24%3A%0A%0A%20%20%20%20%24%24%5C%7Cf_j%20-%20f_l%5C%7C_2%5E2%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20(x_%7Bij%7D%20-%20x_%7Bil%7D)%5E2%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20x_%7Bij%7D%5E2%20%2B%20%5Csum_%7Bi%3D1%7D%5EN%20x_%7Bil%7D%5E2%20-%202%20%5Csum_%7Bi%3D1%7D%5EN%20x_%7Bij%7D%20x_%7Bil%7D%20%3D%202(N%20-%201)(1%20-%20r_%7Bjl%7D)%24%24%0A%0A%20%20%20%20Thus%2C%20clustering%20standardized%20columns%20using%20Euclidean%20distance%20with%20Ward's%20linkage%20groups%20features%20that%20exhibit%20high%20mutual%20correlation.%0A%0A%20%20%20%20%23%23%23%23%20Aggregation%20and%20Pooling%0A%20%20%20%20Hierarchical%20clustering%20produces%20%24k%24%20disjoint%20subsets%20of%20feature%20indices%20%24%5Cmathcal%7BG%7D_1%2C%20%5Cmathcal%7BG%7D_2%2C%20%5Cdots%2C%20%5Cmathcal%7BG%7D_k%24%20such%20that%3A%0A%0A%20%20%20%20%24%24%5Cbigcup_%7Bm%3D1%7D%5Ek%20%5Cmathcal%7BG%7D_m%20%3D%20%5C%7B1%2C%20%5Cdots%2C%20p%5C%7D%2C%20%5Cquad%20%5Ctext%7Bwith%20%7D%20%5Cmathcal%7BG%7D_a%20%5Ccap%20%5Cmathcal%7BG%7D_b%20%3D%20%5Cemptyset%20%5Cquad%20%5Cforall%20a%20%5Cneq%20b%24%24%0A%0A%20%20%20%20The%20transformed%20reduced%20matrix%20%24Z_%7B%5Ctext%7BFA%7D%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20k%7D%24%20pools%20each%20cluster%20via%20the%20sample%20mean%3A%0A%0A%20%20%20%20%24%24z_%7Bi%2C%20m%7D%20%3D%20%5Cfrac%7B1%7D%7B%7C%5Cmathcal%7BG%7D_m%7C%7D%20%5Csum_%7Bj%20%5Cin%20%5Cmathcal%7BG%7D_m%7D%20x_%7Bi%2C%20j%7D%24%24%0A%0A%20%20%20%20In%20matrix%20notation%2C%20this%20corresponds%20to%20multiplication%20by%20a%20sparse%20binary%20block%20projection%20matrix%20%24W%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bp%20%5Ctimes%20k%7D%24%3A%0A%0A%20%20%20%20%24%24Z_%7B%5Ctext%7BFA%7D%7D%20%3D%20X%20W%2C%20%5Cquad%20W_%7Bj%2C%20m%7D%20%3D%20%5Cbegin%7Bcases%7D%20%5Cfrac%7B1%7D%7B%7C%5Cmathcal%7BG%7D_m%7C%7D%20%26%20%5Ctext%7Bif%20%7D%20j%20%5Cin%20%5Cmathcal%7BG%7D_m%20%5C%5C%200%20%26%20%5Ctext%7Botherwise%7D%20%5Cend%7Bcases%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FeatureAgglomeration%2C%20PCA%2C%20StandardScaler%2C%20np%2C%20pd)%3A%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20n_samples%20%3D%20600%0A%0A%20%20%20%20%23%20Create%203%20latent%20generative%20factors%0A%20%20%20%20z1%20%3D%20np.random.normal(0%2C%201%2C%20n_samples)%0A%20%20%20%20z2%20%3D%20np.random.normal(0%2C%201%2C%20n_samples)%0A%20%20%20%20z3%20%3D%20np.random.normal(0%2C%201%2C%20n_samples)%0A%0A%20%20%20%20%23%20Cluster%201%3A%20Features%20driven%20primarily%20by%20z1%20(e.g.%20Engine%20Sensors)%0A%20%20%20%20x1%20%3D%20z1%20%2B%20np.random.normal(0%2C%200.25%2C%20n_samples)%0A%20%20%20%20x2%20%3D%200.9%20*%20z1%20%2B%20np.random.normal(0%2C%200.30%2C%20n_samples)%0A%20%20%20%20x3%20%3D%200.85%20*%20z1%20%2B%20np.random.normal(0%2C%200.35%2C%20n_samples)%0A%0A%20%20%20%20%23%20Cluster%202%3A%20Features%20driven%20primarily%20by%20z2%20(e.g.%20Environmental%20Sensors)%0A%20%20%20%20x4%20%3D%20z2%20%2B%20np.random.normal(0%2C%200.25%2C%20n_samples)%0A%20%20%20%20x5%20%3D%200.92%20*%20z2%20%2B%20np.random.normal(0%2C%200.28%2C%20n_samples)%0A%20%20%20%20x6%20%3D%200.88%20*%20z2%20%2B%20np.random.normal(0%2C%200.32%2C%20n_samples)%0A%0A%20%20%20%20%23%20Cluster%203%3A%20Features%20driven%20primarily%20by%20z3%20(e.g.%20Electrical%20Load)%0A%20%20%20%20x7%20%3D%20z3%20%2B%20np.random.normal(0%2C%200.25%2C%20n_samples)%0A%20%20%20%20x8%20%3D%200.95%20*%20z3%20%2B%20np.random.normal(0%2C%200.25%2C%20n_samples)%0A%0A%20%20%20%20%23%20Independent%20Noise%20Feature%0A%20%20%20%20x9%20%3D%20np.random.normal(0%2C%201%2C%20n_samples)%0A%0A%20%20%20%20raw_data%20%3D%20np.column_stack(%5Bx1%2C%20x2%2C%20x3%2C%20x4%2C%20x5%2C%20x6%2C%20x7%2C%20x8%2C%20x9%5D)%0A%20%20%20%20feature_labels%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Eng_RPM%22%2C%0A%20%20%20%20%20%20%20%20%22Eng_Torque%22%2C%0A%20%20%20%20%20%20%20%20%22Eng_FuelRate%22%2C%0A%20%20%20%20%20%20%20%20%22Amb_Temp%22%2C%0A%20%20%20%20%20%20%20%20%22Amb_Humidity%22%2C%0A%20%20%20%20%20%20%20%20%22Amb_Pressure%22%2C%0A%20%20%20%20%20%20%20%20%22Volt_Battery%22%2C%0A%20%20%20%20%20%20%20%20%22Volt_Alternator%22%2C%0A%20%20%20%20%20%20%20%20%22Vib_Noise%22%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20scaler%20%3D%20StandardScaler()%0A%20%20%20%20x_scaled%20%3D%20scaler.fit_transform(raw_data)%0A%20%20%20%20df_scaled%20%3D%20pd.DataFrame(x_scaled%2C%20columns%3Dfeature_labels)%0A%0A%20%20%20%20%23%20Target%20variable%20driven%20linearly%20by%20the%203%20core%20latent%20factors%20%2B%20noise%0A%20%20%20%20y_target%20%3D%203.0%20*%20z1%20-%202.5%20*%20z2%20%2B%201.8%20*%20z3%20%2B%20np.random.normal(0%2C%200.6%2C%20n_samples)%0A%0A%20%20%20%20%23%20Fit%20PCA%20across%20all%20components%20k%20%3D%201%20..%209%0A%20%20%20%20pca_full%20%3D%20PCA().fit(x_scaled)%0A%20%20%20%20pca_cum_var%20%3D%20np.cumsum(pca_full.explained_variance_ratio_)%0A%0A%20%20%20%20%23%20Fit%20Feature%20Agglomeration%20across%20k%20%3D%201%20..%209%0A%20%20%20%20fa_cum_var%20%3D%20%5B%5D%0A%20%20%20%20for%20k_val%20in%20range(1%2C%2010)%3A%0A%20%20%20%20%20%20%20%20fa%20%3D%20FeatureAgglomeration(n_clusters%3Dk_val)%0A%20%20%20%20%20%20%20%20x_fa%20%3D%20fa.fit_transform(x_scaled)%0A%20%20%20%20%20%20%20%20%23%20Approximate%20reconstruction%20by%20mapping%20pooled%20means%20back%20to%20features%0A%20%20%20%20%20%20%20%20x_reconstructed%20%3D%20fa.inverse_transform(x_fa)%0A%20%20%20%20%20%20%20%20%23%20Fraction%20of%20total%20variance%20explained%3A%201%20-%20MSE%20%2F%20Total%20Var%0A%20%20%20%20%20%20%20%20mse%20%3D%20np.mean((x_scaled%20-%20x_reconstructed)%20**%202)%0A%20%20%20%20%20%20%20%20fa_cum_var.append(max(0.0%2C%201.0%20-%20mse))%0A%0A%20%20%20%20fa_cum_var%20%3D%20np.array(fa_cum_var)%0A%0A%20%20%20%20%23%20Fit%20k%20%3D%203%20models%20for%20direct%20comparison%0A%20%20%20%20pca_3%20%3D%20PCA(n_components%3D3).fit(x_scaled)%0A%20%20%20%20fa_3%20%3D%20FeatureAgglomeration(n_clusters%3D3).fit(x_scaled)%0A%0A%20%20%20%20corr_matrix%20%3D%20df_scaled.corr().values%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20corr_matrix%2C%0A%20%20%20%20%20%20%20%20fa_3%2C%0A%20%20%20%20%20%20%20%20fa_cum_var%2C%0A%20%20%20%20%20%20%20%20feature_labels%2C%0A%20%20%20%20%20%20%20%20pca_3%2C%0A%20%20%20%20%20%20%20%20pca_cum_var%2C%0A%20%20%20%20%20%20%20%20x_scaled%2C%0A%20%20%20%20%20%20%20%20y_target%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20corr_matrix%2C%0A%20%20%20%20fa_cum_var%2C%0A%20%20%20%20feature_labels%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pca_cum_var%2C%0A)%3A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EFeature%20Correlation%20Matrix%20(Block-Collinear%20Structure)%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EVariance%20Retention%3A%20PCA%20vs%20Feature%20Agglomeration%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.14%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Correlation%20Heatmap%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Heatmap(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3Dnp.round(corr_matrix%2C%202)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dfeature_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dfeature_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22RdBu_r%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zmin%3D-1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zmax%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3Dnp.round(corr_matrix%2C%202)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20texttemplate%3D%22%25%7Btext%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorbar%3Ddict(title%3D%22Correlation%22%2C%20x%3D0.44%2C%20len%3D0.8)%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%20Right%3A%20Cumulative%20variance%20explained%0A%20%20%20%20dims%20%3D%20np.arange(1%2C%2010)%0A%20%20%20%20fig.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%3Ddims%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpca_cum_var%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%232563EB%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8%2C%20color%3D%22%231D4ED8%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22PCA%20(Optimal%20Linear%20Projection)%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%20fig.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%3Ddims%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dfa_cum_var%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%2310B981%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8%2C%20color%3D%22%23047857%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Feature%20Agglomeration%20(Cluster%20Pooling)%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%20fig.add_vline(%0A%20%20%20%20%20%20%20%20x%3D3%2C%0A%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23DC2626%22%2C%20width%3D1.5%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20annotation_text%3D%22k%20%3D%203%20Natural%20Clusters%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22bottom%20right%22%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%20fig.update_xaxes(title_text%3D%22Target%20Reduced%20Dimension%20(k)%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Proportion%20of%20Variance%20Retained%22%2C%20range%3D%5B0.3%2C%201.05%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20fig.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%3D520%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D70%2C%20b%3D50)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.28%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20viz%20%3D%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LinearRegression%2C%0A%20%20%20%20fa_3%2C%0A%20%20%20%20feature_labels%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pca_3%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20r2_score%2C%0A%20%20%20%20train_test_split%2C%0A%20%20%20%20x_scaled%2C%0A%20%20%20%20y_target%2C%0A)%3A%0A%20%20%20%20%23%20Example%201%3A%20Loadings%20Sparsity%20Comparison%0A%20%20%20%20pca_loadings%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20pca_3.components_.T%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22PCA_PC1%22%2C%20%22PCA_PC2%22%2C%20%22PCA_PC3%22%5D%2C%0A%20%20%20%20%20%20%20%20index%3Dfeature_labels%2C%0A%20%20%20%20).round(3)%0A%0A%20%20%20%20%23%20Feature%20Agglomeration%20Cluster%20Assignments%0A%20%20%20%20fa_clusters%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Feature%22%3A%20feature_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22FA_Assigned_Cluster%22%3A%20%5Bf%22Cluster_%7Bc%7D%22%20for%20c%20in%20fa_3.labels_%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Cluster_Interpretation%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Engine%20Dynamics%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20c%20%3D%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20(%22Ambient%20Environment%22%20if%20c%20%3D%3D%200%20else%20%22Electrical%20%2F%20Power%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20c%20in%20fa_3.labels_%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%202%3A%20Downstream%20Regression%20Predictive%20Benchmark%0A%20%20%20%20x_tr%2C%20x_te%2C%20y_tr%2C%20y_te%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20x_scaled%2C%20y_target%2C%20test_size%3D0.35%2C%20random_state%3D42%0A%20%20%20%20)%0A%0A%20%20%20%20%23%201.%20Full%20original%20model%20(p%20%3D%209)%0A%20%20%20%20m_full%20%3D%20LinearRegression().fit(x_tr%2C%20y_tr)%0A%20%20%20%20r2_full%20%3D%20r2_score(y_te%2C%20m_full.predict(x_te))%0A%20%20%20%20cond_full%20%3D%20np.linalg.cond(x_tr.T%20%40%20x_tr)%0A%0A%20%20%20%20%23%202.%20PCA%20reduced%20model%20(k%20%3D%203)%0A%20%20%20%20x_tr_pca%20%3D%20pca_3.transform(x_tr)%0A%20%20%20%20x_te_pca%20%3D%20pca_3.transform(x_te)%0A%20%20%20%20m_pca%20%3D%20LinearRegression().fit(x_tr_pca%2C%20y_tr)%0A%20%20%20%20r2_pca%20%3D%20r2_score(y_te%2C%20m_pca.predict(x_te_pca))%0A%20%20%20%20cond_pca%20%3D%20np.linalg.cond(x_tr_pca.T%20%40%20x_tr_pca)%0A%0A%20%20%20%20%23%203.%20Feature%20Agglomeration%20model%20(k%20%3D%203)%0A%20%20%20%20x_tr_fa%20%3D%20fa_3.transform(x_tr)%0A%20%20%20%20x_te_fa%20%3D%20fa_3.transform(x_te)%0A%20%20%20%20m_fa%20%3D%20LinearRegression().fit(x_tr_fa%2C%20y_tr)%0A%20%20%20%20r2_fa%20%3D%20r2_score(y_te%2C%20m_fa.predict(x_te_fa))%0A%20%20%20%20cond_fa%20%3D%20np.linalg.cond(x_tr_fa.T%20%40%20x_tr_fa)%0A%0A%20%20%20%20df_benchmark%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Feature_Representation%22%3A%20%22All%20Original%20Features%20(p%20%3D%209)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Number_of_Inputs%22%3A%209%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Condition_Number%22%3A%20f%22%7Bcond_full%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_R2%22%3A%20f%22%7Br2_full%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Interpretability%22%3A%20%22Dense%2C%20Collinear%20Features%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hardware_Sensor_Pruning%22%3A%20%22None%20(All%209%20required)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Feature_Representation%22%3A%20%22PCA%20Projections%20(k%20%3D%203)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Number_of_Inputs%22%3A%203%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Condition_Number%22%3A%20f%22%7Bcond_pca%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_R2%22%3A%20f%22%7Br2_pca%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Interpretability%22%3A%20%22Low%20(Dense%20mixtures%20of%209%20features)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hardware_Sensor_Pruning%22%3A%20%22None%20(All%209%20needed%20for%20dot-product)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Feature_Representation%22%3A%20%22Feature%20Agglomeration%20(k%20%3D%203)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Number_of_Inputs%22%3A%203%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Condition_Number%22%3A%20f%22%7Bcond_fa%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_R2%22%3A%20f%22%7Br2_fa%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Interpretability%22%3A%20%22High%20(Block%20cluster%20averages)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hardware_Sensor_Pruning%22%3A%20%22High%20(Redundant%20sensors%20retire)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20table_loadings%20%3D%20mo.ui.table(pca_loadings)%0A%20%20%20%20table_clusters%20%3D%20mo.ui.table(fa_clusters)%0A%20%20%20%20table_bench%20%3D%20mo.ui.table(df_benchmark)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%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
24ba7ae4a1588368f858b6e83ad6271e