4
4
5
5
# Ridge Regression function
6
6
# 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 :
7
+ def ridge_cost_function (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float ) -> float :
8
8
"""
9
9
Compute the Ridge regression cost function with L2 regularization.
10
10
11
- J(θ) = (1/2m) * Σ (y_i - hθ(x))^2 + (α /2) * Σ θ_j^2 (for j=1 to n)
11
+ J(θ) = (1/2m) * Σ (y_i - hθ(x))^2 + (a /2) * Σ θ_j^2 (for j=1 to n)
12
12
13
13
Where:
14
14
- J(θ) is the cost function we aim to minimize
15
15
- m is the number of training examples
16
16
- hθ(x) = X * θ (prediction)
17
17
- y_i is the actual target value for example i
18
- - α is the regularization parameter
18
+ - a is the regularization parameter
19
19
20
20
@param X: The feature matrix (m x n)
21
21
@param y: The target vector (m,)
@@ -26,10 +26,11 @@ def ridge_cost_function(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
26
26
"""
27
27
m = len (y )
28
28
predictions = np .dot (X , theta )
29
- cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 ) + (alpha / 2 ) * np .sum (theta [1 :] ** 2 )
29
+ cost = (1 / (2 * m )) * np .sum ((predictions - y ) ** 2 )
30
+ cost += (alpha / 2 ) * np .sum (theta [1 :] ** 2 )
30
31
return cost
31
32
32
- def ridge_gradient_descent (X : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float , learning_rate : float , max_iterations : int ) -> np .ndarray :
33
+ def ridge_gradient_descent (x : np .ndarray , y : np .ndarray , theta : np .ndarray , alpha : float , learning_rate : float , max_iterations : int ) -> np .ndarray :
33
34
"""
34
35
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
35
36
@@ -63,6 +64,7 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
63
64
64
65
if __name__ == "__main__" :
65
66
import doctest
67
+ doctest .testmod ()
66
68
67
69
# Load California Housing dataset
68
70
california_housing = datasets .fetch_california_housing ()
@@ -97,4 +99,5 @@ def predict(X, theta):
97
99
plt .ylabel ("Predicted values" )
98
100
plt .title ("Ridge Regression: Actual vs Predicted Values" )
99
101
plt .legend ()
102
+ #plots on a graph
100
103
plt .show ()
0 commit comments