3
3
4
4
5
5
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 ]:
14
20
mean = np .mean (X , axis = 0 )
15
21
std = np .std (X , axis = 0 )
16
22
@@ -20,13 +26,11 @@ def feature_scaling(self, X:np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndar
20
26
X_scaled = (X - mean ) / std
21
27
return X_scaled , mean , std
22
28
23
-
24
- def fit (self , X :np .ndarray , y :np .ndarray ) -> None :
29
+ def fit (self , X : np .ndarray , y : np .ndarray ) -> None :
25
30
X_scaled , mean , std = self .feature_scaling (X )
26
31
m , n = X_scaled .shape
27
32
self .theta = np .zeros (n ) # initializing weights to zeros
28
33
29
-
30
34
for i in range (self .num_iterations ):
31
35
predictions = X_scaled .dot (self .theta )
32
36
error = predictions - y
@@ -37,13 +41,11 @@ def fit(self, X:np.ndarray, y:np.ndarray) -> None:
37
41
) / m
38
42
self .theta -= self .alpha * gradient # updating weights
39
43
40
-
41
- def predict (self , X :np .ndarray ) -> np .ndarray :
44
+ def predict (self , X : np .ndarray ) -> np .ndarray :
42
45
X_scaled , _ , _ = self .feature_scaling (X )
43
46
return X_scaled .dot (self .theta )
44
47
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 :
47
49
X_scaled , _ , _ = self .feature_scaling (X )
48
50
m = len (y )
49
51
@@ -53,8 +55,7 @@ def compute_cost(self, X:np.ndarray, y:np.ndarray) -> float:
53
55
) * np .sum (self .theta ** 2 )
54
56
return cost
55
57
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 :
58
59
return np .mean (np .abs (y_true - y_pred ))
59
60
60
61
@@ -66,7 +67,7 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
66
67
y = (y - np .mean (y )) / np .std (y )
67
68
68
69
# 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 ]
70
71
71
72
# initialize and train the ridge regression model
72
73
model = RidgeRegression (alpha = 0.01 , regularization_param = 0.1 , num_iterations = 1000 )
0 commit comments