Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear regression for polynomials and implemented Lasso and Ridge from scratch #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 02 Linear Regression/Lasso&RidgeRegression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np

class Lasso_Ridge_Regression:

def __init__(self, lr=0.001, n_iters=1000, alpha=0.1):
self.lr = lr
self.n_iters = n_iters
self.alpha = alpha # regularization strength
self.weights = None
self.bias = None

def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0

for _ in range(self.n_iters):
y_pred = np.dot(X, self.weights) + self.bias

dw = (1 / n_samples) * np.dot(X.T, (y_pred - y)) + self.alpha * np.sign(self.weights)
'''
dw = (1 / n_samples) * np.dot(X.T, (y_pred - y)) + 2 * self.alpha * self.weights

this is for RidgeRegression

'''

db = (1 / n_samples) * np.sum(y_pred - y)

self.weights -= self.lr * dw
self.bias -= self.lr * db

def predict(self, X):
y_pred = np.dot(X, self.weights) + self.bias
return y_pred
Empty file.