Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement lasso and Ridge regression in Python

Shulou Source: shulou.com Published: 2022-06-01 09:25:22 09月29日 Update

How to implement lasso and ridge regression in Python, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Import library

As with any project, import commonly used libraries, which will help you perform basic data manipulation and drawing.

Now you can start exploratory data analysis.

Exploratory data analysis

First import the dataset and view the first five rows:

You should see:

Note that the Unnamed:0 column is useless. Take it out.

The dataset now looks like this:

As you can see, there are only three advertising media, and sales is the target variable.

See how each variable affects sales by creating a scatter chart. First, build an auxiliary function to create a scatter chart:

You can now generate three different diagrams for each function.

Get the following:

About the sale of money on TV commercials

About the sales of money spent on radio advertisements

About the sales of newspaper advertising

As you can see, television and radio advertisements seem to be good predictors of sales, while there seems to be no correlation between sales and newspaper advertisements.

Fortunately, the dataset does not require further processing, so you are ready to model immediately!

Model

Multiple linear regression-least square fitting

Before you finish, let's take a look at what the code looks like.

First, import the LinearRegression and cross_val_score objects. The first will allow the linear model to be fitted, while the second object will perform k times cross validation.

Then define features and target variables.

The cross_val_score will return MSE's array for each cross-validation step. There are five in the example. Therefore, take the meaning of MSE and print it out. You should get a negative MSE of-3.0729.

Now, let's see if it would be better to return to the Ridge or the lasso.

Ridge regression

For ridge regression, GridSearchCV is introduced. This will allow automatic cross-validation of five times using a series of different regularization parameters to find the best value for alpha.

The code is as follows:

You can then find the best parameters and the best MSE in the following ways:

You should see that the optimal value for alpha is 20 and the negative MSE is-3.07267. This is a slight improvement on the basic multivariate linear regression.

Lasso

For lasso, follow a process that is very similar to Ridge regression:

In this case, the best value for alpha is 1, and the negative MSE is-3.0414, which is the best score for all three models!

Now know how to use lasso and Ridge regression in Python. In this case, it has been seen that the lasso is the most appropriate method, with a regularization value of 1.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Sales lassos advertisements data variables models linearity crossover validation differences three code content parameters objects situations newspapers newspaper advertisements exploratory regular Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Huawei MariaDB Redmi