From a09a72816e96d9593e90a1c0b0172d0d465f72c9 Mon Sep 17 00:00:00 2001 From: Julien RICHARD Date: Tue, 1 Oct 2024 14:43:44 +0200 Subject: [PATCH 1/2] add tests for linear regression --- machine_learning/linear_regression.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 839a5366d..a0930dd02 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -40,7 +40,16 @@ def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): :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) + curr_features - alpha_ * gradient(w.r.t. feature) + + >>> data_x = np.array([[1, 2], [1, 3], [1, 4]]) + >>> data_y = np.array([[2], [2], [2]]) + >>> theta = np.array([[0.0, 0.0]]) + >>> alpha = 0.01 + >>> len_data = len(data_x) + >>> new_theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) + >>> new_theta.round(2) + array([[0.02, 0.06]]) """ n = len_data @@ -58,6 +67,13 @@ def sum_of_square_error(data_x, data_y, len_data, theta): :param len_data : len of the dataset :param theta : contains the feature vector :return : sum of square error computed from given feature's + + >>> data_x = np.array([[1, 2], [1, 3], [1, 4]]) + >>> data_y = np.array([[2], [2], [2]]) + >>> theta = np.array([[0.0, 0.0]]) + >>> len_data = len(data_x) + >>> sum_of_square_error(data_x, data_y, len_data, theta).round(2) + 2.0 """ prod = np.dot(theta, data_x.transpose()) prod -= data_y.transpose() @@ -89,10 +105,17 @@ def run_linear_regression(data_x, data_y): def mean_absolute_error(predicted_y, original_y): - """Return sum of square error for error calculation + """ + 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 + + >>> mean_absolute_error([3.0, 2.0, 1.0], [2.5, 2.0, 1.0]) + 0.16666666666666666 + >>> mean_absolute_error([5.0, 6.0], [5.0, 7.0]) + 0.5 """ total = sum(abs(y - predicted_y[i]) for i, y in enumerate(original_y)) return total / len(original_y) From a33e39ae2cd12b3e9b424db3462fd0db4338e0d5 Mon Sep 17 00:00:00 2001 From: Julien RICHARD Date: Tue, 1 Oct 2024 14:55:03 +0200 Subject: [PATCH 2/2] Refactor sum_of_square_error function in linear_regression.py --- machine_learning/linear_regression.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index a0930dd02..6aa52709d 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -67,13 +67,6 @@ def sum_of_square_error(data_x, data_y, len_data, theta): :param len_data : len of the dataset :param theta : contains the feature vector :return : sum of square error computed from given feature's - - >>> data_x = np.array([[1, 2], [1, 3], [1, 4]]) - >>> data_y = np.array([[2], [2], [2]]) - >>> theta = np.array([[0.0, 0.0]]) - >>> len_data = len(data_x) - >>> sum_of_square_error(data_x, data_y, len_data, theta).round(2) - 2.0 """ prod = np.dot(theta, data_x.transpose()) prod -= data_y.transpose()