Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Classification Lasso sklearn - How to predict classes

Following the example: http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html

from sklearn import linear_model
clf = linear_model.Lasso(alpha=0.1)
clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])

clf.predict(np.array([0,0]).reshape(1,-1))
Out[13]: array([ 0.15])

Can I get the prediction to be a classification instead of a regression. In other words when I give it an input, I would like an output that is categorical.

like image 813
Kevin Avatar asked Apr 18 '16 14:04

Kevin


People also ask

What is predict () sklearn?

The Sklearn 'Predict' Method Predicts an Output That being the case, it provides a set of tools for doing things like training and evaluating machine learning models. What is this? And it also has tools to predict an output value, once the model is trained (for ML techniques that actually make predictions).

Can you use Lasso regression for classification?

You can use the Lasso or elastic net regularization for generalized linear model regression which can be used for classification problems.

How do you evaluate a LASSO model?

Evaluation of the lasso model can be done using metrics like RMSE and R-Square. Alpha is a hyper-parameter in the lasso model which can be tuned using lassoCV to control the regularization.

How do you evaluate Lasso regression in Python?

In Python, Lasso regression can be performed using the Lasso class from the sklearn. linear_model library. The Lasso class takes in a parameter called alpha which represents the strength of the regularization term. A higher alpha value results in a stronger penalty, and therefore fewer features being used in the model.


1 Answers

Use LogisticRegression with penalty='l1'.
It is, essentially, the Lasso regression, but with the additional layer of converting the scores for classes to the "winning" class output label.
Regularization strength is defined by C, which is the INVERSE of alpha, used by Lasso.
Scikit-learn has a very nice brief overview of linear models:
https://scikit-learn.org/stable/modules/linear_model.html

like image 187
Gene M Avatar answered Oct 06 '22 05:10

Gene M