3
3
4
4
5
5
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 )
19
21
20
22
# avoid division by zero for constant features (std = 0)
21
23
std [std == 0 ] = 1 # set std=1 for constant features to avoid NaN
22
24
23
25
x_scaled = (x - mean ) / std
24
26
return x_scaled , mean , std
25
27
26
-
27
- def fit (self , x :np .ndarray , y :np .ndarray ) -> None :
28
+ def fit (self , x : np .ndarray , y : np .ndarray ) -> None :
28
29
x_scaled , mean , std = self .feature_scaling (x )
29
30
m , n = x_scaled .shape
30
31
self .theta = np .zeros (n ) # initializing weights to zeros
@@ -39,13 +40,11 @@ def fit(self, x:np.ndarray, y:np.ndarray) -> None:
39
40
) / m
40
41
self .theta -= self .alpha * gradient # updating weights
41
42
43
+ def predict (self , X : np .ndarray ) -> np .ndarray :
44
+ X_scaled , _ , _ = self .feature_scaling (X )
45
+ return X_scaled .dot (self .theta )
42
46
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 :
49
48
x_scaled , _ , _ = self .feature_scaling (x )
50
49
m = len (y )
51
50
@@ -56,8 +55,7 @@ def compute_cost(self, x:np.ndarray, y:np.ndarray) -> float:
56
55
) * np .sum (self .theta ** 2 )
57
56
return cost
58
57
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 :
61
59
return np .mean (np .abs (y_true - y_pred ))
62
60
63
61
@@ -69,7 +67,7 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
69
67
y = (y - np .mean (y )) / np .std (y )
70
68
71
69
# 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 ]
73
71
74
72
# initialize and train the ridge regression model
75
73
model = RidgeRegression (alpha = 0.01 , regularization_param = 0.1 , num_iterations = 1000 )
0 commit comments