1
1
import numpy as np
2
2
import pandas as pd
3
3
4
+
4
5
class RidgeRegression :
5
6
def __init__ (self , alpha :float = 0.001 , regularization_param :float = 0.1 , num_iterations :int = 1000 ) -> None :
6
7
self .alpha :float = alpha
7
8
self .regularization_param :float = regularization_param
8
9
self .num_iterations :int = num_iterations
9
10
self .theta :np .ndarray = None
10
11
12
+ < << << << HEAD
11
13
12
14
def feature_scaling (self , X :np .ndarray ) -> tuple [np .ndarray , np .ndarray , np .ndarray ]:
15
+ == == == =
16
+ def feature_scaling (self , X ):
17
+ >> >> >> > d4fc2bf852ec4a023380f4ef367edefa88fd6881
13
18
mean = np .mean (X , axis = 0 )
14
19
std = np .std (X , axis = 0 )
15
-
20
+
16
21
# avoid division by zero for constant features (std = 0)
17
22
std [std == 0 ] = 1 # set std=1 for constant features to avoid NaN
18
-
23
+
19
24
X_scaled = (X - mean ) / std
20
25
return X_scaled , mean , std
21
-
22
26
23
27
def fit (self , X :np .ndarray , y :np .ndarray ) -> None :
24
28
X_scaled , mean , std = self .feature_scaling (X )
25
29
m , n = X_scaled .shape
26
30
self .theta = np .zeros (n ) # initializing weights to zeros
27
-
31
+
28
32
for i in range (self .num_iterations ):
29
33
predictions = X_scaled .dot (self .theta )
30
34
error = predictions - y
31
-
35
+
32
36
# computing gradient with L2 regularization
33
- gradient = (X_scaled .T .dot (error ) + self .regularization_param * self .theta ) / m
37
+ gradient = (
38
+ X_scaled .T .dot (error ) + self .regularization_param * self .theta
39
+ ) / m
34
40
self .theta -= self .alpha * gradient # updating weights
35
41
36
-
37
42
def predict (self , X :np .ndarray ) -> np .ndarray :
38
43
X_scaled , _ , _ = self .feature_scaling (X )
39
44
return X_scaled .dot (self .theta )
40
-
41
45
46
+ < << << << HEAD
42
47
def compute_cost (self , X :np .ndarray , y :np .ndarray ) -> float :
43
48
X_scaled , _ , _ = self .feature_scaling (X )
49
+ == == == =
50
+ def compute_cost (self , X , y ):
51
+ X_scaled , _ , _ = self .feature_scaling (X )
52
+ >> >> >> > d4fc2bf852ec4a023380f4ef367edefa88fd6881
44
53
m = len (y )
45
-
54
+
46
55
predictions = X_scaled .dot (self .theta )
47
- cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (self .regularization_param / (2 * m )) * np .sum (self .theta ** 2 )
56
+ cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (
57
+ self .regularization_param / (2 * m )
58
+ ) * np .sum (self .theta ** 2 )
48
59
return cost
49
-
50
60
51
61
def mean_absolute_error (self , y_true :np .ndarray , y_pred :np .ndarray ) -> float :
52
62
return np .mean (np .abs (y_true - y_pred ))
53
-
63
+
54
64
55
65
# Example usage
56
66
if __name__ == "__main__" :
@@ -59,8 +69,13 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
59
69
y = df ["ADR" ].values
60
70
y = (y - np .mean (y )) / np .std (y )
61
71
72
+ < << << << HEAD
62
73
# added bias term to the feature matrix
63
74
X = np .c_ [np .ones (X .shape [0 ]), X ]
75
+ == == == =
76
+ # Add bias term (intercept) to the feature matrix
77
+ X = np .c_ [np .ones (X .shape [0 ]), X ]
78
+ >> >> >> > d4fc2bf852ec4a023380f4ef367edefa88fd6881
64
79
65
80
# initialize and train the ridge regression model
66
81
model = RidgeRegression (alpha = 0.01 , regularization_param = 0.1 , num_iterations = 1000 )
@@ -72,4 +87,4 @@ def mean_absolute_error(self, y_true:np.ndarray, y_pred:np.ndarray) -> float:
72
87
# results
73
88
print ("Optimized Weights:" , model .theta )
74
89
print ("Cost:" , model .compute_cost (X , y ))
75
- print ("Mean Absolute Error:" , model .mean_absolute_error (y , predictions ))
90
+ print ("Mean Absolute Error:" , model .mean_absolute_error (y , predictions ))
0 commit comments