From aa33cc2cef1798354f9248b26db7b1e67fda4465 Mon Sep 17 00:00:00 2001 From: dineshsuthar31 Date: Thu, 7 May 2026 12:05:23 +0530 Subject: [PATCH 1/6] 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/6] [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/6] 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/6] 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/6] 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 From a70313d2d363a89e14f6e0d89ba439ff8c4eea99 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 16 Sep 2026 01:03:40 +0200 Subject: [PATCH 6/6] Update linear_regression.py --- machine_learning/linear_regression.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index c1715f554fd1..cbc1f9f554de 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -1,23 +1,23 @@ """ Linear regression is the most basic type of regression commonly used for -predictive analysis. The idea is pretty simple: we have a dataset and we have +predictive analysis. The idea is pretty simple: we have a dataset, and we have features associated with it. Features should be chosen very cautiously as they determine how much our model will be able to make future predictions. We try to set the weight of these features, over many iterations, so that they best -fit our dataset. In this particular code, I had used a CSGO dataset (ADR vs -Rating). We try to best fit a line through dataset and estimate the parameters. +fit our dataset. In this particular code, I used a CSGO dataset (ADR vs +Rating). We try to best fit a line through the dataset and estimate the parameters. """ # /// script # requires-python = ">=3.13" # dependencies = [ -# "httpx", +# "httpx2", # "numpy", # "matplotlib", # ] # /// -import httpx +import httpx2 import matplotlib.pyplot as plt import numpy as np @@ -25,9 +25,9 @@ def collect_dataset(): """Collect dataset of CSGO The dataset contains ADR vs Rating of a Player - :return : dataset obtained from the link, as matrix + :return : dataset obtained from the link, as a matrix """ - response = httpx.get( + response = httpx2.get( "https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/" "master/Week1/ADRvsRating.csv", timeout=10, @@ -43,13 +43,13 @@ def collect_dataset(): def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): - """Run steep gradient descent and updates the Feature vector accordingly_ + """Run steep gradient descent and update the Feature vector accordingly_ :param data_x : contains the dataset :param data_y : contains the output associated with each data-entry :param len_data : length of the data_ :param alpha : Learning rate of the model - :param theta : Feature vector (weight's for our model) - ;param return : Updated Feature's, using + :param theta : Feature vector (weights for our model) + ;param return : Updated features, using curr_features - alpha_ * gradient(w.r.t. feature) >>> import numpy as np >>> data_x = np.array([[1, 2], [3, 4]]) @@ -70,7 +70,7 @@ def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): def sum_of_square_error(data_x, data_y, len_data, theta): - """Return sum of square error for error calculation + """Return the sum of square error for error calculation :param data_x : contains our dataset :param data_y : contains the output (result vector) :param len_data : len of the dataset @@ -121,7 +121,7 @@ def mean_absolute_error(predicted_y, original_y): """Return sum of square error for error calculation :param predicted_y : contains the output of prediction (result vector) :param original_y : contains values of expected outcome - :return : mean absolute error computed from given feature's + :return : mean absolute error computed from given features >>> predicted_y = [3, -0.5, 2, 7] >>> original_y = [2.5, 0.0, 2, 8]