gpt4 book ai didi

python - ValueError : Solver lbfgs supports only 'l2' or 'none' penalties, 得到了 l1 惩罚

转载 作者:行者123 更新时间:2023-12-03 14:34:00 28 4
gpt4 key购买 nike

我正在使用带有 LogisticRegression 的嵌入式方法(L1 - Lasso)运行分类问题的特征选择过程。

我正在运行以下代码:

from sklearn.linear_model import Lasso, LogisticRegression
from sklearn.feature_selection import SelectFromModel

# using logistic regression with penalty l1.
selection = SelectFromModel(LogisticRegression(C=1, penalty='l1'))
selection.fit(x_train, y_train)

但是我遇到了异常(在 fit 命令上):
   selection.fit(x_train, y_train)
File "C:\Python37\lib\site-packages\sklearn\feature_selection\_from_model.py", line 222, in fit
self.estimator_.fit(X, y, **fit_params)
File "C:\Python37\lib\site-packages\sklearn\linear_model\_logistic.py", line 1488, in fit
solver = _check_solver(self.solver, self.penalty, self.dual)
File "C:\Python37\lib\site-packages\sklearn\linear_model\_logistic.py", line 445, in _check_solver
"got %s penalty." % (solver, penalty))
ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.

我正在运行 python 3.7.6和 s scikit-learn version is 0.22.2.post1
出了什么问题,我该如何解决?

最佳答案

这在 documentation 中得到了澄清。 .

solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’}, default=’lbfgs’

...

  • ‘newton-cg’, ‘lbfgs’, ‘sag’ and ‘saga’ handle L2 or no penalty

  • ‘liblinear’ and ‘saga’ also handle L1 penalty


像这样调用它:
LogisticRegression(C=1, penalty='l1', solver='liblinear')

关于python - ValueError : Solver lbfgs supports only 'l2' or 'none' penalties, 得到了 l1 惩罚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60868629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com