Introduction to Regularization

During the Machine Learning model building, the Regularization Techniques is an unavoidable and important step to improve the model prediction and reduce errors. This is also called the Shrinkage methodWhich we use to add the penalty term to control the complex model to avoid overfitting by reducing the variance.

Let’s discuss the available methods, implementation, and benefits of those in detail here.

The too many parameters/attributes/features along with data and those permutations and combinations of attributes would be sufficient, to capture the possible relationship within dependent and independent variables.

To understand the relationship between the target and available independent variables with several permutations and combinations. For this exercise certainly, we need adequate data in terms of records or datasets for our analysis, hope you are aware of that.

If you have fewer data with huge attributes the depth vs breadth analysis there might lead to that not all possible permutations and combinations among the dependent and independent variables. So those missing values force good or bad to your model. Of course, we can call out this circumstance as Curse of Dimensionality. Here we are looking for these aspects from data along with parameters/attributes/features.

Curse of Dimensionality is not directly mean that too many dimensions, this is the lack of possible permutation and combination.

In another way round the missing data and gap generates empty space, so we couldn’t connect the dots and create the perfect model. It means that the algorithm cannot understand the data and spread across with given space or empty, with multi-dimensional mode and meets with kind of relationship between dependent and independent variables and predicting the future data. If you try to visualize this, it would be really complex format and difficult to follow.

During the training, you will get the above-said observation, but during the testing, the new and not exposed data combination to model’s accuracy will jump across and it suffers from error, because of variance [variance error] and not fit for production move and risk for prediction.

Due to the too many Dimensions with too few data, the algorithm would build the best fit with peaks and deep-down dells in the observation along with the high magnitude of coefficient its leads to overfitting and is not suitable for production. [drastic fluctuation in surface inclination]

To understand or implement these techniques, we should understand the cost function of your linear models.

## Understanding the Regression Graph

The below graph represents the entire parameters existing in the LR model and is very self-explanatory.

## Significance Of Cost Function

Cost function/Error function: Takes slope-intercept (m and c) values and returns the error value/cost value. It shows the error between predicted outcomes is compared with the actual outcomes. It explains how your model is inaccurate in its prediction.

It is used to estimate how badly models are performing for the given dataset and its dimensions.

Why is cost function important in machine learning? Yes, the cost function helps us reach the optimal solution, So how can we do this. will see all possible methods and simple steps using Python libraries.

This function helps us to a figure-out best straight line by minimizing the error

The best fit line is that line where the sum of squared errors around the line is minimized

## Regularization Techniques

Let’s discuss the available Regularization techniques and followed by the implementation

1. Ridge Regression (L2 Regularization):

Basically here, we’re going to minimize the sum of squared errors and sum of the squared coefficients (β). In the background,
the coefficients (β) with a large magnitude will generate the graph peak and
deep slope, to suppress this we’re using the lambda (λ) use to be called a
Penalty Factor and help us to get a smooth surface instead of an irregular-graph. Ridge Regression is used to push the coefficients(β) value nearing zero in terms of magnitude. This is L2 regularization, since its adding a penalty-equivalent to the Square-of-the Magnitude of coefficients.

Ridge Regression = Loss function + Regularized term

#### 2. Lasso Regression (L1 Regularization):

This is very similar to Ridge Regression, with little difference in Penalty Factor that coefficient is magnitude instead of squared. In which there are possibilities of many coefficients becoming zero, so that corresponding attribute/features become zero and dropped from the list, this ultimately reduces the dimensions and supports for dimensionality reduction. So which deciding that those attributes/features are not suitable as predators for predicting target value. This is L1 regularization, because of adding the Absolute-Value as penalty-equivalent to the magnitude of coefficients.

Lasso Regression = Loss function + Regularized term

#### 3. Characteristics of Lambda

 λ = 0 λ => Minimal λ =>High Lambda or Penalty Factor (λ) No impact on coefficients(β) and model would be Overfit. Not suitablefor Production Generalised model and acceptable accuracy and eligible for Test andTrain. Fit for Production Very high impact on coefficients (β) and leading to underfit. Ultimatelynot fit for Production.

Remember one thing that the Ridge never make coefficients into zero, Lasso will do. So, you can use the second one for feature selection.

Impact of Regularization

The below graphical representation clearly indicates the best fitment.

#### 4. Elastic-Net Regression Regularization:

Even though Python provides excellent libraries, we should understand the mathematics behind this. Here is the detailed derivation for your reference.

## Mathematical approach for L1 and L2

Even though Python provides excellent libraries and straightforward coding, we should understand the mathematics behind this. Here is the detailed derivation for your reference.

Let’s have below multi-linear regression dataset and its equation

As we know Multi-Linear-Regression

y=β0+ β1 x1+ β2 x2+………………+ βn xn —————–1

yi= β0+ Σ βi xi —————–2

Σ yi– β0Σ βi xi

Cost/Loss function:  Σ{ yi– β0Σ βi xij}2—————–3

Regularized term: λΣ βi2—————-4

Ridge Regression = Loss function + Regularized term—————–5

Put 3 and 4 in 5

Ridge Regression = Σ { yi β0Σ βi xij}2+ λ Σ βi2

Lasso Regression = Σ { yi β0Σ βi xij}2+ λ Σi|

• x ==> independent variables
• y ==> target variables
• β ==> coefficients
• λ ==> penalty-factor

How coefficients(β) are calculated internally

## Code for Regularization

Let’s take Automobile – Predictive Analysis and apply the L1 and L2 and how it helps model score.

