Skip to content

Regularization in Machine Learning Narender Kumar Spark By {Examples}

  • by

Regularization is a technique in machine learning that is used to prevent overfitting and improve the generalization performance of a model. Overfitting occurs when a model learns the noise in the training data rather than the underlying pattern or relationship between the input and output variables, resulting in poor performance on new, unseen data.

1. What is Overfitting and Underfitting?

Overfitting and underfitting are two common problems in machine learning that can occur when a model is trained on a dataset.

Overfitting occurs when a model is too complex, and it fits the training data too closely, capturing the noise and random variations in the data rather than the underlying patterns. This can result in poor performance on new, unseen data because the model has memorized the training data instead of learning the general patterns that can be applied to new data. Overfitting can occur when a model has too many parameters relative to the amount of training data, or when the model is trained for too long, resulting in excessively low training error but high test error.

Underfitting, on the other hand, occurs when a model is too simple and cannot capture the underlying patterns in the data, resulting in poor performance on both the training and test data. This can occur when a model has too few parameters or is trained for too few epochs.

To avoid overfitting and underfitting, it is important to choose an appropriate model complexity and regularization.

2. What are Bias and Variance?

Bias and variance are two important concepts in machine learning that refer to the errors that occur when training a model.

Bias refers to the error that is introduced when a model makes assumptions about the relationship between the input variables and the output variable. This error can occur when a model is too simple, or if it has been trained on too little data. A model with high bias will underfit the data, meaning that it will not be able to accurately capture the complexity of the data and will perform poorly on both the training and test datasets.

Variance, on the other hand, refers to the error that is introduced when a model is overly sensitive to the training data and is not able to generalize well to new, unseen data. This error can occur when a model is too complex, or if it has been trained on too much data. A model with high variance will overfit the data, meaning that it will perform well on the training dataset but poorly on the test dataset.

The goal of machine learning is to find a model that has low bias and low variance, which is able to accurately capture the underlying relationship between the input and output variables and generalize well to new data.

3. What is Regularization?

Regularization is a technique in machine learning that is used to prevent overfitting and improve the generalization performance of a model. Overfitting occurs when a model learns the noise in the training data rather than the underlying pattern or relationship between the input and output variables, resulting in poor performance on new, unseen data.

Regularization works by adding a penalty term to the loss function of the model during training, which discourages the model from fitting the training data too closely. The penalty term is typically a function of the weights of the model, and its magnitude is controlled by a hyperparameter called the regularization parameter.

Let’s say we have a linear regression model with two features, x1 and x2, and we want to predict the output y. The model can be represented as:

y = w0 + w1x1 + w2x2

where w0, w1, and w2 are the weights of the model.

To train the model, we minimize the mean squared error loss function:

L = (1/N)*sum((y_pred – y_actual)^2)

where N is the number of samples in the training set, y_pred is the predicted output of the model, and y_actual is the actual output.

However, we notice that the model is overfitting the training data, meaning that it is fitting the noise in the data rather than the underlying pattern, resulting in poor performance on new data.

To prevent overfitting and improve the generalization performance of the model, we can use L2 regularization (also known as Ridge regularization) by adding a penalty term proportional to the square of the weights to the loss function of the model during training. The new loss function is:

L_reg = (1/N)sum((y_pred – y_actual)^2) + alpha(w1^2 + w2^2)

where alpha is the regularisation parameter, which controls the strength of the regularization.

During training, the model will try to minimize this new loss function, which includes the regularisation term. The regularization term will encourage the model to use small weights and prevent large fluctuations, which can help to reduce overfitting.

4. Techniques of Regularization

Regularization is a set of techniques used to prevent overfitting in machine learning models. Overfitting occurs when a model is too complex and learns the training data too well, but performs poorly on new, unseen data. Regularization techniques add a penalty term to the loss function of the model, which encourages the model to choose simpler solutions that generalize better to new data.

Here are some popular regularization techniques:

L1 regularization: also known as Lasso regularization, adds a penalty proportional to the absolute value of the model weights to the loss function. This encourages the model to produce sparse solutions, where some weights are exactly zero.

L2 regularization: also known as Ridge regularization, adds a penalty proportional to the square of the model weights to the loss function. This encourages the model to produce small but non-zero weights.

Elastic Net regularization: combines both L1 and L2 regularization, by adding a penalty term that is a linear combination of the L1 and L2 penalties. This allows the model to learn sparse features while also maintaining some degree of regularization on all features.

Dropout: is a technique that randomly drops out some neurons during training, which forces the model to learn redundant representations of the data. This reduces the risk of overfitting by preventing any one neuron from dominating the model.

Early stopping: involves monitoring the performance of the model on a validation set during training and stopping training when the performance on the validation set stops improving. This prevents the model from continuing to learn the training data too well and overfitting.

Data augmentation: involves creating new training data by applying transformations to the existing data, such as rotating, flipping, or cropping images. This increases the amount of training data available and helps the model to learn more robust features.

These techniques can be used alone or in combination to regularize machine learning models and prevent overfitting.

5. Python Code Example Of the Regularization

Below is an example of implementing L1 regularization using Python and scikit-learn on the California housing dataset

# Import required modules
from sklearn.linear_model import Lasso
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Load the California Housing dataset
X, y = fetch_california_housing(return_X_y=True)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the Lasso regression model with L1 regularization
model = Lasso(alpha=0.1)

# Fit the model to the training data
model.fit(X_train, y_train)

# Evaluate the model on the testing data
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)

print(“MSE: “, mse)

# Output:
MSE: 0.6135115198058131

this example, we load the California Housing dataset using the fetch_california_housing function from scikit-learn. We then split the data into training and testing sets using train_test_split. We initialize a Lasso regression model with an alpha value of 0.1, which controls the strength of the L1 regularization. We fit the model to the training data using the fit method, and evaluate the model on the testing data using predict and mean_squared_error.

6. Conclusion

Regularization is a fundamental technique in machine learning to prevent overfitting of the model on the training data and to improve the generalization performance of the model on unseen data. It works by adding a penalty term to the loss function of the model, which encourages the model to choose simpler solutions that generalize better to new data.

 Regularization is a technique in machine learning that is used to prevent overfitting and improve the generalization performance of a model. Overfitting occurs when a model learns the noise in the training data rather than the underlying pattern or relationship between the input and output variables, resulting in poor performance on new, unseen data. 1.  Read More Machine Learning 

Leave a Reply

Your email address will not be published. Required fields are marked *