Skip to content

Commit 2eeb450

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b1353dd commit 2eeb450

File tree

1 file changed

+19
-18
lines changed
  • machine_learning/ridge_regression

1 file changed

+19
-18
lines changed

machine_learning/ridge_regression/model.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33

44

55
class RidgeRegression:
6-
def __init__(self, alpha:float=0.001, regularization_param:float=0.1, num_iterations:int=1000) -> None:
7-
self.alpha:float = alpha
8-
self.regularization_param:float = regularization_param
9-
self.num_iterations:int = num_iterations
10-
self.theta:np.ndarray = None
11-
12-
13-
def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
6+
def __init__(
7+
self,
8+
alpha: float = 0.001,
9+
regularization_param: float = 0.1,
10+
num_iterations: int = 1000,
11+
) -> None:
12+
self.alpha: float = alpha
13+
self.regularization_param: float = regularization_param
14+
self.num_iterations: int = num_iterations
15+
self.theta: np.ndarray = None
16+
17+
def feature_scaling(
18+
self, X: np.ndarray
19+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
1420
mean = np.mean(X, axis=0)
1521
std = np.std(X, axis=0)
1622

@@ -20,13 +26,11 @@ def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndar
2026
X_scaled = (X - mean) / std
2127
return X_scaled, mean, std
2228

23-
24-
def fit(self, X:np.ndarray, y:np.ndarray) -> None:
29+
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
2530
X_scaled, mean, std = self.feature_scaling(X)
2631
m, n = X_scaled.shape
2732
self.theta = np.zeros(n) # initializing weights to zeros
2833

29-
3034
for i in range(self.num_iterations):
3135
predictions = X_scaled.dot(self.theta)
3236
error = predictions - y
@@ -37,13 +41,11 @@ def fit(self, X:np.ndarray, y:np.ndarray) -> None:
3741
) / m
3842
self.theta -= self.alpha * gradient # updating weights
3943

40-
41-
def predict(self, X:np.ndarray) -> np.ndarray:
44+
def predict(self, X: np.ndarray) -> np.ndarray:
4245
X_scaled, _, _ = self.feature_scaling(X)
4346
return X_scaled.dot(self.theta)
4447

45-
46-
def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
48+
def compute_cost(self, X: np.ndarray, y: np.ndarray) -> float:
4749
X_scaled, _, _ = self.feature_scaling(X)
4850
m = len(y)
4951

@@ -53,8 +55,7 @@ def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
5355
) * np.sum(self.theta**2)
5456
return cost
5557

56-
57-
def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
58+
def mean_absolute_error(self, y_true: np.ndarray, y_pred: np.ndarray) -> float:
5859
return np.mean(np.abs(y_true - y_pred))
5960

6061

@@ -66,7 +67,7 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
6667
y = (y - np.mean(y)) / np.std(y)
6768

6869
# added bias term to the feature matrix
69-
X = np.c_[np.ones(X.shape[0]), X]
70+
X = np.c_[np.ones(X.shape[0]), X]
7071

7172
# initialize and train the ridge regression model
7273
model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)

0 commit comments

Comments
 (0)