用随机梯度下降算法实现Lasso回归python代码
时间: 2023-11-14 16:07:20 浏览: 43
以下是使用随机梯度下降算法实现Lasso回归的Python代码:
```python
import numpy as np
class LassoRegression:
def __init__(self, alpha=0.1, max_iter=1000, tol=1e-4):
self.alpha = alpha
self.max_iter = max_iter
self.tol = tol
def fit(self, X, y):
n_samples, n_features = X.shape
self.w = np.zeros(n_features)
self.b = 0
for i in range(self.max_iter):
rand_idx = np.random.randint(n_samples)
X_i = X[rand_idx, :].reshape(1, -1)
y_i = y[rand_idx].reshape(1, -1)
y_pred = X_i.dot(self.w) + self.b
error = y_i - y_pred
l1_grad = -np.abs(self.w) / self.w
l1_grad[np.isinf(l1_grad)] = 0
w_grad = X_i.T.dot(error) + self.alpha * l1_grad.reshape(-1, 1)
if np.abs(w_grad).all() < self.tol:
break
self.w -= self.alpha * w_grad.reshape(-1)
self.b -= self.alpha * error
def predict(self, X):
return X.dot(self.w) + self.b
```
其中,`alpha` 是正则化参数,`max_iter` 是最大迭代次数,`tol` 是算法收敛的阈值。`fit` 方法使用随机梯度下降算法来拟合模型,`predict` 方法用于预测。