Implementing elastic net regression
Elastic net regression is a type of regression that combines lasso regression with ridge regression by adding L1 and L2 regularization terms to the loss function.
Getting ready
Implementing elastic net regression should be straightforward after the previous two recipes, so we will implement this in multiple linear regression on the iris dataset, instead of sticking to the two-dimensional data as before. We will use petal length, petal width, and sepal width to predict sepal length.
How to do it...
We proceed with the recipe as follows:
- First, we load the necessary libraries and initialize a graph, as follows:
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from sklearn import datasets sess = tf.Session()
- Now, we load the data. This time, each element of
x
data will be a list of three values instead of one. Use the following code:
iris = datasets.load_iris() x_vals = np.array([[x[1], x[2], x[3]] for x in iris.data]) y_vals = np...