Skip to content

Commit b1353dd

Browse files
committed
ridge regression
2 parents 7484cda + 21fe32f commit b1353dd

File tree

1 file changed

+6
-1
lines changed
  • machine_learning/ridge_regression

1 file changed

+6
-1
lines changed

machine_learning/ridge_regression/model.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndar
2020
X_scaled = (X - mean) / std
2121
return X_scaled, mean, std
2222

23+
2324
def fit(self, X:np.ndarray, y:np.ndarray) -> None:
2425
X_scaled, mean, std = self.feature_scaling(X)
2526
m, n = X_scaled.shape
2627
self.theta = np.zeros(n) # initializing weights to zeros
2728

29+
2830
for i in range(self.num_iterations):
2931
predictions = X_scaled.dot(self.theta)
3032
error = predictions - y
@@ -35,12 +37,14 @@ def fit(self, X:np.ndarray, y:np.ndarray) -> None:
3537
) / m
3638
self.theta -= self.alpha * gradient # updating weights
3739

40+
3841
def predict(self, X:np.ndarray) -> np.ndarray:
3942
X_scaled, _, _ = self.feature_scaling(X)
4043
return X_scaled.dot(self.theta)
4144

45+
4246
def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
43-
X_scaled, _, _ = self.feature_scaling(X)
47+
X_scaled, _, _ = self.feature_scaling(X)
4448
m = len(y)
4549

4650
predictions = X_scaled.dot(self.theta)
@@ -49,6 +53,7 @@ def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
4953
) * np.sum(self.theta**2)
5054
return cost
5155

56+
5257
def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
5358
return np.mean(np.abs(y_true - y_pred))
5459

0 commit comments

Comments
 (0)