Book Image

Applied Supervised Learning with R

By : Karthik Ramasubramanian, Jojo Moolayil
Book Image

Applied Supervised Learning with R

By: Karthik Ramasubramanian, Jojo Moolayil

Overview of this book

R provides excellent visualization features that are essential for exploring data before using it in automated learning. Applied Supervised Learning with R helps you cover the complete process of employing R to develop applications using supervised machine learning algorithms for your business needs. The book starts by helping you develop your analytical thinking to create a problem statement using business inputs and domain research. You will then learn different evaluation metrics that compare various algorithms, and later progress to using these metrics to select the best algorithm for your problem. After finalizing the algorithm you want to use, you will study the hyperparameter optimization technique to fine-tune your set of optimal parameters. The book demonstrates how you can add different regularization terms to avoid overfitting your model. By the end of this book, you will have gained the advanced skills you need for modeling a supervised machine learning algorithm that precisely fulfills your business needs.
Table of Contents (12 chapters)
Applied Supervised Learning with R
Preface

LASSO Regression


Least Absolute Shrinkage and Selection Operator (LASSO) follows a similar structure to that of ridge regression, except for the penalty term, which in LASSO regression is L1 (sum of absolute values of the coefficient estimates) in contrast to ridge regression where it's L2 (sum of squared coefficients):

LASSO regression turns some coefficients to zero, thus the effect of a particular variable is nullified. This makes it efficient in feature selection while fitting data.

Exercise 57: LASSO Regression

In this exercise, we will apply LASSO regression on the Beijing PM2.5 dataset. We will use the same cv.glmnet() function to find the optimal lambda value.

Perform the following steps to complete the exercise:

  1. First, let's set up seed to get similar results using the following command:

    set.seed(100) #Setting the seed to get similar results.
    model_LASSO = cv.glmnet(X,Y,alpha = 1,lambda = 10^seq(4,-1,-0.1))
  2. Now, use the following command to find the optimal value of lambda after cross...