Skip to content

Commit a84d209

Browse files
committed
added ridge regression
1 parent b72320b commit a84d209

File tree

1 file changed

+12
-12
lines changed
  • machine_learning/ridge_regression

1 file changed

+12
-12
lines changed

machine_learning/ridge_regression/model.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import pandas as pd
33

44
class RidgeRegression:
5-
def __init__(self, alpha=0.001, regularization_param=0.1, num_iterations=1000):
6-
self.alpha = alpha
7-
self.regularization_param = regularization_param
8-
self.num_iterations = num_iterations
9-
self.theta = None
5+
def __init__(self, alpha:float=0.001, regularization_param:float=0.1, num_iterations:int=1000) -> None:
6+
self.alpha:float = alpha
7+
self.regularization_param:float = regularization_param
8+
self.num_iterations:int = num_iterations
9+
self.theta:np.ndarray = None
1010

1111

12-
def feature_scaling(self, X):
12+
def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
1313
mean = np.mean(X, axis=0)
1414
std = np.std(X, axis=0)
1515

@@ -20,7 +20,7 @@ def feature_scaling(self, X):
2020
return X_scaled, mean, std
2121

2222

23-
def fit(self, X, y):
23+
def fit(self, X:np.ndarray, y:np.ndarray) -> None:
2424
X_scaled, mean, std = self.feature_scaling(X)
2525
m, n = X_scaled.shape
2626
self.theta = np.zeros(n) # initializing weights to zeros
@@ -34,12 +34,12 @@ def fit(self, X, y):
3434
self.theta -= self.alpha * gradient # updating weights
3535

3636

37-
def predict(self, X):
37+
def predict(self, X:np.ndarray) -> np.ndarray:
3838
X_scaled, _, _ = self.feature_scaling(X)
3939
return X_scaled.dot(self.theta)
4040

4141

42-
def compute_cost(self, X, y):
42+
def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
4343
X_scaled, _, _ = self.feature_scaling(X)
4444
m = len(y)
4545

@@ -48,7 +48,7 @@ def compute_cost(self, X, y):
4848
return cost
4949

5050

51-
def mean_absolute_error(self, y_true, y_pred):
51+
def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
5252
return np.mean(np.abs(y_true - y_pred))
5353

5454

@@ -59,10 +59,10 @@ def mean_absolute_error(self, y_true, y_pred):
5959
y = df["ADR"].values
6060
y = (y - np.mean(y)) / np.std(y)
6161

62-
# Add bias term (intercept) to the feature matrix
62+
# added bias term to the feature matrix
6363
X = np.c_[np.ones(X.shape[0]), X]
6464

65-
# initialize and train the Ridge Regression model
65+
# initialize and train the ridge regression model
6666
model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)
6767
model.fit(X, y)
6868

0 commit comments

Comments
 (0)