Skip to content

Commit fb1b7a7

Browse files
committed
Updated code as per PR feedback
1 parent 6991fb0 commit fb1b7a7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

machine_learning/ridge_regression.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
# Ridge Regression function
66
# 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:
88
"""
99
Compute the Ridge regression cost function with L2 regularization.
1010
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)
1212
1313
Where:
1414
- J(θ) is the cost function we aim to minimize
1515
- m is the number of training examples
1616
- hθ(x) = X * θ (prediction)
1717
- y_i is the actual target value for example i
18-
- α is the regularization parameter
18+
- a is the regularization parameter
1919
2020
@param X: The feature matrix (m x n)
2121
@param y: The target vector (m,)
@@ -26,10 +26,11 @@ def ridge_cost_function(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
2626
"""
2727
m = len(y)
2828
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)
3031
return cost
3132

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:
3334
"""
3435
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
3536
@@ -63,6 +64,7 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
6364

6465
if __name__ == "__main__":
6566
import doctest
67+
doctest.testmod()
6668

6769
# Load California Housing dataset
6870
california_housing = datasets.fetch_california_housing()
@@ -97,4 +99,5 @@ def predict(X, theta):
9799
plt.ylabel("Predicted values")
98100
plt.title("Ridge Regression: Actual vs Predicted Values")
99101
plt.legend()
102+
#plots on a graph
100103
plt.show()

0 commit comments

Comments
 (0)