Lasso Regression #1
Lasso regression analysis is a shrinkage and variable selection method for linear regression models. The goal of lasso regression is to obtain the subset of predictors that minimizes prediction error for a quantitative response variable.Â
The following code will demonstrate the working of Lasso Regression to determine whether a person has diabetes or not using some feature attributes.Â
1. Import all the useful classes and packages and also specify the correct path of the dataset file that is to be used.
2. Use the dropna() function to remove all the null valued rows from the dataset. Then, display the dataset that you are using.
3. Take two variable X and y and store the feature attributes and target attribute respectively. All the attributes other than the target attribute (Outcome in this case) is considered as a feature attribute.
4. Apply the train_test_split function that we imported earlier to divide the dataset into training data and test data. X_train and X_test is the training and test data of the sample and y_train and y_test is the response/outcome train and test data. We will consider test_size as 0.3 i.e. 70% data for training and 30% for testing. Random state is kept to be 123 thus the dataset produces the same kind of distribution everytime the dataset runs.
5. Now, we use the Lasso Regression analysis. We will use LassoLarsCV with 10 cross validations. We keep precompute as False so that the system doesn’t use any pre-computed matrices. Then, fit the model with training data.
6. Now, find the mean squared error of the training data and test data. The values calculated in this case is 0.16 and 0.15 for training and test data respectively. The values of error is very low , thus telling the model is good.
7. Calculate the R2 Score of the model. The R2 score calculated is 0.27 and 0.35 of the training and test data respectively.
8. We also print the regression variables that are used to compute our result.
9. We use matplotlib to plot a graph between regression coefficients or Lasso Paths.
This type of regression nullifies some values to zeroes. It also considers the same kind of datatypes in all the attributes.










