主要问题:为什么 Lasso 回归的系数不会通过 最小化而缩小到零scipy.minimize?
我正在尝试使用 scipy.minimize 创建套索模型。然而,它仅在 alpha 为零时才起作用(因此仅像基本平方误差一样)。当 alpha 不为零时,它会返回更差的结果(更高的损失),并且仍然没有一个系数为零。
我知道 Lasso 是不可微分的,但我尝试使用 Powell 优化器,它应该处理非微分损失(我也尝试过 BFGS,它应该处理非平滑)。这些优化器都不起作用。
为了测试这一点,我创建了数据集,其中 y 是随机的(此处提供是可重现的),X 的第一个特征恰好是 y*.5,其他四个特征是随机的(此处也提供是可重现的)。我希望算法将这些随机系数缩小到零并只保留第一个系数,但它没有发生。
对于套索损失函数,我使用本文中的公式(图 1,第一页)
我的代码如下:
from scipy.optimize import minimize
import numpy as np
class Lasso:
def _pred(self,X,w):
return np.dot(X,w)
def LossLasso(self,weights,X,y,alpha):
w = weights
yp = self._pred(X,w)
loss = np.linalg.norm(y - yp)**2 + alpha * np.sum(abs(w))
return loss
def fit(self,X,y,alpha=0.0):
initw = np.random.rand(X.shape[1]) #initial weights
res = minimize(self.LossLasso,
initw,
args=(X,y,alpha),
method='Powell')
return res
if __name__=='__main__': …Run Code Online (Sandbox Code Playgroud) machine-learning lasso-regression scipy data-science loss-function