Skip to content

Commit 544a38b

Browse files
committed
resolved conflicts
1 parent c76784e commit 544a38b

File tree

1 file changed

+19
-19
lines changed
  • machine_learning/ridge_regression

1 file changed

+19
-19
lines changed

machine_learning/ridge_regression/model.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44

55
class RidgeRegression:
6-
def __init__(
7-
self,
8-
alpha: float = 0.001,
9-
regularization_param: float = 0.1,
10-
num_iterations: int = 1000,
11-
) -> None:
6+
def __init__(self,
7+
alpha: float = 0.001,
8+
regularization_param: float = 0.1,
9+
num_iterations: int = 1000,
10+
) -> None:
1211
self.alpha: float = alpha
1312
self.regularization_param: float = regularization_param
1413
self.num_iterations: int = num_iterations
1514
self.theta: np.ndarray = None
1615

1716
def feature_scaling(
18-
self, X: np.ndarray
17+
self, x: np.ndarray
1918
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
20-
mean = np.mean(X, axis=0)
21-
std = np.std(X, axis=0)
19+
mean = np.mean(x, axis=0)
20+
std = np.std(x, axis=0)
2221

2322
# avoid division by zero for constant features (std = 0)
2423
std[std == 0] = 1 # set std=1 for constant features to avoid NaN
@@ -31,7 +30,7 @@ def fit(self, x: np.ndarray, y: np.ndarray) -> None:
3130
m, n = x_scaled.shape
3231
self.theta = np.zeros(n) # initializing weights to zeros
3332

34-
for i in range(self.num_iterations):
33+
for _ in range(self.num_iterations):
3534
predictions = x_scaled.dot(self.theta)
3635
error = predictions - y
3736

@@ -41,18 +40,19 @@ def fit(self, x: np.ndarray, y: np.ndarray) -> None:
4140
) / m
4241
self.theta -= self.alpha * gradient # updating weights
4342

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

4847
def compute_cost(self, x: np.ndarray, y: np.ndarray) -> float:
4948
x_scaled, _, _ = self.feature_scaling(x)
5049
m = len(y)
5150

5251
predictions = x_scaled.dot(self.theta)
53-
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (
54-
self.regularization_param / (2 * m)
55-
) * np.sum(self.theta**2)
52+
cost = (
53+
1 / (2 * m)) * np.sum((predictions - y) ** 2) + (
54+
self.regularization_param / (2 * m)
55+
) * np.sum(self.theta**2)
5656
return cost
5757

5858
def mean_absolute_error(self, y_true: np.ndarray, y_pred: np.ndarray) -> float:
@@ -61,9 +61,9 @@ def mean_absolute_error(self, y_true: np.ndarray, y_pred: np.ndarray) -> float:
6161

6262
# Example usage
6363
if __name__ == "__main__":
64-
df = pd.read_csv("ADRvsRating.csv")
65-
x = df[["Rating"]].values
66-
y = df["ADR"].values
64+
data = pd.read_csv("ADRvsRating.csv")
65+
x = data[["Rating"]].to_numpy()
66+
y = data["ADR"].to_numpy()
6767
y = (y - np.mean(y)) / np.std(y)
6868

6969
# added bias term to the feature matrix

0 commit comments

Comments
 (0)