Objective: Predicting the Mileage/Miles Per Gallon (mpg) of a car using given features of the car.

```print("*************************")

print("Import required libraries")

print("*************************")

%matplotlib inline

import numpy as np

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import Ridge

from sklearn.linear_model import Lasso

from sklearn.metrics import r2_score```
```Output
*************************
Import required libraries
*************************print("*************************")
print(" Using auto-mpg dataset ")
print("*************************")
Using auto-mpg dataset
*************************EDA: Will do little EDA (Exploratory Data Analysis), to understand the datasetprint("############################################")
print("          Info Of the Data Set")
print("############################################")
df_cars.info()Observation:1. we could see that the features and its data type, along with Null constraints.2. Horsepower and name features are objects in the given data set. have to take care of during the modelling.Data Cleaning/Wrangling:Is the process of cleaning and consolidating the complex data sets for easy access and analysis.Action:replace(‘?’,’NaN’)Converting “horsepower” Object type into intdf_cars.horsepower = df_cars.horsepower.str.replace('?','NaN').astype(float)
df_cars.horsepower.fillna(df_cars.horsepower.mean(),inplace=True)
df_cars.horsepower = df_cars.horsepower.astype(int)
print("######################################################################")
print("          After Cleaning and type covertion in the Data Set")
print("######################################################################")
df_cars.info()OutputScale all the columns successfully doneTrain and Test Splitfrom sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.25, random_state=1)Observation:1. We could see that the features/column/fields and its data type, along with Null count2. horsepower is now int type and name still as an object type in the given data set, since this column not going to support either way as preditors.# Statistics of the data
display(df_cars.describe().round(2))# Skewness and kurtosis
print("Skewness: %f" %df_cars['mpg'].skew())
print("Kurtosis: %f" %df_cars['mpg'].kurt())Output: Look at the curve and how it is distributed across and see the same.Skewness: 0.457066
Kurtosis: -0.510781sns_plot = sns.distplot(df_cars["mpg"])plt.figure(figsize=(10,6))
sns.heatmap(df_cars.corr(),cmap=plt.cm.Reds,annot=True)
plt.title('Heatmap',
fontsize=13)
plt.show()Output: Look at the heatmapThere is a strong NEGATIVE correlation between mpg and below featuresDisplacementHorsepowerWeightCylindersSo, if those variables increases, the mpg will decrease.Feature Selectionprint("Predictor variables")
X = df_cars.drop('mpg', axis=1)
print(list(X.columns))
print("Dependent variable")
y = df_cars[['mpg']]
print(list(y.columns))Output: Here is the Feature SelectionPredictor variables[‘cylinders’, ‘displacement’, ‘horsepower’, ‘weight’, ‘acceleration’, ‘model_year’, ‘origin_america’, ‘origin_asia’, ‘origin_europe’]
Dependent variable[‘mpg’]Scaling the feature to align the datafrom sklearn import preprocessing
print("Scale all the columns successfully done")
X_scaled = preprocessing.scale(X)
X_scaled = pd.DataFrame(X_scaled, columns=X.columns)
y_scaled = preprocessing.scale(y)
y_scaled = pd.DataFrame(y_scaled, columns=y.columns)OutputScale all the columns successfully doneTrain and Test Splitfrom sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.25, random_state=1)LinearRegression Fit and finding the coefficient.regression_model = LinearRegression()
regression_model.fit(X_train, y_train)
for idcoff, columnname in enumerate(X_train.columns):
print("The coefficient for {} is {}".format(columnname, regression_model.coef_[idcoff]))Output: Try to understand the coefficient (βi)The coefficient for cylinders is -0.08627732236942003
The coefficient for displacement is 0.385244857729236
The coefficient for horsepower is -0.10297215401481062
The coefficient for weight is -0.7987498466220165
The coefficient for acceleration is 0.023089636890550748
The coefficient for model_year is 0.3962256595226441
The coefficient for origin_america is 0.3761300367522465
The coefficient for origin_asia is 0.43102736614202025
The coefficient for origin_europe is 0.4412719522838424intercept = regression_model.intercept_
print("The intercept for our model is {}".format(intercept))
OutputThe intercept for our model is 0.015545728908811594Scores (LR)print(regression_model.score(X_train, y_train))
print(regression_model.score(X_test, y_test))Output0.81408632953522180.843164735865974Now, will apply regularization techniques and review the scores and impact of the techniques on the model.Create a Regularized RIDGE Model and coefficients.ridge = Ridge(alpha=.3)
ridge.fit(X_train,y_train)
print ("Ridge model:", (ridge.coef_))Output: Compare with LR model coefficientRidge model: [[-0.07274955 0.3508473 -0.10462368 -0.78067332 0.01896661 0.394392330.29378926 0.36094062 0.37375046]]Create a Regularized LASSO Model and coefficientslasso = Lasso(alpha=0.1)
lasso.fit(X_train,y_train)
print ("Lasso model:", (lasso.coef_))Output: Compare with LR model coefficient and RIDGE, Here you could see that the few coefficients and zeroed (0) and during the fitment, they are excluded from the feature list.Lasso model: [-0. -0. -0.01262531 -0.6098498 0. 0.29478559-0.03712132 0. 0. ]Scores (RIDGE)print(ridge.score(X_train, y_train))
print(ridge.score(X_test, y_test))Output0.8139778320249321
0.8438110638424217Scores (LASSO)print(lasso.score(X_train, y_train))
print(lasso.score(X_test, y_test))Output0.7866202435701324