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.linear_model%20import%20ElasticNet%2C%20Lasso%2C%20LinearRegression%2C%20Ridge%0A%20%20%20%20from%20sklearn.metrics%20import%20mean_squared_error%2C%20r2_score%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20ElasticNet%2C%0A%20%20%20%20%20%20%20%20Lasso%2C%0A%20%20%20%20%20%20%20%20LinearRegression%2C%0A%20%20%20%20%20%20%20%20Ridge%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%20mean_squared_error%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(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Note%2032%3A%20Elastic%20Net%20Regularization%2C%20Grouping%20Effects%2C%20and%20Convex%20Penalty%20Geometry%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B31%20Gaussian%20Mixture%20Models%5D(31_gaussian_mixture_models.py)%20%7C%20Next%20Note%3A%20%5B33%20Huber%20Loss%5D(33_huber_loss.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%20Ordinary%20Least%20Squares%20(OLS)%20regression%20fails%20in%20high-dimensional%20or%20multicollinear%20regimes%20because%20the%20matrix%20%24%5Cmathbf%7BX%7D%5E%5Ctop%20%5Cmathbf%7BX%7D%24%20becomes%20ill-conditioned%20or%20singular.%20While%20Ridge%20(%24L_2%24)%20and%20Lasso%20(%24L_1%24)%20provide%20classical%20regularized%20alternatives%2C%20each%20carries%20significant%20individual%20limitations%3A%0A%20%20%20%201.%20**The%20Limitations%20of%20Pure%20Lasso%20(%24L_1%24)**%3A%0A%20%20%20%20%20%20%20-%20**Arbitrary%20Selection%20on%20Collinear%20Features**%3A%20When%20two%20or%20more%20features%20are%20strongly%20correlated%20(%24r%20%5Capprox%200.99%24)%2C%20Lasso%20arbitrarily%20selects%20one%20feature%20and%20zeroes%20out%20the%20others%2C%20leading%20to%20erratic%20feature%20attribution%20that%20changes%20with%20small%20data%20resamplings.%0A%20%20%20%20%20%20%20-%20**Dimensionality%20Saturation**%3A%20If%20the%20number%20of%20features%20exceeds%20observations%20(%24p%20%3E%20n%24)%2C%20Lasso%20can%20select%20at%20most%20%24n%24%20non-zero%20predictors%20before%20saturating.%0A%20%20%20%202.%20**The%20Limitations%20of%20Pure%20Ridge%20(%24L_2%24)**%3A%0A%20%20%20%20%20%20%20-%20Ridge%20shrinks%20correlated%20coefficients%20together%20smoothly%2C%20but%20because%20the%20%24L_2%24%20penalty%20is%20strictly%20differentiable%20everywhere%2C%20it%20cannot%20set%20any%20coefficient%20to%20exactly%20zero%2C%20failing%20to%20produce%20sparse%2C%20interpretable%20models.%0A%20%20%20%203.%20**Zou%20%26%20Hastie's%20Elastic%20Net%20Synthesis%20(2005)**%3A%0A%20%20%20%20%20%20%20Elastic%20Net%20combines%20the%20%24L_1%24%20and%20%24L_2%24%20penalties%20into%20a%20convex%20compromise%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathcal%7BL%7D(%5Cboldsymbol%7B%5Cbeta%7D)%20%3D%20%5Cfrac%7B1%7D%7B2n%7D%5C%7C%5Cmathbf%7By%7D%20-%20%5Cmathbf%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%20%2B%20%5Calpha%20%5Cleft%5B%5Crho%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_1%20%2B%20%5Cfrac%7B1%20-%20%5Crho%7D%7B2%7D%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20where%20%24%5Crho%20%5Cin%20%5B0%2C%201%5D%24%20is%20the%20%60l1_ratio%60.%0A%20%20%20%204.%20**The%20Grouping%20Property**%3A%20The%20strict%20convexity%20of%20the%20%24L_2%24%20penalty%20ensures%20that%20strongly%20correlated%20features%20enter%20or%20leave%20the%20model%20together%20with%20similar%20coefficients%2C%20overcoming%20Lasso's%20arbitrary%20selection%20instability%20while%20maintaining%20exact%20sparsity.%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.%20Mathematical%20Formulation%0A%0A%20%20%20%20Given%20standardized%20design%20matrix%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20p%7D%24%20and%20response%20vector%20%24%5Cmathbf%7By%7D%20%5Cin%20%5Cmathbb%7BR%7D%5En%24%2C%20the%20Elastic%20Net%20optimization%20problem%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmin_%7B%5Cboldsymbol%7B%5Cbeta%7D%7D%20%5Cfrac%7B1%7D%7B2n%7D%20%5C%7C%5Cmathbf%7By%7D%20-%20%5Cmathbf%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%20%2B%20%5Clambda_1%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_1%20%2B%20%5Cfrac%7B%5Clambda_2%7D%7B2%7D%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20Scikit-Learn%20parameterization%2C%20letting%20%24%5Calpha%20%3D%20%5Clambda_1%20%2B%20%5Clambda_2%24%20and%20%24%5Crho%20%3D%20%5Cfrac%7B%5Clambda_1%7D%7B%5Clambda_1%20%2B%20%5Clambda_2%7D%20%5Cin%20%5B0%2C%201%5D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmin_%7B%5Cboldsymbol%7B%5Cbeta%7D%7D%20%5Cfrac%7B1%7D%7B2n%7D%20%5C%7C%5Cmathbf%7By%7D%20-%20%5Cmathbf%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%20%2B%20%5Calpha%20%5Crho%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_1%20%2B%20%5Cfrac%7B%5Calpha(1%20-%20%5Crho)%7D%7B2%7D%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20-%20%24%5Crho%20%3D%201%24%3A%20Reduces%20identically%20to%20Lasso%20(%24L_1%24).%0A%20%20%20%20-%20%24%5Crho%20%3D%200%24%3A%20Reduces%20identically%20to%20Ridge%20(%24L_2%24).%0A%20%20%20%20-%20%240%20%3C%20%5Crho%20%3C%201%24%3A%20Elastic%20Net%20with%20balanced%20sparsity%20and%20grouping.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20Coordinate%20Descent%20and%20the%20Soft-Thresholding%20Operator%0A%0A%20%20%20%20Elastic%20Net%20is%20optimized%20via%20cyclical%20coordinate%20descent.%20For%20each%20coordinate%20%24j%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20p%5C%7D%24%2C%20keeping%20all%20other%20coefficients%20%24%5Cboldsymbol%7B%5Cbeta%7D_%7B%5Csetminus%20j%7D%24%20fixed%2C%20we%20compute%20the%20partial%20residual%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Br%7D_%7B-j%7D%20%3D%20%5Cmathbf%7By%7D%20-%20%5Csum_%7Bk%20%5Cneq%20j%7D%20%5Cmathbf%7Bx%7D_k%20%5Cbeta_k%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20univariate%20subgradient%20optimality%20condition%20yields%20a%20closed-form%20two-stage%20update%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbeta_j%5E%7B(t%2B1)%7D%20%3D%20%5Cfrac%7B%5Cmathcal%7BS%7D%5Cleft(%5Cfrac%7B1%7D%7Bn%7D%20%5Cmathbf%7Bx%7D_j%5E%5Ctop%20%5Cmathbf%7Br%7D_%7B-j%7D%2C%20%5C%2C%20%5Calpha%20%5Crho%5Cright)%7D%7B1%20%2B%20%5Calpha%20(1%20-%20%5Crho)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24%5Cmathcal%7BS%7D(z%2C%20%5Cgamma)%24%20is%20the%20**Soft-Thresholding%20Operator**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathcal%7BS%7D(z%2C%20%5Cgamma)%20%3D%20%5Coperatorname%7Bsign%7D(z)%20%5Cmax(0%2C%20%5C%2C%20%7Cz%7C%20-%20%5Cgamma)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Two-Stage%20Mechanism%3A%0A%20%20%20%201.%20**Sparsity%20via%20Soft-Thresholding**%3A%20The%20numerator%20sets%20%24%5Cbeta_j%20%3D%200%24%20if%20the%20correlation%20with%20the%20residual%20%24%7Cz%7C%20%5Cleq%20%5Calpha%20%5Crho%24.%0A%20%20%20%202.%20**Grouping%20via%20Ridge%20Shrinkage**%3A%20The%20denominator%20divides%20by%20%241%20%2B%20%5Calpha(1%20-%20%5Crho)%20%3E%201%24%2C%20scaling%20down%20non-zero%20coefficients%20to%20prevent%20variance%20inflation%20under%20collinearity.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20The%20Grouping%20Effect%20Theorem%0A%0A%20%20%20%20Zou%20%26%20Hastie%20(2005)%20proved%20a%20fundamental%20theorem%20governing%20correlated%20features%20in%20Elastic%20Net%3A%0A%0A%20%20%20%20%3E%20**Theorem%20(Grouping%20Bound)**%3A%20Assume%20%24%5Cmathbf%7Bx%7D_i%24%20and%20%24%5Cmathbf%7Bx%7D_j%24%20are%20standardized%20(%24%5C%7C%5Cmathbf%7Bx%7D_i%5C%7C_2%5E2%20%3D%20%5C%7C%5Cmathbf%7Bx%7D_j%5C%7C_2%5E2%20%3D%20n%24)%2C%20with%20sample%20correlation%20%24r%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Cmathbf%7Bx%7D_i%5E%5Ctop%20%5Cmathbf%7Bx%7D_j%24.%20If%20%24%5Chat%7B%5Cbeta%7D_i%20%5Ccdot%20%5Chat%7B%5Cbeta%7D_j%20%3E%200%24%2C%20then%3A%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20%24%24%0A%20%20%20%20%3E%20%7C%5Chat%7B%5Cbeta%7D_i%20-%20%5Chat%7B%5Cbeta%7D_j%7C%20%5Cleq%20%5Cfrac%7B%5C%7C%5Cmathbf%7By%7D%5C%7C_2%7D%7B%5Clambda_2%7D%20%5Csqrt%7B2(1%20-%20r)%7D%0A%20%20%20%20%3E%20%24%24%0A%0A%20%20%20%20As%20the%20correlation%20between%20features%20approaches%20unity%20(%24r%20%5Cto%201%24)%2C%20%24%5Csqrt%7B2(1%20-%20r)%7D%20%5Cto%200%24%2C%20forcing%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clim_%7Br%20%5Cto%201%7D%20%7C%5Chat%7B%5Cbeta%7D_i%20-%20%5Chat%7B%5Cbeta%7D_j%7C%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Under%20pure%20Lasso%20(%24%5Clambda_2%20%3D%200%24)%2C%20the%20bound%20denominator%20is%20zero%2C%20allowing%20%24%7C%5Chat%7B%5Cbeta%7D_i%20-%20%5Chat%7B%5Cbeta%7D_j%7C%24%20to%20remain%20arbitrarily%20large.%20Elastic%20Net%20guarantees%20that%20collinear%20features%20receive%20virtually%20identical%20regression%20coefficients.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20Constraint%20Geometry%20Comparison%0A%0A%20%20%20%20In%20the%20Lagrangian%20dual%20formulation%2C%20minimizing%20the%20squared%20loss%20subject%20to%20a%20norm%20constraint%20%24%5COmega(%5Cboldsymbol%7B%5Cbeta%7D)%20%5Cleq%20C%24%3A%0A%0A%20%20%20%20%7C%20Penalty%20Type%20%7C%20Unit%20Ball%20Geometry%20%24%5COmega(%5Cboldsymbol%7B%5Cbeta%7D)%20%5Cleq%201%24%20%7C%20Singularities%20(Corners%20on%20Axes)%3F%20%7C%20Curvature%20along%20Edges%3F%20%7C%20Resulting%20Behavior%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20**Ridge%20(%24L_2%24)**%20%7C%20Circle%20%2F%20Sphere%20%7C%20No%20(smooth%20everywhere)%20%7C%20Strictly%20convex%20%7C%20Grouping%2C%20no%20feature%20selection%20%7C%0A%20%20%20%20%7C%20**Lasso%20(%24L_1%24)**%20%7C%20Cross-Polytope%20%2F%20Diamond%20%7C%20Yes%20(sharp%20vertices%20on%20axes)%20%7C%20None%20(flat%20hyperplanes)%20%7C%20Sparse%20selection%2C%20erratic%20under%20collinearity%20%7C%0A%20%20%20%20%7C%20**Elastic%20Net**%20%7C%20Curved%20Diamond%20%7C%20Yes%20(sharp%20vertices%20on%20axes)%20%7C%20Strictly%20convex%20edges%20%7C%20Both%20sparsity%20and%20robust%20grouping%20%7C%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%20Data%20Generation%3A%20Controlled%20Multicollinearity%20Experiment%0A%20%20%20%20%23%20500%20samples%2C%206%20features%3A%0A%20%20%20%20%23%20x1%2C%20x2%3A%20Strongly%20collinear%20pair%20(r%20~%200.95)%0A%20%20%20%20%23%20x3%2C%20x4%3A%20Moderately%20collinear%20pair%20(r%20~%200.80)%0A%20%20%20%20%23%20x5%2C%20x6%3A%20Pure%20noise%20variables%20(uncorrelated)%0A%20%20%20%20np.random.seed(47)%0A%20%20%20%20_n%20%3D%20400%0A%0A%20%20%20%20_latent_1%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%20%20%20%20_x1%20%3D%20_latent_1%20%2B%20np.random.normal(0.0%2C%200.20%2C%20_n)%0A%20%20%20%20_x2%20%3D%20_latent_1%20%2B%20np.random.normal(0.0%2C%200.20%2C%20_n)%0A%0A%20%20%20%20_latent_2%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%20%20%20%20_x3%20%3D%20_latent_2%20%2B%20np.random.normal(0.0%2C%200.40%2C%20_n)%0A%20%20%20%20_x4%20%3D%20_latent_2%20%2B%20np.random.normal(0.0%2C%200.40%2C%20_n)%0A%0A%20%20%20%20_x5%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%20%20%20%20_x6%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%0A%20%20%20%20%23%20True%20model%3A%20both%20x1%20and%20x2%20have%20equal%20positive%20causal%20effect%20(beta1%20%3D%20beta2%20%3D%203.0)%0A%20%20%20%20%23%20x3%20has%20moderate%20effect%20(beta3%20%3D%202.0)%2C%20x4%2C%20x5%2C%20x6%20have%20zero%20effect%0A%20%20%20%20_y%20%3D%203.0%20*%20_x1%20%2B%203.0%20*%20_x2%20%2B%202.0%20*%20_x3%20%2B%20np.random.normal(0.0%2C%201.5%2C%20_n)%0A%0A%20%20%20%20df_elastic%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%22x1_collinear_A%22%3A%20_x1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x2_collinear_B%22%3A%20_x2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x3_signal%22%3A%20_x3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x4_correlated_noise%22%3A%20_x4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x5_pure_noise%22%3A%20_x5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x6_pure_noise%22%3A%20_x6%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22y%22%3A%20_y%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20return%20(df_elastic%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ElasticNet%2C%0A%20%20%20%20Lasso%2C%0A%20%20%20%20LinearRegression%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20df_elastic%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20np%2C%0A%20%20%20%20train_test_split%2C%0A)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%20Subplot%201%3A%202D%20Geometry%20of%20Unit%20Balls%20(L1%20vs%20L2%20vs%20Elastic%20Net)%20with%20Tangent%20Loss%20Contours%0A%20%20%20%20%23%20Subplot%202%3A%20Grouping%20Effect%3A%20Lasso%20vs.%20Elastic%20Net%20Coefficient%20Trajectories%20as%20Alpha%20Increases%0A%20%20%20%20%23%20Subplot%203%3A%20Model%20Coefficient%20Profiles%20across%20OLS%2C%20Ridge%2C%20Lasso%2C%20and%20Elastic%20Net%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.%20Constraint%20Balls%3A%20L1%2C%20L2%2C%20and%20Elastic%20Net%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20Grouping%20Effect%3A%20Collinear%20x1%20%26%20x2%20Paths%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Estimated%20Coefficients%20Across%20Regularizers%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%20Subplot%201%3A%20Geometric%20Unit%20Balls%0A%20%20%20%20_theta%20%3D%20np.linspace(0%2C%202%20*%20np.pi%2C%20200)%0A%20%20%20%20_cos_t%20%3D%20np.cos(_theta)%0A%20%20%20%20_sin_t%20%3D%20np.sin(_theta)%0A%0A%20%20%20%20%23%20L2%20Circle%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_cos_t%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_sin_t%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%233b82f6%22%2C%20width%3D2%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22L2%20Ball%20(Ridge)%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%20L1%20Diamond%20(%7Cb1%7C%20%2B%20%7Cb2%7C%20%3D%201)%0A%20%20%20%20_l1_x%20%3D%20%5B1%2C%200%2C%20-1%2C%200%2C%201%5D%0A%20%20%20%20_l1_y%20%3D%20%5B0%2C%201%2C%200%2C%20-1%2C%200%5D%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_l1_x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_l1_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22L1%20Ball%20(Lasso)%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%20Elastic%20Net%20Hybrid%20Ball%3A%200.5%20*%20%7Cb1%7C%20%2B%200.5%20*%20%7Cb2%7C%20%2B%200.25%20*%20(b1%5E2%20%2B%20b2%5E2)%20%3D%20C%0A%20%20%20%20%23%20Parametric%20trace%3A%0A%20%20%20%20_en_pts_x%20%3D%20%5B%5D%0A%20%20%20%20_en_pts_y%20%3D%20%5B%5D%0A%20%20%20%20for%20_th%20in%20_theta%3A%0A%20%20%20%20%20%20%20%20_dx%2C%20_dy%20%3D%20np.cos(_th)%2C%20np.sin(_th)%0A%20%20%20%20%20%20%20%20_l1_norm%20%3D%20abs(_dx)%20%2B%20abs(_dy)%0A%20%20%20%20%20%20%20%20_l2_sq%20%3D%20_dx**2%20%2B%20_dy**2%0A%20%20%20%20%20%20%20%20%23%20Solve%20r%20*%20(0.6%20*%20l1%20%2B%200.4%20*%20r%20*%20l2_sq)%20%3D%201%0A%20%20%20%20%20%20%20%20%23%200.4%20*%20r%5E2%20%2B%200.6%20*%20l1%20*%20r%20-%201%20%3D%200%0A%20%20%20%20%20%20%20%20_a_q%20%3D%200.4%20*%20_l2_sq%0A%20%20%20%20%20%20%20%20_b_q%20%3D%200.6%20*%20_l1_norm%0A%20%20%20%20%20%20%20%20_r_sol%20%3D%20(-_b_q%20%2B%20np.sqrt(_b_q**2%20%2B%204%20*%20_a_q))%20%2F%20(2%20*%20_a_q)%0A%20%20%20%20%20%20%20%20_en_pts_x.append(_r_sol%20*%20_dx)%0A%20%20%20%20%20%20%20%20_en_pts_y.append(_r_sol%20*%20_dy)%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_en_pts_x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_en_pts_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310b981%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Elastic%20Net%20Ball%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%20Collinear%20Coefficient%20Paths%0A%20%20%20%20_X_all%20%3D%20df_elastic.drop(%22y%22%2C%20axis%3D1)%0A%20%20%20%20_y_all%20%3D%20df_elastic%5B%22y%22%5D%0A%0A%20%20%20%20_alphas%20%3D%20np.logspace(-2%2C%201%2C%2040)%0A%20%20%20%20_lasso_b1%20%3D%20%5B%5D%0A%20%20%20%20_lasso_b2%20%3D%20%5B%5D%0A%20%20%20%20_enet_b1%20%3D%20%5B%5D%0A%20%20%20%20_enet_b2%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20_a%20in%20_alphas%3A%0A%20%20%20%20%20%20%20%20_l_mod%20%3D%20Lasso(alpha%3D_a%2C%20random_state%3D42).fit(_X_all%2C%20_y_all)%0A%20%20%20%20%20%20%20%20_lasso_b1.append(_l_mod.coef_%5B0%5D)%0A%20%20%20%20%20%20%20%20_lasso_b2.append(_l_mod.coef_%5B1%5D)%0A%0A%20%20%20%20%20%20%20%20_e_mod%20%3D%20ElasticNet(alpha%3D_a%2C%20l1_ratio%3D0.5%2C%20random_state%3D42).fit(_X_all%2C%20_y_all)%0A%20%20%20%20%20%20%20%20_enet_b1.append(_e_mod.coef_%5B0%5D)%0A%20%20%20%20%20%20%20%20_enet_b2.append(_e_mod.coef_%5B1%5D)%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_alphas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_lasso_b1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Lasso%20x1%20(Erratic%20Split)%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%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_alphas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_lasso_b2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23f87171%22%2C%20width%3D2%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Lasso%20x2%20(Zeroed%20First)%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%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_alphas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_enet_b1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%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%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22ElasticNet%20x1%20(Grouped)%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%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_alphas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_enet_b2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23047857%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22ElasticNet%20x2%20(Grouped)%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%20Coefficient%20Profiles%20Across%204%20Estimators%0A%20%20%20%20_X_tr%2C%20_X_te%2C%20_y_tr%2C%20_y_te%20%3D%20train_test_split(_X_all%2C%20_y_all%2C%20test_size%3D0.25%2C%20random_state%3D42)%0A%0A%20%20%20%20_ols%20%3D%20LinearRegression().fit(_X_tr%2C%20_y_tr)%0A%20%20%20%20_ridge%20%3D%20Ridge(alpha%3D5.0).fit(_X_tr%2C%20_y_tr)%0A%20%20%20%20_lasso%20%3D%20Lasso(alpha%3D0.3%2C%20random_state%3D42).fit(_X_tr%2C%20_y_tr)%0A%20%20%20%20_enet%20%3D%20ElasticNet(alpha%3D0.3%2C%20l1_ratio%3D0.5%2C%20random_state%3D42).fit(_X_tr%2C%20_y_tr)%0A%0A%20%20%20%20_feat_labels%20%3D%20list(_X_all.columns)%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_feat_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ols.coef_%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22OLS%20(Unstable%20Weights)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%2394a3b8%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%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_feat_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ridge.coef_%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Ridge%20(No%20Sparsity)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%233b82f6%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%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_feat_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_lasso.coef_%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Lasso%20(Broken%20Grouping)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%23ef4444%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%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_feat_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_enet.coef_%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22ElasticNet%20(Sparse%20%2B%20Grouped)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%2310b981%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%20barmode%3D%22group%22%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%22Elastic%20Net%3A%20Penalty%20Geometry%2C%20Grouping%20Dynamics%2C%20and%20Coefficient%20Selection%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%22Beta%201%22%2C%20range%3D%5B-1.4%2C%201.4%5D%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Beta%202%22%2C%20range%3D%5B-1.4%2C%201.4%5D%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Alpha%20Penalty%20Parameter%22%2C%20type%3D%22log%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Coefficient%20Value%22%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Predictor%20Feature%22%2C%20tickangle%3D35%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Estimated%20Weight%20(Beta)%22%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**Cyclical%20Coordinate%20Descent%20Implementation%20of%20Elastic%20Net%20from%20Scratch**%3A%20Pure%20NumPy%20implementation%20applying%20the%20soft-thresholding%20operator%20and%20Ridge%20denominator%20update%2C%20verified%20against%20Scikit-Learn.%0A%20%20%20%202.%20**Multi-Model%20Generalization%20Benchmark%20on%20Collinear%20Data**%3A%20Comprehensive%20evaluation%20comparing%20OLS%2C%20Ridge%2C%20Lasso%2C%20and%20Elastic%20Net%20across%20Train%2FTest%20MSE%2C%20%24R%5E2%24%2C%20and%20Collinear%20Feature%20Balance%20(%24%7C%5Chat%7B%5Cbeta%7D_1%20-%20%5Chat%7B%5Cbeta%7D_2%7C%24).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ElasticNet%2C%20df_elastic%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20NumPy%20Coordinate%20Descent%20Implementation%20of%20Elastic%20Net%0A%20%20%20%20_X%20%3D%20df_elastic.drop(%22y%22%2C%20axis%3D1).to_numpy()%0A%20%20%20%20_y%20%3D%20df_elastic%5B%22y%22%5D.to_numpy()%0A%20%20%20%20_n%2C%20_p%20%3D%20_X.shape%0A%0A%20%20%20%20%23%20Standardize%20X%20and%20center%20y%0A%20%20%20%20_X_mean%20%3D%20np.mean(_X%2C%20axis%3D0)%0A%20%20%20%20_X_std%20%3D%20np.std(_X%2C%20axis%3D0%2C%20ddof%3D1)%0A%20%20%20%20_X_norm%20%3D%20(_X%20-%20_X_mean)%20%2F%20_X_std%0A%20%20%20%20_y_mean%20%3D%20np.mean(_y)%0A%20%20%20%20_y_centered%20%3D%20_y%20-%20_y_mean%0A%0A%20%20%20%20_alpha%20%3D%200.2%0A%20%20%20%20_l1_ratio%20%3D%200.6%0A%20%20%20%20_lambda1%20%3D%20_alpha%20*%20_l1_ratio%0A%20%20%20%20_lambda2%20%3D%20_alpha%20*%20(1.0%20-%20_l1_ratio)%0A%0A%20%20%20%20%23%20Soft%20thresholding%20function%0A%20%20%20%20def%20_soft_threshold(z%2C%20gamma)%3A%0A%20%20%20%20%20%20%20%20return%20np.sign(z)%20*%20np.maximum(0.0%2C%20np.abs(z)%20-%20gamma)%0A%0A%20%20%20%20%23%20Coordinate%20Descent%20Loop%0A%20%20%20%20_beta_scratch%20%3D%20np.zeros(_p)%0A%20%20%20%20_max_iter%20%3D%20100%0A%20%20%20%20for%20_iter%20in%20range(_max_iter)%3A%0A%20%20%20%20%20%20%20%20for%20_j%20in%20range(_p)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Compute%20partial%20residual%3A%20r_j%20%3D%20y%20-%20X_%7B-j%7D%20beta_%7B-j%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20_pred_others%20%3D%20_X_norm%20%40%20_beta_scratch%20-%20_X_norm%5B%3A%2C%20_j%5D%20*%20_beta_scratch%5B_j%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_r_j%20%3D%20_y_centered%20-%20_pred_others%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Correlation%20with%20column%20j%0A%20%20%20%20%20%20%20%20%20%20%20%20_rho_j%20%3D%20np.dot(_X_norm%5B%3A%2C%20_j%5D%2C%20_r_j)%20%2F%20_n%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Elastic%20Net%20coordinate%20update%0A%20%20%20%20%20%20%20%20%20%20%20%20_beta_scratch%5B_j%5D%20%3D%20_soft_threshold(_rho_j%2C%20_lambda1)%20%2F%20(1.0%20%2B%20_lambda2)%0A%0A%20%20%20%20%23%20Rescale%20scratch%20beta%20to%20original%20feature%20scale%0A%20%20%20%20_beta_scratch_original%20%3D%20_beta_scratch%20%2F%20_X_std%0A%0A%20%20%20%20%23%20Scikit-Learn%20Reference%0A%20%20%20%20_enet_sk%20%3D%20ElasticNet(alpha%3D_alpha%2C%20l1_ratio%3D_l1_ratio%2C%20fit_intercept%3DTrue%2C%20random_state%3D42)%0A%20%20%20%20_enet_sk.fit(_X%2C%20_y)%0A%0A%20%20%20%20_feature_names%20%3D%20list(df_elastic.drop(%22y%22%2C%20axis%3D1).columns)%0A%20%20%20%20_comparison_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_i%2C%20_name%20in%20enumerate(_feature_names)%3A%0A%20%20%20%20%20%20%20%20_comparison_rows.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Feature%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scratch%20Coordinate%20Descent%22%3A%20f%22%7B_beta_scratch_original%5B_i%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20ElasticNet%22%3A%20f%22%7B_enet_sk.coef_%5B_i%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20f%22%7Bnp.abs(_beta_scratch_original%5B_i%5D%20-%20_enet_sk.coef_%5B_i%5D)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_cd_comparison%20%3D%20pd.DataFrame(_comparison_rows)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ElasticNet%2C%0A%20%20%20%20Lasso%2C%0A%20%20%20%20LinearRegression%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20df_elastic%2C%0A%20%20%20%20mean_squared_error%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20r2_score%2C%0A%20%20%20%20train_test_split%2C%0A)%3A%0A%20%20%20%20%23%20Example%202%3A%20Multi-Model%20Benchmark%20on%20Collinear%20Data%0A%20%20%20%20_X%20%3D%20df_elastic.drop(%22y%22%2C%20axis%3D1)%0A%20%20%20%20_y%20%3D%20df_elastic%5B%22y%22%5D%0A%20%20%20%20_X_tr%2C%20_X_te%2C%20_y_tr%2C%20_y_te%20%3D%20train_test_split(_X%2C%20_y%2C%20test_size%3D0.25%2C%20random_state%3D42)%0A%0A%20%20%20%20_models%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22OLS%20(Unregularized)%22%3A%20LinearRegression()%2C%0A%20%20%20%20%20%20%20%20%22Ridge%20(L2%20%3D%201.0)%22%3A%20Ridge(alpha%3D1.0)%2C%0A%20%20%20%20%20%20%20%20%22Lasso%20(L1%20%3D%200.2)%22%3A%20Lasso(alpha%3D0.2%2C%20random_state%3D42)%2C%0A%20%20%20%20%20%20%20%20%22Elastic%20Net%20(alpha%3D0.2%2C%20rho%3D0.5)%22%3A%20ElasticNet(alpha%3D0.2%2C%20l1_ratio%3D0.5%2C%20random_state%3D42)%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20_benchmark_records%20%3D%20%5B%5D%0A%20%20%20%20for%20_name%2C%20_mod%20in%20_models.items()%3A%0A%20%20%20%20%20%20%20%20_mod.fit(_X_tr%2C%20_y_tr)%0A%20%20%20%20%20%20%20%20_pred_tr%20%3D%20_mod.predict(_X_tr)%0A%20%20%20%20%20%20%20%20_pred_te%20%3D%20_mod.predict(_X_te)%0A%0A%20%20%20%20%20%20%20%20_b1%20%3D%20_mod.coef_%5B0%5D%0A%20%20%20%20%20%20%20%20_b2%20%3D%20_mod.coef_%5B1%5D%0A%20%20%20%20%20%20%20%20_collinear_diff%20%3D%20np.abs(_b1%20-%20_b2)%0A%20%20%20%20%20%20%20%20_zero_coefs%20%3D%20int(np.sum(np.abs(_mod.coef_)%20%3C%201e-4))%0A%0A%20%20%20%20%20%20%20%20_benchmark_records.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Model%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Train%20R%5E2%22%3A%20f%22%7Br2_score(_y_tr%2C%20_pred_tr)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Test%20R%5E2%22%3A%20f%22%7Br2_score(_y_te%2C%20_pred_te)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Test%20MSE%22%3A%20f%22%7Bmean_squared_error(_y_te%2C%20_pred_te)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Collinear%20Imbalance%20%7Cb1%20-%20b2%7C%22%3A%20f%22%7B_collinear_diff%3A.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Zeroed%20Coefficients%22%3A%20f%22%7B_zero_coefs%7D%20%2F%206%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Diagnosis%22%3A%20%22Balanced%20%26%20Sparse%22%20if%20_name.startswith(%22Elastic%22)%20else%20(%22Overfitting%20Collinearity%22%20if%20%22OLS%22%20in%20_name%20else%20(%22No%20Sparsity%22%20if%20%22Ridge%22%20in%20_name%20else%20%22Unstable%20Selection%22))%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_bench%20%3D%20pd.DataFrame(_benchmark_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
a49d9fdc5e72319c29e6f0f9b6154411