From 5de5b80100ac8ad75d5a4cd1b7ba5967b449b7eb Mon Sep 17 00:00:00 2001 From: Smruthi Rao Date: Sun, 25 Feb 2024 16:34:45 +0530 Subject: [PATCH 1/6] added bipolar binary function --- .../bipolar_binary_step.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 neural_network/activation_functions/bipolar_binary_step.py diff --git a/neural_network/activation_functions/bipolar_binary_step.py b/neural_network/activation_functions/bipolar_binary_step.py new file mode 100644 index 000000000000..d70acaec57c7 --- /dev/null +++ b/neural_network/activation_functions/bipolar_binary_step.py @@ -0,0 +1,34 @@ +""" +This script demonstrates the implementation of the Bipolar Binary Step function. + +It's an activation function in which the neuron outputs 1 if the input is positive +or 0, else outputs -1 if the input is negative. + +It's a simple activation function which is mentioned in this wikipedia article: +https://en.wikipedia.org/wiki/Activation_function +""" + +import numpy as np + +def bipolar_binary_step(vector: np.ndarray) -> np.ndarray: + """ + Implements the binary step function + + Parameters: + vector (ndarray): A vector that consists of numeric values + + Returns: + vector (ndarray): Input vector after applying binary step function + + >>> vector = np.array([-1.2, 0, 2, 1.45, -3.7, 0.3]) + >>> bipolar_binary_step(vector) # doctest: +NORMALIZE_WHITESPACE + array([-1, 1, 1, 1, -1, 1]) + """ + + return np.where(vector >= 0, 1, -1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 9bd4765fccd493cf08a7a6d56f76cc5f38edc2b5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 25 Feb 2024 11:10:46 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/activation_functions/bipolar_binary_step.py | 1 + 1 file changed, 1 insertion(+) diff --git a/neural_network/activation_functions/bipolar_binary_step.py b/neural_network/activation_functions/bipolar_binary_step.py index d70acaec57c7..373ba6ceaea4 100644 --- a/neural_network/activation_functions/bipolar_binary_step.py +++ b/neural_network/activation_functions/bipolar_binary_step.py @@ -10,6 +10,7 @@ import numpy as np + def bipolar_binary_step(vector: np.ndarray) -> np.ndarray: """ Implements the binary step function From 1486a54d654b8770c1200624e3dc28b9d124de0c Mon Sep 17 00:00:00 2001 From: Smruthi Rao Date: Wed, 27 Mar 2024 23:26:09 +0530 Subject: [PATCH 3/6] modified linear regression to work on OLS --- machine_learning/linear_regression.py | 81 +++++---------------------- 1 file changed, 14 insertions(+), 67 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 39bee5712c16..f1f619986769 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -31,85 +31,32 @@ def collect_dataset(): return dataset -def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): - """Run steep gradient descent and updates 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 - curr_features - alpha_ * gradient(w.r.t. feature) - """ - n = len_data - - prod = np.dot(theta, data_x.transpose()) - prod -= data_y.transpose() - sum_grad = np.dot(prod, data_x) - theta = theta - (alpha / n) * sum_grad - return theta - - -def sum_of_square_error(data_x, data_y, len_data, theta): - """Return 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 - :param theta : contains the feature vector - :return : sum of square error computed from given feature's - """ - prod = np.dot(theta, data_x.transpose()) - prod -= data_y.transpose() - sum_elem = np.sum(np.square(prod)) - error = sum_elem / (2 * len_data) - return error - - -def run_linear_regression(data_x, data_y): - """Implement Linear regression over the dataset - :param data_x : contains our dataset - :param data_y : contains the output (result vector) +def run_linear_regression_ols(data_x, data_y): + """Implement Linear regression using OLS over the dataset + :param data_x : contains our dataset + :param data_y : contains the output (result vector) :return : feature for line of best fit (Feature vector) """ - iterations = 100000 - alpha = 0.0001550 - - no_features = data_x.shape[1] - len_data = data_x.shape[0] - 1 + # Add a column of ones to data_x for the bias term + data_x = np.c_[np.ones(data_x.shape[0]), data_x].astype(float) - theta = np.zeros((1, no_features)) - - 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}") + # Use NumPy's built-in function to solve the linear regression problem + theta = np.linalg.inv(data_x.T.dot(data_x)).dot(data_x.T).dot(data_y) return theta -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 - """ - total = sum(abs(y - predicted_y[i]) for i, y in enumerate(original_y)) - return total / len(original_y) - - def main(): """Driver function""" data = collect_dataset() - - len_data = data.shape[0] - data_x = np.c_[np.ones(len_data), data[:, :-1]].astype(float) + data_x = data[:, :-1].astype(float) data_y = data[:, -1].astype(float) - theta = run_linear_regression(data_x, data_y) - len_result = theta.shape[1] - print("Resultant Feature vector : ") - for i in range(len_result): - print(f"{theta[0, i]:.5f}") + theta = run_linear_regression_ols(data_x, data_y) + print("Resultant Feature vector (weights): ") + theta_list = theta.tolist()[0] + for i in range(len(theta_list)): + print(f"{theta_list[i]:.5f}") if __name__ == "__main__": From 6a8adc9a7d1ebbd1650e39109fb573d30500a30d Mon Sep 17 00:00:00 2001 From: Smruthi Rao Date: Wed, 27 Mar 2024 23:28:44 +0530 Subject: [PATCH 4/6] Delete neural_network/activation_functions/bipolar_binary_step.py --- .../bipolar_binary_step.py | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 neural_network/activation_functions/bipolar_binary_step.py diff --git a/neural_network/activation_functions/bipolar_binary_step.py b/neural_network/activation_functions/bipolar_binary_step.py deleted file mode 100644 index 373ba6ceaea4..000000000000 --- a/neural_network/activation_functions/bipolar_binary_step.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -This script demonstrates the implementation of the Bipolar Binary Step function. - -It's an activation function in which the neuron outputs 1 if the input is positive -or 0, else outputs -1 if the input is negative. - -It's a simple activation function which is mentioned in this wikipedia article: -https://en.wikipedia.org/wiki/Activation_function -""" - -import numpy as np - - -def bipolar_binary_step(vector: np.ndarray) -> np.ndarray: - """ - Implements the binary step function - - Parameters: - vector (ndarray): A vector that consists of numeric values - - Returns: - vector (ndarray): Input vector after applying binary step function - - >>> vector = np.array([-1.2, 0, 2, 1.45, -3.7, 0.3]) - >>> bipolar_binary_step(vector) # doctest: +NORMALIZE_WHITESPACE - array([-1, 1, 1, 1, -1, 1]) - """ - - return np.where(vector >= 0, 1, -1) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 29fb882c7fab464b67f33e3e66ec92fb6ccb6af5 Mon Sep 17 00:00:00 2001 From: Smruthi Rao Date: Mon, 10 Jun 2024 06:57:07 +0530 Subject: [PATCH 5/6] Update machine_learning/linear_regression.py Co-authored-by: Tianyi Zheng --- 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 f1f619986769..800438416d57 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -31,7 +31,7 @@ def collect_dataset(): return dataset -def run_linear_regression_ols(data_x, data_y): +def ols_linear_regression(data_x: np.ndarray, data_y: np.ndarray) -> np.ndarray: """Implement Linear regression using OLS over the dataset :param data_x : contains our dataset :param data_y : contains the output (result vector) From 1629c4cf976d44dbd386c0ff48bb2dc252ad2103 Mon Sep 17 00:00:00 2001 From: Smruthi Rao Date: Mon, 10 Jun 2024 06:57:40 +0530 Subject: [PATCH 6/6] Update machine_learning/linear_regression.py Co-authored-by: Tianyi Zheng --- 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 800438416d57..faea0f1353ad 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -41,7 +41,7 @@ def ols_linear_regression(data_x: np.ndarray, data_y: np.ndarray) -> np.ndarray: data_x = np.c_[np.ones(data_x.shape[0]), data_x].astype(float) # Use NumPy's built-in function to solve the linear regression problem - theta = np.linalg.inv(data_x.T.dot(data_x)).dot(data_x.T).dot(data_y) + theta = np.linalg.inv(data_x.T @ data_x) @ data_x.T @ data_y return theta