3
3
from matplotlib import pyplot as plt
4
4
from sklearn import datasets
5
5
6
+
6
7
# Ridge Regression function
7
8
# reference : https://en.wikipedia.org/wiki/Ridge_regression
8
- def ridge_cost_function (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float ) -> float :
9
+ def ridge_cost_function (
10
+ x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float
11
+ ) -> float :
9
12
"""
10
13
Compute the Ridge regression cost function with L2 regularization.
11
14
@@ -27,12 +30,26 @@ def ridge_cost_function(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
27
30
"""
28
31
m = len (y )
29
32
predictions = np .dot (x , theta )
33
+ < << << << HEAD
30
34
cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + \
31
35
(alpha / 2 ) * np .sum (theta [1 :] ** 2 )
36
+ == == == =
37
+ cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (alpha / 2 ) * np .sum (
38
+ theta [1 :] ** 2
39
+ )
40
+ >> >> >> > c8c1d9a5896ed6f64a71a2f9392eb4ecc7ffff12
32
41
33
42
return cost
34
43
35
- def ridge_gradient_descent (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float , learning_rate : float , max_iterations : int ) -> np .ndarray :
44
+
45
+ def ridge_gradient_descent (
46
+ x : np .ndarray ,
47
+ y : np .ndarray ,
48
+ theta : np .ndarray ,
49
+ alpha : float ,
50
+ learning_rate : float ,
51
+ max_iterations : int ,
52
+ ) -> np .ndarray :
36
53
"""
37
54
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
38
55
@@ -62,8 +79,13 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
62
79
63
80
return theta
64
81
82
+ < << << << HEAD
83
+ == == == =
84
+
85
+ >> >> >> > c8c1d9a5896ed6f64a71a2f9392eb4ecc7ffff12
65
86
if __name__ == "__main__" :
66
87
import doctest
88
+
67
89
doctest .testmod ()
68
90
69
91
# Load California Housing dataset
@@ -83,7 +105,9 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
83
105
learning_rate = 0.01
84
106
max_iterations = 1000
85
107
86
- optimized_theta = ridge_gradient_descent (x , y , theta_initial , alpha , learning_rate , max_iterations )
108
+ optimized_theta = ridge_gradient_descent (
109
+ x , y , theta_initial , alpha , learning_rate , max_iterations
110
+ )
87
111
print (f"Optimized theta: { optimized_theta } " )
88
112
89
113
# Prediction
@@ -94,8 +118,8 @@ def predict(x, theta):
94
118
95
119
# Plotting the results (here we visualize predicted vs actual values)
96
120
plt .figure (figsize = (10 , 6 ))
97
- plt .scatter (y , y_pred , color = 'b' , label = ' Predictions vs Actual' )
98
- plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = 'r' , label = ' Perfect Fit' )
121
+ plt .scatter (y , y_pred , color = "b" , label = " Predictions vs Actual" )
122
+ plt .plot ([min (y ), max (y )], [min (y ), max (y )], color = "r" , label = " Perfect Fit" )
99
123
plt .xlabel ("Actual values" )
100
124
plt .ylabel ("Predicted values" )
101
125
plt .title ("Ridge Regression: Actual vs Predicted Values" )
0 commit comments