A Gentle Introduction to Elastic Net in Machine Learning

Spread the love

Elastic Net in Machine Learning –

Elastic Net is a middle ground between Ridge Regression and Lasso regression. The regularization term is a simple mix of both Ridge and Lasso’s regularization terms, and you can control the mix ratio r.

When r=0, Elastic Net is equivalent to Ridge Regression and when r=1, it is equivalent to Lasso Regression

How to train a Elastic Net model in sklearn ?

let’s read a dataset to work with.

import pandas as pd
import numpy as np
from sklearn import datasets

housing = datasets.fetch_california_housing()
X = pd.DataFrame(housing.data, columns=housing.feature_names)
y = housing.target
X.head()

Next split the data into training and test set.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Now train an Elastic Net model in scikit-Learn.

from sklearn.linear_model import ElasticNet
from sklearn.metrics import mean_squared_error

elastic_net = ElasticNet(alpha=0.1, l1_ratio=0.5)
elastic_net.fit(X_train, y_train)
y_pred = elastic_net.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
rmse
# output
0.7570333016471739

Related posts –

  1. Introduction to Linear Regression in Machine Learning
  2. Introduction to Ridge Regression in Machine Learning
  3. A Gentle Introduction to Lasso Regression in Machine Learning

Rating: 1 out of 5.

Leave a Reply