Skip to content

Commit 3876437

Browse files
committed
resolved conflicts
2 parents 1713cbe + 2eeb450 commit 3876437

File tree

1 file changed

+22
-24
lines changed
  • machine_learning/ridge_regression

1 file changed

+22
-24
lines changed

machine_learning/ridge_regression/model.py

+22-24
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33

44

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

2022
# avoid division by zero for constant features (std = 0)
2123
std[std == 0] = 1 # set std=1 for constant features to avoid NaN
2224

2325
x_scaled = (x - mean) / std
2426
return x_scaled, mean, std
2527

26-
27-
def fit(self, x:np.ndarray, y:np.ndarray) -> None:
28+
def fit(self, x: np.ndarray, y: np.ndarray) -> None:
2829
x_scaled, mean, std = self.feature_scaling(x)
2930
m, n = x_scaled.shape
3031
self.theta = np.zeros(n) # initializing weights to zeros
@@ -39,13 +40,11 @@ def fit(self, x:np.ndarray, y:np.ndarray) -> None:
3940
) / m
4041
self.theta -= self.alpha * gradient # updating weights
4142

43+
def predict(self, X: np.ndarray) -> np.ndarray:
44+
X_scaled, _, _ = self.feature_scaling(X)
45+
return X_scaled.dot(self.theta)
4246

43-
def predict(self, x:np.ndarray) -> np.ndarray:
44-
x_scaled, _, _ = self.feature_scaling(x)
45-
return x_scaled.dot(self.theta)
46-
47-
48-
def compute_cost(self, x:np.ndarray, y:np.ndarray) -> float:
47+
def compute_cost(self, x: np.ndarray, y: np.ndarray) -> float:
4948
x_scaled, _, _ = self.feature_scaling(x)
5049
m = len(y)
5150

@@ -56,8 +55,7 @@ def compute_cost(self, x:np.ndarray, y:np.ndarray) -> float:
5655
) * np.sum(self.theta**2)
5756
return cost
5857

59-
60-
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:
6159
return np.mean(np.abs(y_true - y_pred))
6260

6361

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

7169
# added bias term to the feature matrix
72-
x = np.c_[np.ones(x.shape[0]), x]
70+
x = np.c_[np.ones(x.shape[0]), x]
7371

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

0 commit comments

Comments
 (0)