帮我写一个只用numpy实现Lasso 回归模型,要求使用梯度下降法实现
时间: 2024-06-02 19:13:55 浏览: 95
import numpy as np
class LassoRegression:
def __init__(self, alpha=1, learning_rate=0.01, max_iter=1000, tol=1e-4):
self.alpha = alpha
self.learning_rate = learning_rate
self.max_iter = max_iter
self.tol = tol
def fit(self, X, y):
self.weights = np.zeros(X.shape[1])
for i in range(self.max_iter):
old_weights = self.weights.copy()
gradient = self._compute_gradient(X, y)
self.weights -= self.learning_rate * gradient
self.weights = self._proximal_operator(self.weights, self.alpha * self.learning_rate)
if np.linalg.norm(self.weights - old_weights) < self.tol:
break
def predict(self, X):
return np.dot(X, self.weights)
def _compute_gradient(self, X, y):
y_pred = self.predict(X)
residuals = y_pred - y
gradient = np.dot(X.T, residuals) / len(y)
return gradient
def _proximal_operator(self, w, threshold):
return np.sign(w) * np.maximum(np.abs(w) - threshold, 0)
阅读全文