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
@@ -26,10 +29,20 @@ def ridge_cost_function(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
26
29
"""
27
30
m = len (y )
28
31
predictions = np .dot (X , theta )
29
- cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (alpha / 2 ) * np .sum (theta [1 :] ** 2 )
32
+ cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (alpha / 2 ) * np .sum (
33
+ theta [1 :] ** 2
34
+ )
30
35
return cost
31
36
32
- def ridge_gradient_descent (X : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float , learning_rate : float , max_iterations : int ) -> np .ndarray :
37
+
38
+ def ridge_gradient_descent (
39
+ X : np .ndarray ,
40
+ y : np .ndarray ,
41
+ theta : np .ndarray ,
42
+ alpha : float ,
43
+ learning_rate : float ,
44
+ max_iterations : int ,
45
+ ) -> np .ndarray :
33
46
"""
34
47
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
35
48
@@ -60,7 +73,6 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
60
73
return theta
61
74
62
75
63
-
64
76
if __name__ == "__main__" :
65
77
import doctest
66
78
@@ -81,18 +93,21 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
81
93
learning_rate = 0.01
82
94
max_iterations = 1000
83
95
84
- optimized_theta = ridge_gradient_descent (X , y , theta_initial , alpha , learning_rate , max_iterations )
96
+ optimized_theta = ridge_gradient_descent (
97
+ X , y , theta_initial , alpha , learning_rate , max_iterations
98
+ )
85
99
print (f"Optimized theta: { optimized_theta } " )
86
100
87
101
# Prediction
88
102
def predict (X , theta ):
89
103
return np .dot (X , theta )
104
+
90
105
y_pred = predict (X , optimized_theta )
91
106
92
107
# Plotting the results (here we visualize predicted vs actual values)
93
108
plt .figure (figsize = (10 , 6 ))
94
- plt .scatter (y , y_pred , color = 'b' , label = ' Predictions vs Actual' )
95
- plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = 'r' , label = ' Perfect Fit' )
109
+ plt .scatter (y , y_pred , color = "b" , label = " Predictions vs Actual" )
110
+ plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = "r" , label = " Perfect Fit" )
96
111
plt .xlabel ("Actual values" )
97
112
plt .ylabel ("Predicted values" )
98
113
plt .title ("Ridge Regression: Actual vs Predicted Values" )
0 commit comments