Skip to content

Commit 351e83b

Browse files
committed
Updated code as per PR feedback 6
2 parents ffd18e0 + c8c1d9a commit 351e83b

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

machine_learning/ridge_regression.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
from matplotlib import pyplot as plt
44
from sklearn import datasets
55

6+
67
# Ridge Regression function
78
# 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:
912
"""
1013
Compute the Ridge regression cost function with L2 regularization.
1114
@@ -27,12 +30,26 @@ def ridge_cost_function(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
2730
"""
2831
m = len(y)
2932
predictions = np.dot(x, theta)
33+
<<<<<<< HEAD
3034
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + \
3135
(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
3241

3342
return cost
3443

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:
3653
"""
3754
Perform gradient descent to minimize the cost function and fit the Ridge regression model.
3855
@@ -62,8 +79,13 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
6279

6380
return theta
6481

82+
<<<<<<< HEAD
83+
=======
84+
85+
>>>>>>> c8c1d9a5896ed6f64a71a2f9392eb4ecc7ffff12
6586
if __name__ == "__main__":
6687
import doctest
88+
6789
doctest.testmod()
6890

6991
# Load California Housing dataset
@@ -83,7 +105,9 @@ def ridge_gradient_descent(x: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
83105
learning_rate = 0.01
84106
max_iterations = 1000
85107

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+
)
87111
print(f"Optimized theta: {optimized_theta}")
88112

89113
# Prediction
@@ -94,8 +118,8 @@ def predict(x, theta):
94118

95119
# Plotting the results (here we visualize predicted vs actual values)
96120
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")
99123
plt.xlabel("Actual values")
100124
plt.ylabel("Predicted values")
101125
plt.title("Ridge Regression: Actual vs Predicted Values")

0 commit comments

Comments
 (0)