From 7ea2f6625e6cbc1050bc3394ee481e6f8d484d15 Mon Sep 17 00:00:00 2001 From: Ruhanika Date: Tue, 7 Oct 2025 11:03:45 +0530 Subject: [PATCH 1/2] enhancement --- machine_learning/01_linear_regression.py | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 machine_learning/01_linear_regression.py diff --git a/machine_learning/01_linear_regression.py b/machine_learning/01_linear_regression.py new file mode 100644 index 000000000000..f74f649d1054 --- /dev/null +++ b/machine_learning/01_linear_regression.py @@ -0,0 +1,54 @@ +import numpy as np + +# -------------------- Naive Linear Regression -------------------- +def naive_linear_regression(X, y, learning_rate=0.01, epochs=1000): + """ + 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): + """ + 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) From cb783fe205d526247d1c8ca46493690c4b515746 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 17:18:02 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/01_linear_regression.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/01_linear_regression.py b/machine_learning/01_linear_regression.py index f74f649d1054..616c66df5352 100644 --- a/machine_learning/01_linear_regression.py +++ b/machine_learning/01_linear_regression.py @@ -1,5 +1,6 @@ import numpy as np + # -------------------- Naive Linear Regression -------------------- def naive_linear_regression(X, y, learning_rate=0.01, epochs=1000): """ @@ -27,6 +28,7 @@ def naive_linear_regression(X, y, learning_rate=0.01, epochs=1000): theta[j][0] -= learning_rate * grad / m return theta + # -------------------- Vectorized Linear Regression -------------------- def vectorized_linear_regression(X, y, learning_rate=0.01, epochs=1000): """ @@ -41,11 +43,12 @@ def vectorized_linear_regression(X, y, learning_rate=0.01, epochs=1000): 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 + 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)