Skip to content

Commit

Permalink
fix: feedback addressed my jay
Browse files Browse the repository at this point in the history
  • Loading branch information
alto10002 committed Jan 30, 2025
1 parent e15faaa commit a6999ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions src/matrics_calculator/r2.py
Original file line number Diff line number Diff line change
@@ -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
-------
Expand All @@ -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)

0 comments on commit a6999ab

Please sign in to comment.