Skip to content

Commit 6fc134d

Browse files
committed
added ridge regression
2 parents a84d209 + d4fc2bf commit 6fc134d

File tree

1 file changed

+28
-13
lines changed
  • machine_learning/ridge_regression

1 file changed

+28
-13
lines changed
+28-13
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
11
import numpy as np
22
import pandas as pd
33

4+
45
class RidgeRegression:
56
def __init__(self, alpha:float=0.001, regularization_param:float=0.1, num_iterations:int=1000) -> None:
67
self.alpha:float = alpha
78
self.regularization_param:float = regularization_param
89
self.num_iterations:int = num_iterations
910
self.theta:np.ndarray = None
1011

12+
<<<<<<< HEAD
1113

1214
def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
15+
=======
16+
def feature_scaling(self, X):
17+
>>>>>>> d4fc2bf852ec4a023380f4ef367edefa88fd6881
1318
mean = np.mean(X, axis=0)
1419
std = np.std(X, axis=0)
15-
20+
1621
# avoid division by zero for constant features (std = 0)
1722
std[std == 0] = 1 # set std=1 for constant features to avoid NaN
18-
23+
1924
X_scaled = (X - mean) / std
2025
return X_scaled, mean, std
21-
2226

2327
def fit(self, X:np.ndarray, y:np.ndarray) -> None:
2428
X_scaled, mean, std = self.feature_scaling(X)
2529
m, n = X_scaled.shape
2630
self.theta = np.zeros(n) # initializing weights to zeros
27-
31+
2832
for i in range(self.num_iterations):
2933
predictions = X_scaled.dot(self.theta)
3034
error = predictions - y
31-
35+
3236
# computing gradient with L2 regularization
33-
gradient = (X_scaled.T.dot(error) + self.regularization_param * self.theta) / m
37+
gradient = (
38+
X_scaled.T.dot(error) + self.regularization_param * self.theta
39+
) / m
3440
self.theta -= self.alpha * gradient # updating weights
3541

36-
3742
def predict(self, X:np.ndarray) -> np.ndarray:
3843
X_scaled, _, _ = self.feature_scaling(X)
3944
return X_scaled.dot(self.theta)
40-
4145

46+
<<<<<<< HEAD
4247
def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
4348
X_scaled, _, _ = self.feature_scaling(X)
49+
=======
50+
def compute_cost(self, X, y):
51+
X_scaled, _, _ = self.feature_scaling(X)
52+
>>>>>>> d4fc2bf852ec4a023380f4ef367edefa88fd6881
4453
m = len(y)
45-
54+
4655
predictions = X_scaled.dot(self.theta)
47-
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (self.regularization_param / (2 * m)) * np.sum(self.theta**2)
56+
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (
57+
self.regularization_param / (2 * m)
58+
) * np.sum(self.theta**2)
4859
return cost
49-
5060

5161
def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
5262
return np.mean(np.abs(y_true - y_pred))
53-
63+
5464

5565
# Example usage
5666
if __name__ == "__main__":
@@ -59,8 +69,13 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
5969
y = df["ADR"].values
6070
y = (y - np.mean(y)) / np.std(y)
6171

72+
<<<<<<< HEAD
6273
# added bias term to the feature matrix
6374
X = np.c_[np.ones(X.shape[0]), X]
75+
=======
76+
# Add bias term (intercept) to the feature matrix
77+
X = np.c_[np.ones(X.shape[0]), X]
78+
>>>>>>> d4fc2bf852ec4a023380f4ef367edefa88fd6881
6479

6580
# initialize and train the ridge regression model
6681
model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)
@@ -72,4 +87,4 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
7287
# results
7388
print("Optimized Weights:", model.theta)
7489
print("Cost:", model.compute_cost(X, y))
75-
print("Mean Absolute Error:", model.mean_absolute_error(y, predictions))
90+
print("Mean Absolute Error:", model.mean_absolute_error(y, predictions))

0 commit comments

Comments
 (0)