Skip to content
Open
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
57 changes: 57 additions & 0 deletions machine_learning/01_linear_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 (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 5 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (N803)

machine_learning/01_linear_regression.py:5:29: N803 Argument name `X` should be lowercase

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/01_linear_regression.py, please provide doctest for the function naive_linear_regression

Please provide descriptive name for the parameter: X

Please provide descriptive name for the parameter: y

Please provide return type hint for the function: naive_linear_regression. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide type hint for the parameter: y

Please provide type hint for the parameter: learning_rate

Please provide type hint for the parameter: epochs

"""
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 33 in machine_learning/01_linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (N803)

machine_learning/01_linear_regression.py:33:34: N803 Argument name `X` should be lowercase

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/01_linear_regression.py, please provide doctest for the function vectorized_linear_regression

Please provide descriptive name for the parameter: X

Please provide descriptive name for the parameter: y

Please provide return type hint for the function: vectorized_linear_regression. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide type hint for the parameter: y

Please provide type hint for the parameter: learning_rate

Please provide type hint for the parameter: epochs

"""
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