In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.