From 2293d37599c9bcc7c1e3769ead589bd3aa09da99 Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Tue, 22 Oct 2024 23:19:28 +0530 Subject: [PATCH 01/11] Fix issue #12108: Added Ridge Regression to Machine Learning --- machine_learning/ridge_regression.py | 108 +++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 machine_learning/ridge_regression.py diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py new file mode 100644 index 000000000..d4d3162e5 --- /dev/null +++ b/machine_learning/ridge_regression.py @@ -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): + """ + 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): + """ + 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 + return X_scaled, mean, std + + def fit(self, X, 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 + 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): + """ + 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 + return X_scaled.dot(self.theta) + + def compute_cost(self, X, 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 + 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): + """ + 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)) From e9ef03eadbca254bd5557899d1655f13954335dd Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Tue, 22 Oct 2024 23:49:07 +0530 Subject: [PATCH 02/11] Added type hints and minor case improvements --- machine_learning/ridge_regression.py | 51 ++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index d4d3162e5..65fb82318 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -3,7 +3,7 @@ import pandas as pd class RidgeRegression: - def __init__(self, alpha=0.001, lambda_=0.1, iterations=1000): + def __init__(self, alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000) -> None: """ Ridge Regression Constructor :param alpha: Learning rate for gradient descent @@ -15,24 +15,38 @@ class RidgeRegression: self.iterations = iterations self.theta = None - def feature_scaling(self, X): + def feature_scaling(self, features: np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]: """ - 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 + Normalize features to have mean 0 and standard deviation 1. + + :param features: Input features, shape (m, n) + :return: Tuple containing: + - Scaled features + - Mean of each feature + - Standard deviation of each feature + + Example: + >>> rr = RidgeRegression() + >>> features = np.array([[1, 2], [2, 3], [4, 6]]) + >>> scaled_features, mean, std = rr.feature_scaling(features) + >>> np.allclose(scaled_features.mean(axis=0), 0) + True + >>> np.allclose(scaled_features.std(axis=0), 1) + True """ - mean = np.mean(X, axis=0) - std = np.std(X, axis=0) + mean = np.mean(features, axis=0) + std = np.std(features, 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 - return X_scaled, mean, std + scaled_features = (features - mean) / std + return scaled_features, mean, std - def fit(self, X, y): + def fit(self, X: np.ndarray, y: np.ndarray) -> None: """ - Fit the Ridge Regression model to the training data + Fit the Ridge Regression model to the training data. + :param X: Input features, shape (m, n) :param y: Target values, shape (m,) """ @@ -48,18 +62,20 @@ class RidgeRegression: gradient = (X_scaled.T.dot(error) + self.lambda_ * self.theta) / m self.theta -= self.alpha * gradient # Update weights - def predict(self, X): + def predict(self, X: np.ndarray) -> np.ndarray: """ - Predict values using the trained model + 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 return X_scaled.dot(self.theta) - def compute_cost(self, X, y): + def compute_cost(self, X: np.ndarray, y: np.ndarray) -> float: """ - Compute the cost function with regularization + Compute the cost function with regularization. + :param X: Input features, shape (m, n) :param y: Target values, shape (m,) :return: Computed cost @@ -72,9 +88,10 @@ class RidgeRegression: ) * np.sum(self.theta**2) return cost - def mean_absolute_error(self, y_true, y_pred): + def mean_absolute_error(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: """ - Compute Mean Absolute Error (MAE) between true and predicted values + 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 From 861618ef11210d6dd4bdcba8671a20214b86801d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:19:50 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/ridge_regression.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 65fb82318..1d7e0c694 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -3,7 +3,9 @@ import pandas as pd class RidgeRegression: - def __init__(self, alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000) -> None: + def __init__( + self, alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000 + ) -> None: """ Ridge Regression Constructor :param alpha: Learning rate for gradient descent @@ -15,7 +17,9 @@ class RidgeRegression: self.iterations = iterations self.theta = None - def feature_scaling(self, features: np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + def feature_scaling( + self, features: np.ndarray + ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: """ Normalize features to have mean 0 and standard deviation 1. From 2dc60f475b7147b4fb54f2e534ac492f96a359f3 Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Tue, 22 Oct 2024 23:58:50 +0530 Subject: [PATCH 04/11] Resolved ruff checks --- machine_learning/ridge_regression.py | 44 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 1d7e0c694..cc60d2218 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -47,46 +47,46 @@ class RidgeRegression: scaled_features = (features - mean) / std return scaled_features, mean, std - def fit(self, X: np.ndarray, y: np.ndarray) -> None: + def fit(self, x: np.ndarray, y: np.ndarray) -> None: """ Fit the Ridge Regression model to the training data. - :param X: Input features, shape (m, n) + :param x: Input features, shape (m, n) :param y: Target values, shape (m,) """ - X_scaled, mean, std = self.feature_scaling(X) # Normalize features - m, n = X_scaled.shape + x_scaled, mean, std = self.feature_scaling(x) # Normalize features + 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) + for _ 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 + gradient = (x_scaled.T.dot(error) + self.lambda_ * self.theta) / m self.theta -= self.alpha * gradient # Update weights - def predict(self, X: np.ndarray) -> np.ndarray: + def predict(self, x: np.ndarray) -> np.ndarray: """ Predict values using the trained model. - :param X: Input features, shape (m, n) + :param x: Input features, shape (m, n) :return: Predicted values, shape (m,) """ - X_scaled, _, _ = self.feature_scaling(X) # Scale features using training data - return X_scaled.dot(self.theta) + x_scaled, _, _ = self.feature_scaling(x) # Scale features using training data + return x_scaled.dot(self.theta) - def compute_cost(self, X: np.ndarray, y: np.ndarray) -> float: + def compute_cost(self, x: np.ndarray, y: np.ndarray) -> float: """ Compute the cost function with regularization. - :param X: Input features, shape (m, n) + :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 + x_scaled, _, _ = self.feature_scaling(x) # Scale features using training data m = len(y) - predictions = X_scaled.dot(self.theta) + predictions = x_scaled.dot(self.theta) cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + ( self.lambda_ / (2 * m) ) * np.sum(self.theta**2) @@ -106,24 +106,24 @@ class RidgeRegression: # Example usage if __name__ == "__main__": # Load dataset - df = pd.read_csv( + data = 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 + x = data[["Rating"]].to_numpy() # Feature: Rating + y = data["ADR"].to_numpy() # 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 + 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) + model.fit(x, y) # Predictions - predictions = model.predict(X) + predictions = model.predict(x) # Results print("Optimized Weights:", model.theta) - print("Cost:", model.compute_cost(X, y)) + print("Cost:", model.compute_cost(x, y)) print("Mean Absolute Error:", model.mean_absolute_error(y, predictions)) From 61945d03c674ebca5f98ccb9c16fb384620f2b47 Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Wed, 23 Oct 2024 00:08:52 +0530 Subject: [PATCH 05/11] Added doctests --- machine_learning/ridge_regression.py | 76 ++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 21 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index cc60d2218..02a48f360 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -47,47 +47,73 @@ class RidgeRegression: scaled_features = (features - mean) / std return scaled_features, mean, std - def fit(self, x: np.ndarray, y: np.ndarray) -> None: + def fit(self, features: np.ndarray, target: np.ndarray) -> None: """ Fit the Ridge Regression model to the training data. - :param x: Input features, shape (m, n) - :param y: Target values, shape (m,) + :param features: Input features, shape (m, n) + :param target: Target values, shape (m,) + + Example: + >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) + >>> features = np.array([[1, 2], [2, 3], [4, 6]]) + >>> target = np.array([1, 2, 3]) + >>> rr.fit(features, target) + >>> rr.theta is not None + True """ - x_scaled, mean, std = self.feature_scaling(x) # Normalize features - m, n = x_scaled.shape + features_scaled, mean, std = self.feature_scaling(features) # Normalize features + m, n = features_scaled.shape self.theta = np.zeros(n) # Initialize weights to zeros - for _ in range(self.iterations): - predictions = x_scaled.dot(self.theta) - error = predictions - y + for i in range(self.iterations): + predictions = features_scaled.dot(self.theta) + error = predictions - target # Compute gradient with L2 regularization - gradient = (x_scaled.T.dot(error) + self.lambda_ * self.theta) / m + gradient = (features_scaled.T.dot(error) + self.lambda_ * self.theta) / m self.theta -= self.alpha * gradient # Update weights - def predict(self, x: np.ndarray) -> np.ndarray: + def predict(self, features: np.ndarray) -> np.ndarray: """ Predict values using the trained model. - :param x: Input features, shape (m, n) + :param features: Input features, shape (m, n) :return: Predicted values, shape (m,) - """ - x_scaled, _, _ = self.feature_scaling(x) # Scale features using training data - return x_scaled.dot(self.theta) - def compute_cost(self, x: np.ndarray, y: np.ndarray) -> float: + Example: + >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) + >>> features = np.array([[1, 2], [2, 3], [4, 6]]) + >>> target = np.array([1, 2, 3]) + >>> rr.fit(features, target) + >>> predictions = rr.predict(features) + >>> predictions.shape == target.shape + True + """ + features_scaled, _, _ = self.feature_scaling(features) # Scale features using training data + return features_scaled.dot(self.theta) + + def compute_cost(self, features: np.ndarray, target: np.ndarray) -> float: """ Compute the cost function with regularization. - :param x: Input features, shape (m, n) - :param y: Target values, shape (m,) + :param features: Input features, shape (m, n) + :param target: Target values, shape (m,) :return: Computed cost + + Example: + >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) + >>> features = np.array([[1, 2], [2, 3], [4, 6]]) + >>> target = np.array([1, 2, 3]) + >>> rr.fit(features, target) + >>> cost = rr.compute_cost(features, target) + >>> isinstance(cost, float) + True """ - x_scaled, _, _ = self.feature_scaling(x) # Scale features using training data - m = len(y) - predictions = x_scaled.dot(self.theta) - cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + ( + features_scaled, _, _ = self.feature_scaling(features) # Scale features using training data + m = len(target) + predictions = features_scaled.dot(self.theta) + cost = (1 / (2 * m)) * np.sum((predictions - target) ** 2) + ( self.lambda_ / (2 * m) ) * np.sum(self.theta**2) return cost @@ -99,6 +125,14 @@ class RidgeRegression: :param y_true: Actual target values, shape (m,) :param y_pred: Predicted target values, shape (m,) :return: MAE + + Example: + >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) + >>> y_true = np.array([1, 2, 3]) + >>> y_pred = np.array([1.1, 2.1, 2.9]) + >>> mae = rr.mean_absolute_error(y_true, y_pred) + >>> isinstance(mae, float) + True """ return np.mean(np.abs(y_true - y_pred)) From a2d07af8c1f005a60c31ff002c05a48d81d13ddf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:39:20 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/ridge_regression.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 02a48f360..0cd32caeb 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -62,7 +62,9 @@ class RidgeRegression: >>> rr.theta is not None True """ - features_scaled, mean, std = self.feature_scaling(features) # Normalize features + features_scaled, mean, std = self.feature_scaling( + features + ) # Normalize features m, n = features_scaled.shape self.theta = np.zeros(n) # Initialize weights to zeros @@ -90,9 +92,11 @@ class RidgeRegression: >>> predictions.shape == target.shape True """ - features_scaled, _, _ = self.feature_scaling(features) # Scale features using training data + features_scaled, _, _ = self.feature_scaling( + features + ) # Scale features using training data return features_scaled.dot(self.theta) - + def compute_cost(self, features: np.ndarray, target: np.ndarray) -> float: """ Compute the cost function with regularization. @@ -110,7 +114,9 @@ class RidgeRegression: >>> isinstance(cost, float) True """ - features_scaled, _, _ = self.feature_scaling(features) # Scale features using training data + features_scaled, _, _ = self.feature_scaling( + features + ) # Scale features using training data m = len(target) predictions = features_scaled.dot(self.theta) cost = (1 / (2 * m)) * np.sum((predictions - target) ** 2) + ( From 8f1f091aa4db5a1ca8f8e2dfd0a7f6caf5d56b11 Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Wed, 23 Oct 2024 00:14:37 +0530 Subject: [PATCH 07/11] Resolved ruff checks --- machine_learning/ridge_regression.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 0cd32caeb..1206d41b5 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -68,7 +68,7 @@ class RidgeRegression: m, n = features_scaled.shape self.theta = np.zeros(n) # Initialize weights to zeros - for i in range(self.iterations): + for _ in range(self.iterations): predictions = features_scaled.dot(self.theta) error = predictions - target @@ -149,21 +149,21 @@ if __name__ == "__main__": data = pd.read_csv( "https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/master/Week1/ADRvsRating.csv" ) - x = data[["Rating"]].to_numpy() # Feature: Rating - y = data["ADR"].to_numpy() # Target: ADR - y = (y - np.mean(y)) / np.std(y) + data_x = data[["Rating"]].to_numpy() # Feature: Rating + data_y = data["ADR"].to_numpy() # Target: ADR + data_y = (data_y - np.mean(data_y)) / np.std(data_y) # Add bias term (intercept) to the feature matrix - x = np.c_[np.ones(X.shape[0]), x] # Add intercept term + data_x = np.c_[np.ones(data_x.shape[0]), data_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) + model.fit(data_x, data_y) # Predictions - predictions = model.predict(x) + predictions = model.predict(data_x) # Results print("Optimized Weights:", model.theta) - print("Cost:", model.compute_cost(x, y)) - print("Mean Absolute Error:", model.mean_absolute_error(y, predictions)) + print("Cost:", model.compute_cost(data_x, data_y)) + print("Mean Absolute Error:", model.mean_absolute_error(data_y, predictions)) From 5bf9b854b466525fe0f1aae60422c903715ef61d Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Wed, 23 Oct 2024 00:20:29 +0530 Subject: [PATCH 08/11] Resolved mypy checks --- machine_learning/ridge_regression.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 1206d41b5..ad34600b3 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -1,5 +1,6 @@ import numpy as np import pandas as pd +from typing import Optional, Tuple class RidgeRegression: @@ -15,7 +16,7 @@ class RidgeRegression: self.alpha = alpha self.lambda_ = lambda_ self.iterations = iterations - self.theta = None + self.theta: Optional[np.ndarray] = None # Initialize as None, later will be ndarray def feature_scaling( self, features: np.ndarray @@ -92,6 +93,9 @@ class RidgeRegression: >>> predictions.shape == target.shape True """ + if self.theta is None: + raise ValueError("Model is not trained yet. Call the `fit` method first.") + features_scaled, _, _ = self.feature_scaling( features ) # Scale features using training data @@ -114,6 +118,9 @@ class RidgeRegression: >>> isinstance(cost, float) True """ + if self.theta is None: + raise ValueError("Model is not trained yet. Call the `fit` method first.") + features_scaled, _, _ = self.feature_scaling( features ) # Scale features using training data From 85020a76c28127b7aa4f0e923a0f3dcd284df915 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:51:19 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/ridge_regression.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index ad34600b3..20324e987 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -16,7 +16,9 @@ class RidgeRegression: self.alpha = alpha self.lambda_ = lambda_ self.iterations = iterations - self.theta: Optional[np.ndarray] = None # Initialize as None, later will be ndarray + self.theta: Optional[np.ndarray] = ( + None # Initialize as None, later will be ndarray + ) def feature_scaling( self, features: np.ndarray @@ -95,7 +97,7 @@ class RidgeRegression: """ if self.theta is None: raise ValueError("Model is not trained yet. Call the `fit` method first.") - + features_scaled, _, _ = self.feature_scaling( features ) # Scale features using training data @@ -120,7 +122,7 @@ class RidgeRegression: """ if self.theta is None: raise ValueError("Model is not trained yet. Call the `fit` method first.") - + features_scaled, _, _ = self.feature_scaling( features ) # Scale features using training data From 52345d90138fb5f598d30db43bab0d2080268b98 Mon Sep 17 00:00:00 2001 From: Harmanaya Sharma Date: Wed, 23 Oct 2024 00:28:46 +0530 Subject: [PATCH 10/11] Resolved ruff checks --- machine_learning/ridge_regression.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 20324e987..32c76a90c 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -1,7 +1,5 @@ import numpy as np import pandas as pd -from typing import Optional, Tuple - class RidgeRegression: def __init__( @@ -16,9 +14,7 @@ class RidgeRegression: self.alpha = alpha self.lambda_ = lambda_ self.iterations = iterations - self.theta: Optional[np.ndarray] = ( - None # Initialize as None, later will be ndarray - ) + self.theta: np.ndarray | None = None # Initialize as None, later will be ndarray def feature_scaling( self, features: np.ndarray From 4204bf6d280bf45b476cebe56726a8b0f2b76fa1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:59:33 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/ridge_regression.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/ridge_regression.py b/machine_learning/ridge_regression.py index 32c76a90c..3976cf8a7 100644 --- a/machine_learning/ridge_regression.py +++ b/machine_learning/ridge_regression.py @@ -1,6 +1,7 @@ import numpy as np import pandas as pd + class RidgeRegression: def __init__( self, alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000 @@ -14,7 +15,9 @@ class RidgeRegression: self.alpha = alpha self.lambda_ = lambda_ self.iterations = iterations - self.theta: np.ndarray | None = None # Initialize as None, later will be ndarray + self.theta: np.ndarray | None = ( + None # Initialize as None, later will be ndarray + ) def feature_scaling( self, features: np.ndarray