From a6999ab68d140540e8864fbeda2f3a1c567d172d Mon Sep 17 00:00:00 2001 From: Jay Mangat Date: Thu, 30 Jan 2025 11:41:21 -0800 Subject: [PATCH] fix: feedback addressed my jay --- README.md | 4 ++++ src/matrics_calculator/r2.py | 36 ++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ca8a429..e2f82d1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ A package providing functions to calculate key regression metrics: R-squared, Mean Absolute Error (MAE), Mean Squared Error (MSE), and Mean Absolute Percentage Error (MAPE). +## Python Ecosystem + +matrics_calculator provides a lightweight and easy-to-use alternative for calculating key regression metrics, complementing existing libraries like scikit-learn. While scikit-learn offers a full suite of machine learning tools, matrics_calculator focuses solely on evaluation metrics, making it a useful option for quick analysis, custom workflows, or educational purposes. Its simplicity makes it accessible for users who need essential regression metrics without the overhead of a larger machine learning framework. + ## Features This package consists of four functions: diff --git a/src/matrics_calculator/r2.py b/src/matrics_calculator/r2.py index 1e0e3ae..ed93eff 100644 --- a/src/matrics_calculator/r2.py +++ b/src/matrics_calculator/r2.py @@ -1,17 +1,17 @@ # r-squared value calculation -def r2(predictor, response): +def r2(y_pred, y_true): """ Calculates r-squared using linear regression. - Computes the r-squared value (coefficient of determination) using the provided predictor - list and response list. + Computes the r-squared value (coefficient of determination) using the provided y_pred + list and y_true list. Parameters ---------- - predictor : list - Predictor values to be used in calculating r-sqaured value. - response : list - Response values to be used in calculating r-sqaured value. + y_pred : list + y_pred values to be used in calculating r-sqaured value. + y_true : list + y_true values to be used in calculating r-sqaured value. Returns ------- @@ -35,23 +35,23 @@ def r2(predictor, response): from sklearn.linear_model import LinearRegression import numpy as np - if not isinstance(predictor, list) or not isinstance(predictor, list): + if not isinstance(y_pred, list) or not isinstance(y_pred, list): print('Input must be lists') return None - if len(predictor) == 0 or len(response) == 0: + if len(y_pred) == 0 or len(y_true) == 0: print('Input cannot be empty') return None - if isinstance(predictor,list): - predictor = np.array(predictor) - if isinstance(response,list): - response = np.array(response) + if isinstance(y_pred,list): + y_pred = np.array(y_pred) + if isinstance(y_true,list): + y_true = np.array(y_true) model = LinearRegression() - model.fit(predictor.reshape(-1,1),response) - response_predicted = model.predict(predictor.reshape(-1,1)) - response_mean = np.mean(response) - RSS = sum(((response-response_predicted) ** 2)) - TSS = sum(((response-response_mean) ** 2)) + model.fit(y_pred.reshape(-1,1),y_true) + y_true_predicted = model.predict(y_pred.reshape(-1,1)) + y_true_mean = np.mean(y_true) + RSS = sum(((y_true-y_true_predicted) ** 2)) + TSS = sum(((y_true-y_true_mean) ** 2)) return round(1 - (RSS/TSS),3) \ No newline at end of file