简体   繁体   English

Python - 分类套索 sklearn - 如何预测类

[英]Python - Classification Lasso sklearn - How to predict classes

Following the example: http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html以下示例: 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.换句话说,当我给它一个输入时,我想要一个分类的输出。

Use LogisticRegression with penalty='l1' .LogisticRegressionpenalty='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 .正则化强度由C定义,它是Lasso使用的alpha的 INVERSE。
Scikit-learn has a very nice brief overview of linear models: Scikit-learn 对线性模型有一个非常好的简要概述:
https://scikit-learn.org/stable/modules/linear_model.html https://scikit-learn.org/stable/modules/linear_model.html

您可以使用 Lasso 并以降序方式对预测结果进行排序,因此前 50% 将为 1,最后为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM