ridgelassoand elastic nets these penalized models often go by rather interesting names the penalized model is often called the lassowhile an penalized model is known as ridge regression of coursewe can combine the two and we obtain an elastic net model both the lasso and the ridge result in smaller coefficients than unpenalized regression howeverthe lasso has the additional property that it results in more coefficients being set to zerothis means that the final model does not even use some of its input featuresthe model is sparse this is often very desirable property as the model performs both feature selection and regression in single step you will notice that whenever we add penaltywe also add weight lwhich governs how much penalization we want when is close to zerowe are very close to ols (in factif you set to zeroyou are just performing ols)and when is largewe have model which is very different from the ols one the ridge model is older as the lasso is hard to compute manually howeverwith modern computerswe can use the lasso as easily as ridgeor even combine them to form elastic nets an elastic net has two penaltiesone for the absolute value and another for the squares using lasso or elastic nets in scikit-learn let us adapt the preceding example to use elastic nets using scikit-learnit is very easy to swap in the elastic net regressor for the least squares one that we had beforefrom sklearn linear_model import elasticnet en elasticnet(fit_intercept=truealpha= now we use en whereas before we had used lr this is the only change that is needed the results are exactly what we would have expected the training error increases to (which was before)but the cross-validation error decreases to (which was beforewe have larger error on the training databut we gain better generalization we could have tried an penalty using the lasso class or using the ridge class with the same code the next plot shows what happens when we switch from unpenalized regression (shown as dotted lineto lasso regressionwhich is closer to flat line the benefits of lasso regression arehowevermore apparent when we have many input variables and we consider this setting next