-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Added Ridge Regression To Machine Learning #12108 #12126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Akshat-Raii
wants to merge
5
commits into
TheAlgorithms:master
from
Akshat-Raii:akshat_ridgeRegression
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c300b02
Added Ridge Regression To Machine Learning
Akshat-Raii 154944c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0fec92b
Add Ridge Regression To Machine Learning
Akshat-Raii 1cae427
Add Ridge Regression To Machine Learning
Akshat-Raii 46dabe0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
""" | ||
Ridge Regression with L2 Regularization using Gradient Descent. | ||
|
||
Ridge Regression is a type of linear regression that includes an L2 regularization | ||
term to prevent overfitting and improve generalization. It is commonly used when | ||
multicollinearity is present in the data. | ||
|
||
More on Ridge Regression: https://en.wikipedia.org/wiki/Tikhonov_regularization | ||
""" | ||
|
||
from typing import Tuple | ||
import numpy as np | ||
import pandas as pd | ||
|
||
def load_data(file_path: str) -> Tuple[np.ndarray, np.ndarray]: | ||
Check failure on line 15 in machine_learning/ridge_regression.py
|
||
""" | ||
Load data from a CSV file and return features and target arrays. | ||
|
||
Args: | ||
file_path: Path to the CSV file. | ||
|
||
Returns: | ||
A tuple containing features (X) and target (y) as numpy arrays. | ||
|
||
Example: | ||
>>> data = pd.DataFrame({'ADR': [200, 220], 'Rating': [1.2, 1.4]}) | ||
>>> data.to_csv('sample.csv', index=False) | ||
>>> X, y = load_data('sample.csv') | ||
>>> X.shape == (2, 1) and y.shape == (2,) | ||
True | ||
""" | ||
data = pd.read_csv(file_path) | ||
X = data[['Rating']].to_numpy() # Use .to_numpy() instead of .values (PD011) | ||
y = data['ADR'].to_numpy() | ||
return X, y | ||
|
||
def ridge_gradient_descent( | ||
X: np.ndarray, y: np.ndarray, reg_lambda: float, learning_rate: float, | ||
num_iters: int = 1000 | ||
) -> np.ndarray: | ||
""" | ||
Perform Ridge Regression using gradient descent. | ||
|
||
Args: | ||
X: Feature matrix. | ||
y: Target vector. | ||
reg_lambda: Regularization parameter (lambda). | ||
learning_rate: Learning rate for gradient descent. | ||
num_iters: Number of iterations for gradient descent. | ||
|
||
Returns: | ||
Optimized weights (coefficients) for predicting ADR from Rating. | ||
|
||
Example: | ||
>>> X = np.array([[1.2], [1.4]]) | ||
>>> y = np.array([200, 220]) | ||
>>> ridge_gradient_descent(X, y, reg_lambda=0.1, learning_rate=0.01).shape == (1,) | ||
True | ||
""" | ||
weights = np.zeros(X.shape[1]) | ||
m = len(y) | ||
|
||
for _ in range(num_iters): | ||
predictions = X @ weights | ||
error = predictions - y | ||
gradient = (X.T @ error + reg_lambda * weights) / m | ||
weights -= learning_rate * gradient | ||
|
||
return weights | ||
|
||
def mean_absolute_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
Calculate the Mean Absolute Error (MAE) between true and predicted values. | ||
|
||
Args: | ||
y_true: Actual values. | ||
y_pred: Predicted values. | ||
|
||
Returns: | ||
Mean absolute error. | ||
|
||
Example: | ||
>>> mean_absolute_error(np.array([200, 220]), np.array([205, 215])) | ||
5.0 | ||
""" | ||
return np.mean(np.abs(y_true - y_pred)) | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
|
||
# Load the data | ||
X, y = load_data("sample.csv") | ||
|
||
# Fit the Ridge Regression model | ||
optimized_weights = ridge_gradient_descent(X, y, reg_lambda=0.1, learning_rate=0.01) | ||
|
||
# Make predictions | ||
y_pred = X @ optimized_weights | ||
|
||
# Calculate Mean Absolute Error | ||
mae = mean_absolute_error(y, y_pred) | ||
print("Optimized Weights:", optimized_weights) | ||
print("Mean Absolute Error:", mae) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
X
Please provide descriptive name for the parameter:
y