Machine Learning Series: Regularization


Hello Reader,

 

In this post, I want to describe the concept of regularization and how to apply it in your ML experiments. Let's get started

 

To have a robust model it should be able to generalize and provide a good prediction on instances not used during the training phase

 

A low-quality prediction can be caused by one of the following cause:

  • Bias: the model is too simple for the problem we are trying to solve e.g. using a linear model when the data has polynomial distribution. A model with high bias is under-fitting its training data and results in low scores on both training and test sets.
  • Variance: the model is too sensitive to training data variations. A model affected by high variance overfits the training data. It has a high score on the training and a low score on the test data
  • Low-Quality Data: the data contains errors that do not allow the model to learn the correct patterns. 

 

How to solve these issues? In case of model bias, we can change the algorithm to a more complex one or increase the number of features. For low-quality data, the only solution is to fix the data or ask the data source to fix it. Finally, for high variance, you can get more training instances or add regularization.

 

The regularization is going to constraint the model reducing its degrees of freedom and its overfitting to the training set increasing the model generalization on new instances.

 

Here the main regularizations we can use to constraint the weights of linear models:

 

Ridge: also know as Tikhonov regularization, the regularization will add the following term to the cost function


The term this term is also known as 1/2 (|| θ ||2 )2  where || θ ||2 represent the Euclidian distance identified by l2 norm. The hyper-parameter alpha is used to define the amount of regularization. The bias term  θ0 is not regularized.

 

Lasso: also known as Least Absolute Shrinkage and Selection Operator Regression, it is similar to the Ridge regression but in this case it adds the || θ ||1 known as Manhattan distance identified by l1 norm. below the term added to the cost function. 


An important characteristic of Lasso is that it tends to eliminate features that are not relevant.

 

Elastic Net: it sits in between ridge and lasso regularization it uses both terms, the hyperparameter r is used to control the mix ratio. Below the term added to the cost function


Early Stopping: in this case, there is no term added to the cost function instead the training process is interrupted when the validation error has reached its minimum and starts to increase (which means that the model starts overfitting).

 

Let's see the regularization in action. I am going to generate a synthetic dataset to simulate a classification problem.




Let's train a Logistic Regression model with no regularization



Let's score the predictions on the train and test datasets, using the AUC metrics (I am going to write about the ML metrics in a future post)



As you can see the score drops on the test set, this behavior shows that the model is overfitting the training sample and is not able to generalize to new instances never seen before. Let's have a look at the decision boundary plot on top of the test set



The decision boundary follows the shape of the training set. Notice the cycle in the blue area, it was packed with orange points in the training set, but the boundary does not fit the test set. The decision boundary is the line where the model predicts 0.5 probability on both classes.

 

Now let's train the model by adding l2 regularization


Let's see how it scores now




The gap between train and test has been reduced, let's see the decision boundary now



Now the decision boundary is smooth and it not overfitting the training set.



Recommended resources 

Notebook:

Course:  

Books:

Comments