Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions machine_learning/mini_batch_gradient_descent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Mini-Batch Gradient Descent : https://en.wikipedia.org/wiki/Stochastic_gradient_descent
Mini-batch gradient descent is an optimization method for training models
by splitting the data into small batches.
"""

import numpy as np


def mini_batch_gradient_descent(
X: np.ndarray, y: np.ndarray, lr: float = 0.01, batch_size: int = 16, n_epochs: int = 50

Check failure on line 11 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/mini_batch_gradient_descent.py:11:89: E501 Line too long (92 > 88)

Check failure on line 11 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/mini_batch_gradient_descent.py:11:5: N803 Argument name `X` should be lowercase

Check failure on line 11 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/mini_batch_gradient_descent.py:11:89: E501 Line too long (92 > 88)

Check failure on line 11 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/mini_batch_gradient_descent.py:11:5: N803 Argument name `X` should be lowercase
):
"""
Mini-Batch Gradient Descent for linear regression.

Parameters
----------
X : np.ndarray
Feature matrix.
y : np.ndarray
Target values.
lr : float
Learning rate.
batch_size : int
Size of mini-batches.
n_epochs : int
Number of training epochs.

Returns
-------
weights : np.ndarray
Learned weights.
bias : float
Learned bias.

Example
-------
>>> import numpy as np
>>> X = np.array([[1],[2],[3],[4]])
>>> y = np.array([2,4,6,8])
>>> w, b = mini_batch_gradient_descent(X, y, lr=0.1, batch_size=2, n_epochs=100)
>>> round(w[0], 1) # slope close to 2
2.0
"""
n_samples, n_features = X.shape
weights = np.zeros(n_features)
bias = 0

for _ in range(n_epochs):
indices = np.random.permutation(n_samples)

Check failure on line 50 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

machine_learning/mini_batch_gradient_descent.py:50:19: NPY002 Replace legacy `np.random.permutation` call with `np.random.Generator`

Check failure on line 50 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

machine_learning/mini_batch_gradient_descent.py:50:19: NPY002 Replace legacy `np.random.permutation` call with `np.random.Generator`
X_shuffled, y_shuffled = X[indices], y[indices]

Check failure on line 51 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

machine_learning/mini_batch_gradient_descent.py:51:9: N806 Variable `X_shuffled` in function should be lowercase

Check failure on line 51 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

machine_learning/mini_batch_gradient_descent.py:51:9: N806 Variable `X_shuffled` in function should be lowercase
for start in range(0, n_samples, batch_size):
end = start + batch_size
X_batch, y_batch = X_shuffled[start:end], y_shuffled[start:end]

Check failure on line 54 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

machine_learning/mini_batch_gradient_descent.py:54:13: N806 Variable `X_batch` in function should be lowercase

Check failure on line 54 in machine_learning/mini_batch_gradient_descent.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

machine_learning/mini_batch_gradient_descent.py:54:13: N806 Variable `X_batch` in function should be lowercase
y_pred = np.dot(X_batch, weights) + bias
error = y_pred - y_batch
weights -= lr * (X_batch.T @ error) / len(y_batch)
bias -= lr * np.mean(error)
return weights, bias


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading