diff --git a/02 Linear Regression/Lasso&RidgeRegression.py b/02 Linear Regression/Lasso&RidgeRegression.py new file mode 100644 index 0000000..f472cf2 --- /dev/null +++ b/02 Linear Regression/Lasso&RidgeRegression.py @@ -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 diff --git a/02 Linear Regression/PolynomialRegression.py b/02 Linear Regression/PolynomialRegression.py new file mode 100644 index 0000000..e69de29