From 50b557aa0503fb6f4a128bb481d01c829a7366ff Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sun, 22 Oct 2023 12:03:26 +0530 Subject: [PATCH 1/7] Add: Ordinary Least Squares Regression Algorithm --- DIRECTORY.md | 1 + .../ordinary_least_squares_regression.py | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 machine_learning/ordinary_least_squares_regression.py diff --git a/DIRECTORY.md b/DIRECTORY.md index d108acf8dcfb..96cc3f6b21cf 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -548,6 +548,7 @@ * [Loss Functions](machine_learning/loss_functions.py) * [Mfcc](machine_learning/mfcc.py) * [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py) + * [Ordinary Least Squares Regression](machine_learning/ordinary_least_squares_regression.py) * [Polynomial Regression](machine_learning/polynomial_regression.py) * [Scoring Functions](machine_learning/scoring_functions.py) * [Self Organizing Map](machine_learning/self_organizing_map.py) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py new file mode 100644 index 000000000000..c4df64da35dc --- /dev/null +++ b/machine_learning/ordinary_least_squares_regression.py @@ -0,0 +1,76 @@ +""" +Ordinary Least Squares Regression (OLSR): + +Ordinary Least Squares Regression (OLSR) is a statistical method for +estimating the parameters of a linear regression model. +It is the most commonly used regression method, +and it is based on the principle of minimizing +the sum of the squared residuals. + +Below is simple implementation of OLSR +without using any external libraries. + +WIKI: https://en.wikipedia.org/wiki/Ordinary_least_squares +""" + +import numpy as np + +def ols_regression(x, y): + """ + Performs Ordinary Least Squares Regression (OLSR) on the given data. + + Args: + x (numpy.ndarray): The independent variable. + y (numpy.ndarray): The dependent variable. + + Returns: + a (float): The intercept of the regression line. + b (float): The slope of the regression line. + + Examples: + >>> x = np.array([1, 2, 3, 4, 5]) + >>> y = np.array([2, 4, 6, 8, 10]) + >>> a, b = ols_regression(x, y) + >>> a # Intercept should be 0.0 + 0.0 + >>> round(b, 2) # Slope should be 2.0 + 2.0 + """ + + # Calculate the mean of the independent variable and + # the dependent variable. + x_mean = np.mean(x) + y_mean = np.mean(y) + + # Calculate the slope of the regression line. + b = np.sum((x - x_mean) * (y - y_mean)) / np.sum((x - x_mean)**2) + + # Calculate the intercept of the regression line. + a = y_mean - b * x_mean + + return a, b + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Load the data + x = np.array([1, 2, 3, 4, 5]) + y = np.array([2, 4, 6, 8, 10]) + + # Perform OLS regression + a, b = ols_regression(x, y) + + # Intercept (a) and slope (b) of the regression line + print('Intercept:', a) + print('Slope:', b) + + # Predict the target variable for a new data point with + # an independent variable value of 6 + x_new = 6 + + # Make a prediction + y_pred = a + b * x_new + + print('Prediction:', y_pred) From 36065cab8f00d702eeeccae6aa9d3479ddfd31c8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 06:39:46 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../ordinary_least_squares_regression.py | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index c4df64da35dc..e0d1453fc88d 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -1,13 +1,13 @@ """ Ordinary Least Squares Regression (OLSR): -Ordinary Least Squares Regression (OLSR) is a statistical method for -estimating the parameters of a linear regression model. -It is the most commonly used regression method, -and it is based on the principle of minimizing +Ordinary Least Squares Regression (OLSR) is a statistical method for +estimating the parameters of a linear regression model. +It is the most commonly used regression method, +and it is based on the principle of minimizing the sum of the squared residuals. -Below is simple implementation of OLSR +Below is simple implementation of OLSR without using any external libraries. WIKI: https://en.wikipedia.org/wiki/Ordinary_least_squares @@ -15,8 +15,9 @@ import numpy as np + def ols_regression(x, y): - """ + """ Performs Ordinary Least Squares Regression (OLSR) on the given data. Args: @@ -35,20 +36,21 @@ def ols_regression(x, y): 0.0 >>> round(b, 2) # Slope should be 2.0 2.0 - """ + """ + + # Calculate the mean of the independent variable and + # the dependent variable. + x_mean = np.mean(x) + y_mean = np.mean(y) - # Calculate the mean of the independent variable and - # the dependent variable. - x_mean = np.mean(x) - y_mean = np.mean(y) + # Calculate the slope of the regression line. + b = np.sum((x - x_mean) * (y - y_mean)) / np.sum((x - x_mean) ** 2) - # Calculate the slope of the regression line. - b = np.sum((x - x_mean) * (y - y_mean)) / np.sum((x - x_mean)**2) + # Calculate the intercept of the regression line. + a = y_mean - b * x_mean - # Calculate the intercept of the regression line. - a = y_mean - b * x_mean + return a, b - return a, b if __name__ == "__main__": import doctest @@ -63,14 +65,14 @@ def ols_regression(x, y): a, b = ols_regression(x, y) # Intercept (a) and slope (b) of the regression line - print('Intercept:', a) - print('Slope:', b) + print("Intercept:", a) + print("Slope:", b) - # Predict the target variable for a new data point with + # Predict the target variable for a new data point with # an independent variable value of 6 x_new = 6 # Make a prediction y_pred = a + b * x_new - print('Prediction:', y_pred) + print("Prediction:", y_pred) From 725031a1a8e22dc548b7e1d96bfffc90014e298f Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sun, 22 Oct 2023 12:14:43 +0530 Subject: [PATCH 3/7] fix: typos --- machine_learning/ordinary_least_squares_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index e0d1453fc88d..9f8051c2988a 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -16,7 +16,7 @@ import numpy as np -def ols_regression(x, y): +def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple: """ Performs Ordinary Least Squares Regression (OLSR) on the given data. From f8f63b14b96e6756dc01684a99ec59e383ecf7c4 Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sun, 22 Oct 2023 12:24:42 +0530 Subject: [PATCH 4/7] fix: descriptive names --- .../ordinary_least_squares_regression.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index 9f8051c2988a..74b968ad078f 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -16,7 +16,7 @@ import numpy as np -def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple: +def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple: """ Performs Ordinary Least Squares Regression (OLSR) on the given data. @@ -40,16 +40,16 @@ def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple: # Calculate the mean of the independent variable and # the dependent variable. - x_mean = np.mean(x) - y_mean = np.mean(y) + x_mean = np.mean(x_point) + y_mean = np.mean(y_point) # Calculate the slope of the regression line. - b = np.sum((x - x_mean) * (y - y_mean)) / np.sum((x - x_mean) ** 2) + slope = np.sum((x_point - x_mean) * (y_point - y_mean)) / np.sum((x_point - x_mean) ** 2) # Calculate the intercept of the regression line. - a = y_mean - b * x_mean + intercept = y_mean - slope * x_mean - return a, b + return intercept, slope if __name__ == "__main__": @@ -58,21 +58,21 @@ def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple: doctest.testmod() # Load the data - x = np.array([1, 2, 3, 4, 5]) - y = np.array([2, 4, 6, 8, 10]) + x_points = np.array([1, 2, 3, 4, 5]) + y_points = np.array([2, 4, 6, 8, 10]) # Perform OLS regression - a, b = ols_regression(x, y) + intercept, slope = ols_regression(x_points, y_points) # Intercept (a) and slope (b) of the regression line - print("Intercept:", a) - print("Slope:", b) + print("Intercept:", intercept) + print("Slope:", slope) # Predict the target variable for a new data point with # an independent variable value of 6 x_new = 6 # Make a prediction - y_pred = a + b * x_new + y_pred = intercept + slope * x_new print("Prediction:", y_pred) From eaca0d9e602d6b18feb0d99afaaea19189184292 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 06:55:24 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/ordinary_least_squares_regression.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index 74b968ad078f..ce7b8a6d8199 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -44,7 +44,9 @@ def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple: y_mean = np.mean(y_point) # Calculate the slope of the regression line. - slope = np.sum((x_point - x_mean) * (y_point - y_mean)) / np.sum((x_point - x_mean) ** 2) + slope = np.sum((x_point - x_mean) * (y_point - y_mean)) / np.sum( + (x_point - x_mean) ** 2 + ) # Calculate the intercept of the regression line. intercept = y_mean - slope * x_mean From fc7b0d4df90af129742110730afae38311179e79 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 14 Sep 2026 07:16:40 +0200 Subject: [PATCH 6/7] Change intercept output to float in examples --- machine_learning/ordinary_least_squares_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index ce7b8a6d8199..d19d86a8ea27 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -32,7 +32,7 @@ def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple: >>> x = np.array([1, 2, 3, 4, 5]) >>> y = np.array([2, 4, 6, 8, 10]) >>> a, b = ols_regression(x, y) - >>> a # Intercept should be 0.0 + >>> float(a) # Intercept should be 0.0 0.0 >>> round(b, 2) # Slope should be 2.0 2.0 From afbfbe52b8cf8acfe54394ad2cd5bd6813c85b6d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 14 Sep 2026 07:46:03 +0200 Subject: [PATCH 7/7] Update ordinary_least_squares_regression.py --- machine_learning/ordinary_least_squares_regression.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py index d19d86a8ea27..4fda6288ea77 100644 --- a/machine_learning/ordinary_least_squares_regression.py +++ b/machine_learning/ordinary_least_squares_regression.py @@ -7,7 +7,7 @@ and it is based on the principle of minimizing the sum of the squared residuals. -Below is simple implementation of OLSR +Below is a simple implementation of OLSR without using any external libraries. WIKI: https://en.wikipedia.org/wiki/Ordinary_least_squares @@ -21,8 +21,8 @@ def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple: Performs Ordinary Least Squares Regression (OLSR) on the given data. Args: - x (numpy.ndarray): The independent variable. - y (numpy.ndarray): The dependent variable. + x: The independent variable. + y: The dependent variable. Returns: a (float): The intercept of the regression line. @@ -34,7 +34,7 @@ def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple: >>> a, b = ols_regression(x, y) >>> float(a) # Intercept should be 0.0 0.0 - >>> round(b, 2) # Slope should be 2.0 + >>> float(round(b, 2)) # Slope should be 2.0 2.0 """