-
-
Notifications
You must be signed in to change notification settings - Fork 47.8k
Add Ridge Regression with L2 Regularization using Gradient Descent #12844
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
Open
Nitin-Prata
wants to merge
5
commits into
TheAlgorithms:master
from
Nitin-Prata:add-ridge-regression
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e621c2
✅ Add doctests to string conversion functions
Nitin-Prata be1332d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 607630b
fix: remove whitespace from blank line (ruff W293)
Nitin-Prata c03f00c
feat: add Ridge Regression with L2 regularization
Nitin-Prata 0c155c3
[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
127 changes: 127 additions & 0 deletions
machine_learning/ridge_regression.py
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,127 @@ | ||
""" | ||
Ridge Regression using Gradient Descent. | ||
|
||
This script implements Ridge Regression (L2 regularization) using gradient descent. | ||
It predicts Average Damage per Round (ADR) using player ratings. | ||
|
||
Author: Nitin Pratap Singh | ||
""" | ||
|
||
import numpy as np | ||
import httpx | ||
|
||
|
||
def collect_dataset(): | ||
""" | ||
Collects CSGO dataset from a remote CSV file. | ||
|
||
The CSV contains ADR vs Rating of players. | ||
|
||
:return: Numpy array of shape (n_samples, 2) | ||
|
||
>>> data = collect_dataset() | ||
>>> data.shape[1] | ||
2 | ||
""" | ||
response = httpx.get( | ||
"https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/" | ||
"master/Week1/ADRvsRating.csv", | ||
timeout=10, | ||
) | ||
lines = response.text.splitlines() | ||
data = [line.split(",") for line in lines] | ||
data.pop(0) # Remove header | ||
dataset = np.array(data, dtype=float) | ||
return dataset | ||
|
||
|
||
def ridge_cost_function(X, y, theta, lam): | ||
""" | ||
Computes the cost for Ridge Regression (L2 regularization). | ||
|
||
:param X: Feature matrix (n_samples, n_features) | ||
:param y: Target vector (n_samples,) | ||
:param theta: Coefficients (n_features,) | ||
:param lam: Regularization strength (lambda) | ||
:return: Cost value (float) | ||
|
||
>>> X = np.array([[1, 1], [1, 2]]) | ||
>>> y = np.array([1, 2]) | ||
>>> theta = np.zeros(2) | ||
>>> round(ridge_cost_function(X, y, theta, 0.1), 2) | ||
1.25 | ||
""" | ||
m = len(y) | ||
predictions = X @ theta | ||
error = predictions - y | ||
cost = (1 / (2 * m)) * np.dot(error, error) | ||
reg_cost = (lam / (2 * m)) * np.dot(theta[1:], theta[1:]) | ||
return cost + reg_cost | ||
|
||
|
||
def ridge_gradient_descent(X, y, theta, alpha, iterations, lam, verbose=True): | ||
""" | ||
Performs gradient descent with L2 regularization. | ||
|
||
:param X: Feature matrix (n_samples, n_features) | ||
:param y: Target values (n_samples,) | ||
:param theta: Initial weights (n_features,) | ||
:param alpha: Learning rate (float) | ||
:param iterations: Number of iterations (int) | ||
:param lam: Regularization strength (lambda) | ||
:param verbose: Print cost every 10,000 steps if True | ||
:return: Optimized weights (n_features,) | ||
|
||
>>> X = np.array([[1, 1], [1, 2]]) | ||
>>> y = np.array([1, 2]) | ||
>>> theta = np.zeros(2) | ||
>>> final_theta = ridge_gradient_descent(X, y, theta, 0.1, 10, 0.01, verbose=False) | ||
>>> len(final_theta) | ||
2 | ||
""" | ||
m = len(y) | ||
for i in range(iterations): | ||
predictions = X @ theta | ||
error = predictions - y | ||
gradient = (1 / m) * (X.T @ error) | ||
reg_term = (lam / m) * theta | ||
reg_term[0] = 0 # Do not regularize the bias term | ||
theta = theta - alpha * (gradient + reg_term) | ||
|
||
if i % 10000 == 0 and verbose: | ||
cost = ridge_cost_function(X, y, theta, lam) | ||
print(f"Iteration {i}: Cost = {cost:.5f}") | ||
|
||
return theta | ||
|
||
|
||
def main(): | ||
""" | ||
Driver function for running Ridge Regression | ||
""" | ||
data = collect_dataset() | ||
|
||
# Normalize feature column to avoid overflow | ||
feature = data[:, 0] | ||
feature = (feature - feature.mean()) / feature.std() | ||
|
||
X = np.c_[np.ones(data.shape[0]), feature] # Add bias term | ||
y = data[:, 1] | ||
|
||
theta = np.zeros(X.shape[1]) | ||
alpha = 0.001 # Lowered learning rate | ||
iterations = 100000 | ||
lam = 0.1 # Regularization strength | ||
|
||
final_theta = ridge_gradient_descent(X, y, theta, alpha, iterations, lam) | ||
|
||
print("\nOptimized weights (theta):") | ||
for i, value in enumerate(final_theta): | ||
print(f"θ{i}: {value:.5f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
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
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.