2
2
from matplotlib import pyplot as plt
3
3
from sklearn import datasets
4
4
5
+
5
6
# Ridge Regression function
6
7
# reference : https://en.wikipedia.org/wiki/Ridge_regression
7
- def ridge_cost_function (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float ) -> float :
8
+ def ridge_cost_function (
9
+ x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float
10
+ ) -> float :
8
11
"""
9
12
Compute the Ridge regression cost function with L2 regularization.
10
13
@@ -30,7 +33,15 @@ def ridge_cost_function(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
30
33
cost += (alpha / 2 ) * np .sum (theta [1 :] ** 2 )
31
34
return cost
32
35
33
- def ridge_gradient_descent (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float , learning_rate : float , max_iterations : int ) -> np .ndarray :
36
+
37
+ def ridge_gradient_descent (
38
+ x : np .ndarray ,
39
+ y : np .ndarray ,
40
+ theta : np .ndarray ,
41
+ alpha : float ,
42
+ learning_rate : float ,
43
+ max_iterations : int ,
44
+ ) -> np .ndarray :
34
45
"""
35
46
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
36
47
@@ -61,9 +72,9 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
61
72
return theta
62
73
63
74
64
-
65
75
if __name__ == "__main__" :
66
76
import doctest
77
+
67
78
doctest .testmod ()
68
79
69
80
# Load California Housing dataset
@@ -83,18 +94,21 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
83
94
learning_rate = 0.01
84
95
max_iterations = 1000
85
96
86
- optimized_theta = ridge_gradient_descent (X , y , theta_initial , alpha , learning_rate , max_iterations )
97
+ optimized_theta = ridge_gradient_descent (
98
+ X , y , theta_initial , alpha , learning_rate , max_iterations
99
+ )
87
100
print (f"Optimized theta: { optimized_theta } " )
88
101
89
102
# Prediction
90
103
def predict (X , theta ):
91
104
return np .dot (X , theta )
105
+
92
106
y_pred = predict (X , optimized_theta )
93
107
94
108
# Plotting the results (here we visualize predicted vs actual values)
95
109
plt .figure (figsize = (10 , 6 ))
96
- plt .scatter (y , y_pred , color = 'b' , label = ' Predictions vs Actual' )
97
- plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = 'r' , label = ' Perfect Fit' )
110
+ plt .scatter (y , y_pred , color = "b" , label = " Predictions vs Actual" )
111
+ plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = "r" , label = " Perfect Fit" )
98
112
plt .xlabel ("Actual values" )
99
113
plt .ylabel ("Predicted values" )
100
114
plt .title ("Ridge Regression: Actual vs Predicted Values" )
0 commit comments