From e7cdb0b24da775dc9f5b80823978583fc2f19430 Mon Sep 17 00:00:00 2001 From: lokeshrathi Date: Mon, 17 Apr 2023 11:46:43 +0530 Subject: [PATCH 1/3] made changes to readme.md where the file name is cd numpy-ml and should be cd numpy_ml. created a logic for lasso.py --- README.md | 2 +- numpy_ml/linear_models/lasso.py | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 numpy_ml/linear_models/lasso.py diff --git a/README.md b/README.md index af31328..1000a63 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ To use this code as a starting point for ML prototyping / experimentation, just ```sh $ git clone https://github.com/ddbourgin/numpy-ml.git -$ cd numpy-ml && virtualenv npml && source npml/bin/activate +$ cd numpy_ml && virtualenv npml && source npml/bin/activate $ pip3 install -r requirements-dev.txt ``` diff --git a/numpy_ml/linear_models/lasso.py b/numpy_ml/linear_models/lasso.py new file mode 100644 index 0000000..57bd1ca --- /dev/null +++ b/numpy_ml/linear_models/lasso.py @@ -0,0 +1,48 @@ +import numpy as np + +class LassoRegression: + + ''' + This implementation defines a LassoRegression class with fit and predict methods. + The fit method takes in the training data X and y, where X is an ndarray of shape (N, M) representing a dataset consisting of N examples, each of dimension M, and y is an ndarray of shape (N,) representing the target values. + If fit_intercept is True, the method adds a column of ones to X to account for the intercept term. The method then iteratively updates the regression coefficients self.beta using the coordinate descent algorithm, where each iteration updates the coefficient for one feature while holding the others fixed. + The soft_threshold method is a helper function that implements the soft thresholding operation, which is used to shrink the regression coefficients towards zero. + The predict method takes in a new dataset X and returns the predicted target values based on the learned regression coefficients self.beta. If fit_intercept is True, the method again adds a column of ones to X to account for the intercept term. + ''' + def __init__(self, alpha=1.0, fit_intercept=True, max_iter=1000, tol=1e-4): + self.alpha = alpha + self.fit_intercept = fit_intercept + self.max_iter = max_iter + self.tol = tol + self.beta = None + + def soft_threshold(self, x, gamma): + if x > 0 and gamma < abs(x): + return x - gamma + elif x < 0 and gamma < abs(x): + return x + gamma + else: + return 0 + + def fit(self, X, y): + if self.fit_intercept: + X = np.c_[np.ones(X.shape[0]), X] + + n_features = X.shape[1] + self.beta = np.zeros(n_features) + for _ in range(self.max_iter): + beta_old = np.copy(self.beta) + for j in range(n_features): + X_j = X[:, j] + y_pred = X @ self.beta - X_j * self.beta[j] + rho = X_j.T @ (y - y_pred) + self.beta[j] = self.soft_threshold(rho, self.alpha) + + if np.linalg.norm(self.beta - beta_old) < self.tol: + break + + def predict(self, X): + if self.fit_intercept: + X = np.c_[np.ones(X.shape[0]), X] + + return X @ self.beta From 20502d7ed985a32e259ba4d98ae1a9a1c1e860c3 Mon Sep 17 00:00:00 2001 From: lokeshrathi Date: Mon, 17 Apr 2023 11:51:38 +0530 Subject: [PATCH 2/3] created a logic for lasso.py --- README.md | 2 +- numpy_ml/linear_models/lasso.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 1000a63..8c6c552 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ To use this code as a starting point for ML prototyping / experimentation, just ```sh $ git clone https://github.com/ddbourgin/numpy-ml.git -$ cd numpy_ml && virtualenv npml && source npml/bin/activate +$ cd numpy_ml && virtualenv npml && source npml/bin/activate $ pip3 install -r requirements-dev.txt ``` diff --git a/numpy_ml/linear_models/lasso.py b/numpy_ml/linear_models/lasso.py index 57bd1ca..9217029 100644 --- a/numpy_ml/linear_models/lasso.py +++ b/numpy_ml/linear_models/lasso.py @@ -44,5 +44,4 @@ def fit(self, X, y): def predict(self, X): if self.fit_intercept: X = np.c_[np.ones(X.shape[0]), X] - return X @ self.beta From 8ce355f69949013d3546fc5f3c94ac40de72ba72 Mon Sep 17 00:00:00 2001 From: lokeshrathi Date: Mon, 17 Apr 2023 11:53:25 +0530 Subject: [PATCH 3/3] created a logic for lasso.py --- numpy_ml/linear_models/lasso.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_ml/linear_models/lasso.py b/numpy_ml/linear_models/lasso.py index 9217029..259dae3 100644 --- a/numpy_ml/linear_models/lasso.py +++ b/numpy_ml/linear_models/lasso.py @@ -40,7 +40,6 @@ def fit(self, X, y): if np.linalg.norm(self.beta - beta_old) < self.tol: break - def predict(self, X): if self.fit_intercept: X = np.c_[np.ones(X.shape[0]), X]