Skip to content

Commit

Permalink
Merge pull request #72 from UBC-MDS/feedback_changes_celine
Browse files Browse the repository at this point in the history
fix: Feedback changed fixed from Sarah by Celine
  • Loading branch information
yajing03 authored Feb 2, 2025
2 parents 8d34668 + 3a94fee commit 27d8c9c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ matrics_calculator provides a lightweight and easy-to-use alternative for calcul
This package consists of four functions:
- `r2`:
- This function calculates the R-squared of the model, which measures how well the model explains the variation in the data.
- `mean_absolute_error`:
- `MAE`:
- This function finds the average difference between predicted and actual values.
- `mean_squared_error`:
- This function calculates the average of the squared differences between predictions and actual values.
- `mean_absolute_percentage_error`:
- `MSE`:
- This fuction calculates the average of the squared differences between predictions and actual values.
- `MAPE`:
- This function shows prediction error as a percentage, making it easy to understand.

## matrics_calculator in the Python Ecosystem

`matrics_calculator` works alongside Python libraries like `scikit-learn` by providing simple implementations of regression metrics. Unlike `scikit-learn`’s full toolkit for modeling and evaluation, this package focuses only on metrics, making it easy to use for quick analysis or custom workflows.

## Installation

`matrics_calculator` requires Python **3.7 or later**.

To install the package, navigate to the root directory of the project and run:
```bash
$ pip install matrics_calculator
Expand Down
3 changes: 3 additions & 0 deletions src/matrics_calculator/MAE.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def mean_absolute_error(y_true, y_pred):
>>> mean_absolute_error(y_true, y_pred)
10.0
"""
# Ensure the input lists have the same length; otherwise, raise an error
if len(y_true) != len(y_pred):
raise ValueError("The lengths of y_true and y_pred must be the same.")

# Compute the absolute differences between actual and predicted values, sum them,
# and divide by the total number of observations to calculate MAE
mae = sum(abs(true - pred) for true, pred in zip(y_true, y_pred)) / len(y_true)
return mae
10 changes: 9 additions & 1 deletion src/matrics_calculator/MSE.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def mean_squared_error(y_true, y_pred):
Notes
-----
MSE is defined as:
MSE = (1 / n) * sum((y_true - y_pred)²)
where `n` is the number of observations.
This function assumes that the input `y_true` and `y_pred` have the same length.
Examples
Expand All @@ -31,9 +35,13 @@ def mean_squared_error(y_true, y_pred):
>>> mean_squared_error(y_true, y_pred)
0.375
"""

# Ensure the input lists have the same length; otherwise, raise an error
if len(y_true) != len(y_pred):
raise ValueError("The lengths of y_true and y_pred must be the same.")


# Compute the squared differences between actual and predicted values, sum them,
# and divide by the total number of observations to calculate MSE
mse = sum((true - pred) ** 2 for true, pred in zip(y_true, y_pred)) / len(y_true)
return mse

16 changes: 14 additions & 2 deletions src/matrics_calculator/r2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,35 @@ def r2(y_pred, y_true):
from sklearn.linear_model import LinearRegression
import numpy as np

# Ensure inputs are lists
if not isinstance(y_pred, list) or not isinstance(y_pred, list):
print('Input must be lists')
return None


# Check for empty lists
if len(y_pred) == 0 or len(y_true) == 0:
print('Input cannot be empty')
return None


# Convert lists to NumPy arrays if they are still in list format
if isinstance(y_pred,list):
y_pred = np.array(y_pred)
if isinstance(y_true,list):
y_true = np.array(y_true)

# Create a linear regression model and fit it to the data
model = LinearRegression()
model.fit(y_pred.reshape(-1,1),y_true)
y_true_predicted = model.predict(y_pred.reshape(-1,1))

# Calculate the mean of y_true
y_true_mean = np.mean(y_true)

# Compute Residual Sum of Squares
RSS = sum(((y_true-y_true_predicted) ** 2))

# Compute Total Sum of Squares (TSS)
TSS = sum(((y_true-y_true_mean) ** 2))

# Compute R-squared value and round it to 3 decimal places
return round(1 - (RSS/TSS),3)

0 comments on commit 27d8c9c

Please sign in to comment.