3
3
4
4
5
5
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 :
12
11
self .alpha : float = alpha
13
12
self .regularization_param : float = regularization_param
14
13
self .num_iterations : int = num_iterations
15
14
self .theta : np .ndarray = None
16
15
17
16
def feature_scaling (
18
- self , X : np .ndarray
17
+ self , x : np .ndarray
19
18
) -> 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 )
22
21
23
22
# avoid division by zero for constant features (std = 0)
24
23
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:
31
30
m , n = x_scaled .shape
32
31
self .theta = np .zeros (n ) # initializing weights to zeros
33
32
34
- for i in range (self .num_iterations ):
33
+ for _ in range (self .num_iterations ):
35
34
predictions = x_scaled .dot (self .theta )
36
35
error = predictions - y
37
36
@@ -41,18 +40,19 @@ def fit(self, x: np.ndarray, y: np.ndarray) -> None:
41
40
) / m
42
41
self .theta -= self .alpha * gradient # updating weights
43
42
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 )
47
46
48
47
def compute_cost (self , x : np .ndarray , y : np .ndarray ) -> float :
49
48
x_scaled , _ , _ = self .feature_scaling (x )
50
49
m = len (y )
51
50
52
51
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 )
56
56
return cost
57
57
58
58
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:
61
61
62
62
# Example usage
63
63
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 ()
67
67
y = (y - np .mean (y )) / np .std (y )
68
68
69
69
# added bias term to the feature matrix
0 commit comments