Skip to content
108 changes: 108 additions & 0 deletions machine_learning/ridge_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import numpy as np
import pandas as pd


class RidgeRegression:
def __init__(self, alpha=0.001, lambda_=0.1, iterations=1000):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: alpha

Please provide type hint for the parameter: lambda_

Please provide type hint for the parameter: iterations

"""
Ridge Regression Constructor
:param alpha: Learning rate for gradient descent
:param lambda_: Regularization parameter (L2 regularization)
:param iterations: Number of iterations for gradient descent
"""
self.alpha = alpha
self.lambda_ = lambda_
self.iterations = iterations
self.theta = None

def feature_scaling(self, X):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: feature_scaling. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/ridge_regression.py, please provide doctest for the function feature_scaling

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

"""
Normalize features to have mean 0 and standard deviation 1
:param X: Input features, shape (m, n)
:return: Scaled features, mean, and std for each feature
"""
mean = np.mean(X, axis=0)
std = np.std(X, axis=0)

# Avoid division by zero for constant features (std = 0)
std[std == 0] = 1 # Set std=1 for constant features to avoid NaN

X_scaled = (X - mean) / std

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X_scaled

return X_scaled, mean, std

def fit(self, X, y):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/ridge_regression.py, please provide doctest for the function fit

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

"""
Fit the Ridge Regression model to the training data
:param X: Input features, shape (m, n)
:param y: Target values, shape (m,)
"""
X_scaled, mean, std = self.feature_scaling(X) # Normalize features

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X_scaled

m, n = X_scaled.shape
self.theta = np.zeros(n) # Initialize weights to zeros

for i in range(self.iterations):
predictions = X_scaled.dot(self.theta)
error = predictions - y

# Compute gradient with L2 regularization
gradient = (X_scaled.T.dot(error) + self.lambda_ * self.theta) / m
self.theta -= self.alpha * gradient # Update weights

def predict(self, X):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/ridge_regression.py, please provide doctest for the function predict

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

"""
Predict values using the trained model
:param X: Input features, shape (m, n)
:return: Predicted values, shape (m,)
"""
X_scaled, _, _ = self.feature_scaling(X) # Scale features using training data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X_scaled

return X_scaled.dot(self.theta)

def compute_cost(self, X, y):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: compute_cost. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/ridge_regression.py, please provide doctest for the function compute_cost

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

"""
Compute the cost function with regularization
:param X: Input features, shape (m, n)
:param y: Target values, shape (m,)
:return: Computed cost
"""
X_scaled, _, _ = self.feature_scaling(X) # Scale features using training data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X_scaled

m = len(y)
predictions = X_scaled.dot(self.theta)
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (
self.lambda_ / (2 * m)
) * np.sum(self.theta**2)
return cost

def mean_absolute_error(self, y_true, y_pred):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: mean_absolute_error. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/ridge_regression.py, please provide doctest for the function mean_absolute_error

Please provide type hint for the parameter: y_true

Please provide type hint for the parameter: y_pred

"""
Compute Mean Absolute Error (MAE) between true and predicted values
:param y_true: Actual target values, shape (m,)
:param y_pred: Predicted target values, shape (m,)
:return: MAE
"""
return np.mean(np.abs(y_true - y_pred))


# Example usage
if __name__ == "__main__":
# Load dataset
df = pd.read_csv(
"https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/master/Week1/ADRvsRating.csv"
)
X = df[["Rating"]].values # Feature: Rating
y = df["ADR"].values # Target: ADR
y = (y - np.mean(y)) / np.std(y)

# Add bias term (intercept) to the feature matrix
X = np.c_[np.ones(X.shape[0]), X] # Add intercept term

# Initialize and train the Ridge Regression model
model = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=1000)
model.fit(X, y)

# Predictions
predictions = model.predict(X)

# Results
print("Optimized Weights:", model.theta)
print("Cost:", model.compute_cost(X, y))
print("Mean Absolute Error:", model.mean_absolute_error(y, predictions))