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