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