The regression algorithm LARS provides variables through a linear combination of high-dimensional data. It has to do with progressive regression. In this approach, the most relevant variable is selected at each step, whose direction is equiangular between the two predictors.

In this tutorial, we’ll learn how to use The LARS and Lasso LARS algorithms in Python to fit regression data. We will estimate the housing data set in this tutorial. This article includes

  1. To prepare data
  2. How to Use LARS
  3. How to use Lasso LARS

Let’s start by loading the packages we need.

from sklearn import linear_model

To prepare data

We’ll load Boston’s data set and split it into training and testing.

Boston = load_boston() xtrain, xtest, ytrain, ytest=train\_test\_split(x, y, test_size=0.15)

How to Use LARS

We will use the Lars() class to define the model (with default parameters) and use training data to fit it.

Lars().fit(xtrain, ytrain)

And check the coefficients of the model.

Print (lars.coef_) \[-1.16800795e-01 1.02016954e-02-2.99472206e-01 4.21380667e+ 00-2.18450214e +01 4.01430635e+00 -9.90351759e-03-1.60916999e + 00-2.32195752e-01 2.80140313e-02-1.08077980e +00 1.07377184e-02-5.02331702e-01 \]

Next, we anticipate the test data and examine the MSE and RMSE metrics.

Mean \_squared\_error(ytest, ypred) print("MSE: %.2f" % MSE) MSE: 36.96 print("RMSE: %.2f" % SQRT (MSE)) RMSE: 6.08

Finally, we will create a plot to visualize the raw and predicted data.

plt.show()

 

How to use Lasso Lars

LassoLars is an implementation of LARS algorithm and Lasso model. We will define the model with the LassoLars() class, set the α parameter to 0.1, and fit the model on the training data.

LassoLars(alpha =.1).fit(xtrain, ytrain)

We can check the coefficients.

Print (coef_) \[0.0.0.0.3.00873485 0.0.0.0.-0.28423008 0.0.-0.42849354 \]

Next, we anticipate the test data and examine the MSE and RMSE metrics.

Predict (xtest) print("MSE: %.2f" % MSE) MSE: 45.59 print("RMSE: %.2f" % SQRT (MSE)) RMSE: 6.75

Finally, we will create a plot to visualize the raw and predicted data.

plt.show()

In this tutorial, we have taken a brief look at how to use LARS and Lasso LARS algorithms to fit and predict regression data.

reference

  1. Least Angle Regression, _by Efron Bradley; Hastie Trevor; Johnstone Iain; Tibshirani Robert (2004)_
  2. Least-Angel Regression, Wikipedia