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