Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 3, 2025
1 parent 6991fb0 commit a4f585c
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions machine_learning/ridge_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
from matplotlib import pyplot as plt
from sklearn import datasets


# Ridge Regression function
# reference : https://en.wikipedia.org/wiki/Ridge_regression
def ridge_cost_function(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha: float) -> float:
def ridge_cost_function(
X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha: float

Check failure on line 9 in machine_learning/ridge_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/ridge_regression.py:9:5: N803 Argument name `X` should be lowercase
) -> float:
"""
Compute the Ridge regression cost function with L2 regularization.
Expand All @@ -26,10 +29,20 @@ def ridge_cost_function(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha:
"""
m = len(y)
predictions = np.dot(X, theta)
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (alpha / 2) * np.sum(theta[1:] ** 2)
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (alpha / 2) * np.sum(
theta[1:] ** 2
)
return cost

def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alpha: float, learning_rate: float, max_iterations: int) -> np.ndarray:

def ridge_gradient_descent(
X: np.ndarray,

Check failure on line 39 in machine_learning/ridge_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/ridge_regression.py:39:5: N803 Argument name `X` should be lowercase
y: np.ndarray,
theta: np.ndarray,
alpha: float,
learning_rate: float,
max_iterations: int,
) -> np.ndarray:
"""
Perform gradient descent to minimize the cost function and fit the Ridge regression model.

Check failure on line 47 in machine_learning/ridge_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/ridge_regression.py:47:89: E501 Line too long (94 > 88)
Expand Down Expand Up @@ -60,7 +73,6 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
return theta



if __name__ == "__main__":
import doctest

Check failure on line 77 in machine_learning/ridge_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

machine_learning/ridge_regression.py:77:12: F401 `doctest` imported but unused

Expand All @@ -81,18 +93,21 @@ def ridge_gradient_descent(X: np.ndarray, y: np.ndarray, theta: np.ndarray, alph
learning_rate = 0.01
max_iterations = 1000

optimized_theta = ridge_gradient_descent(X, y, theta_initial, alpha, learning_rate, max_iterations)
optimized_theta = ridge_gradient_descent(
X, y, theta_initial, alpha, learning_rate, max_iterations
)
print(f"Optimized theta: {optimized_theta}")

# Prediction
def predict(X, theta):

Check failure on line 102 in machine_learning/ridge_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/ridge_regression.py:102:17: N803 Argument name `X` should be lowercase
return np.dot(X, theta)

y_pred = predict(X, optimized_theta)

# Plotting the results (here we visualize predicted vs actual values)
plt.figure(figsize=(10, 6))
plt.scatter(y, y_pred, color='b', label='Predictions vs Actual')
plt.plot([min(y), max(y)], [min(y), max(y)], color='r', label='Perfect Fit')
plt.scatter(y, y_pred, color="b", label="Predictions vs Actual")
plt.plot([min(y), max(y)], [min(y), max(y)], color="r", label="Perfect Fit")
plt.xlabel("Actual values")
plt.ylabel("Predicted values")
plt.title("Ridge Regression: Actual vs Predicted Values")
Expand Down

0 comments on commit a4f585c

Please # to comment.