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
54 changes: 54 additions & 0 deletions machine_learning/01_linear_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np

Check failure on line 1 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/01_linear_regression.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

machine_learning/01_linear_regression.py:1:1: N999 Invalid module name: '01_linear_regression'

# -------------------- Naive Linear Regression --------------------
def naive_linear_regression(X, y, learning_rate=0.01, epochs=1000):

Check failure on line 4 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/01_linear_regression.py:4:29: N803 Argument name `X` should be lowercase
"""
Naive Linear Regression using loops.
X: input features (2D array)
y: target values (column vector)
"""
m, n = X.shape
theta = np.zeros((n, 1)) # initialize parameters

for _ in range(epochs):
predictions = []
for i in range(m):
pred = 0
for j in range(n):
pred += X[i][j] * theta[j][0]
predictions.append([pred])
predictions = np.array(predictions)
# compute gradient
errors = predictions - y
for j in range(n):
grad = 0
for i in range(m):
grad += errors[i][0] * X[i][j]
theta[j][0] -= learning_rate * grad / m
return theta

# -------------------- Vectorized Linear Regression --------------------
def vectorized_linear_regression(X, y, learning_rate=0.01, epochs=1000):

Check failure on line 31 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

machine_learning/01_linear_regression.py:31:34: N803 Argument name `X` should be lowercase
"""
Fully vectorized Linear Regression using matrix operations.
"""
m, n = X.shape
theta = np.zeros((n, 1))
for _ in range(epochs):
predictions = X.dot(theta)
errors = predictions - y
gradient = (X.T.dot(errors)) / m
theta -= learning_rate * gradient
return theta

# -------------------- Test Both Implementations --------------------
if __name__ == "__main__":
# Sample dataset
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.dot(X, np.array([[1],[2]])) + 3 # y = 1*x1 + 2*x2 + 3

theta_naive = naive_linear_regression(X, y)
theta_vec = vectorized_linear_regression(X, y)

print("Theta naive:\n", theta_naive)
print("Theta vectorized:\n", theta_vec)
Loading