From aa33cc2cef1798354f9248b26db7b1e67fda4465 Mon Sep 17 00:00:00 2001 From: dineshsuthar31 Date: Thu, 7 May 2026 12:05:23 +0530 Subject: [PATCH 1/5] Add visualization support for linear regression --- machine_learning/linear_regression.py | 61 +++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 5b1e663116cc..41295a6f879c 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -13,12 +13,13 @@ # dependencies = [ # "httpx", # "numpy", +# "matplotlib", # ] # /// import httpx import numpy as np - +import matplotlib.pyplot as plt def collect_dataset(): """Collect dataset of CSGO @@ -102,12 +103,17 @@ def run_linear_regression(data_x, data_y): theta = np.zeros((1, no_features)) + err = [] + for i in range(iterations): theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) - print(f"At Iteration {i + 1} - Error is {error:.5f}") + err.append(error) + + if i % 1000 == 0: + print(f"At Iteration {i + 1} - Error is {error:.5f}") - return theta + return theta, err def mean_absolute_error(predicted_y, original_y): @@ -125,6 +131,45 @@ def mean_absolute_error(predicted_y, original_y): return total / len(original_y) + +# visulization +def plot_regression(data_x, data_y, theta): + """ + Plot regression line with dataset points + """ + + x = np.array(data_x[:, 1]).flatten() + y = np.array(data_y).flatten() + + predictions = theta[0, 0] + theta[0, 1] * x + + plt.scatter(x, y) + + plt.plot(x, predictions) + + plt.xlabel("ADR") + plt.ylabel("Rating") + + plt.title("Linear Regression Best Fit") + + plt.show() + + +def plot_loss(err): + """ + Plot training loss curve + """ + + plt.plot(err) + + plt.xlabel("Iterations") + plt.ylabel("Loss") + + plt.title("Training Loss Curve") + + plt.show() + + def main(): """Driver function""" data = collect_dataset() @@ -133,7 +178,11 @@ def main(): data_x = np.c_[np.ones(len_data), data[:, :-1]].astype(float) data_y = data[:, -1].astype(float) - theta = run_linear_regression(data_x, data_y) + theta,err = run_linear_regression(data_x, data_y) + + plot_regression(data_x, data_y, theta) + plt_loss(err) + len_result = theta.shape[1] print("Resultant Feature vector : ") for i in range(len_result): @@ -145,3 +194,7 @@ def main(): doctest.testmod() main() + + + + From 8fd53296f0d86661dd9b319c4b74d8ffae858507 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 06:42:29 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/linear_regression.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 41295a6f879c..ce15d5526782 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -21,6 +21,7 @@ import numpy as np import matplotlib.pyplot as plt + def collect_dataset(): """Collect dataset of CSGO The dataset contains ADR vs Rating of a Player @@ -109,7 +110,7 @@ def run_linear_regression(data_x, data_y): theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) err.append(error) - + if i % 1000 == 0: print(f"At Iteration {i + 1} - Error is {error:.5f}") @@ -131,8 +132,7 @@ def mean_absolute_error(predicted_y, original_y): return total / len(original_y) - -# visulization +# visulization def plot_regression(data_x, data_y, theta): """ Plot regression line with dataset points @@ -178,7 +178,7 @@ def main(): data_x = np.c_[np.ones(len_data), data[:, :-1]].astype(float) data_y = data[:, -1].astype(float) - theta,err = run_linear_regression(data_x, data_y) + theta, err = run_linear_regression(data_x, data_y) plot_regression(data_x, data_y, theta) plt_loss(err) @@ -194,7 +194,3 @@ def main(): doctest.testmod() main() - - - - From ceddd50dd74700fd817a3c34819eb7d7bd3ccfe8 Mon Sep 17 00:00:00 2001 From: dineshsuthar31 Date: Thu, 7 May 2026 13:02:46 +0530 Subject: [PATCH 3/5] Fix linting and plotting issues --- machine_learning/linear_regression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index ce15d5526782..cf5af298a0d2 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -17,9 +17,9 @@ # ] # /// +import matplotlib.pyplot as plt import httpx import numpy as np -import matplotlib.pyplot as plt def collect_dataset(): @@ -181,7 +181,7 @@ def main(): theta, err = run_linear_regression(data_x, data_y) plot_regression(data_x, data_y, theta) - plt_loss(err) + plot_loss(err) len_result = theta.shape[1] print("Resultant Feature vector : ") From e11bf0f58a0770fc2739755353bc63b898f6ce2a Mon Sep 17 00:00:00 2001 From: dineshsuthar31 Date: Thu, 7 May 2026 13:32:49 +0530 Subject: [PATCH 4/5] Apply ruff auto fixes --- machine_learning/linear_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index cf5af298a0d2..f7353bd9ac5d 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -17,8 +17,8 @@ # ] # /// -import matplotlib.pyplot as plt import httpx +import matplotlib.pyplot as plt import numpy as np From 0c95a166157d0c5ef4e5bf56a944d0a15a6027dc Mon Sep 17 00:00:00 2001 From: dineshsuthar31 Date: Thu, 7 May 2026 13:43:49 +0530 Subject: [PATCH 5/5] Fix spelling issue --- machine_learning/linear_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index f7353bd9ac5d..c1715f554fd1 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -132,7 +132,7 @@ def mean_absolute_error(predicted_y, original_y): return total / len(original_y) -# visulization +# visualization def plot_regression(data_x, data_y, theta): """ Plot regression line with dataset points