Skip to content

Commit

Permalink
Merge pull request #23 from UBC-MDS/r2_test
Browse files Browse the repository at this point in the history
R2 test
  • Loading branch information
yajing03 authored Jan 15, 2025
2 parents 44afc98 + 6bbf147 commit b1eab3a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 41 deletions.
31 changes: 23 additions & 8 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example usage\n",
"\n",
"To use `matrics_calculator` in a project:"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.1.0\n"
]
}
],
"source": [
"import matrics_calculator\n",
"\n",
"print(matrics_calculator.__version__)"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"metadata": {}
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "matrics_calculator",
"language": "python",
"name": "python3"
},
Expand All @@ -37,9 +52,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.10.16"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
3 changes: 1 addition & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions src/matrics_calculator/R_squared.py

This file was deleted.

57 changes: 57 additions & 0 deletions src/matrics_calculator/r2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# r-squared value calculation
def r2(predictor, response):
"""
Calculates r-squared using linear regression.
Computes the r-squared value (coefficient of determination) using the provided predictor
list and response 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.
Returns
-------
float
r-sqaured value which is <= 1. 1 is the best score and a score below 0 is worse than
using the mean of the target as predictions.
Examples
--------
data = {
'math_test': [80, 85, 90, 95],
'science_test': [78, 82, 88, 92],
'final_grade': [84, 87, 91, 94],
'absences': [3, 0, 1, 30]
}
>>> r2(data['math_test'],data['final_grade'])
0.997
>>> r2(data['math_test'],data['absences'])
0.541
"""
from sklearn.linear_model import LinearRegression
import numpy as np

if not isinstance(predictor, list) or not isinstance(predictor, list):
print('Input must be lists')
return None

if len(predictor) == 0 or len(response) == 0:
print('Input cannot be empty')
return None

if isinstance(predictor,list):
predictor = np.array(predictor)
if isinstance(response,list):
response = np.array(response)

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))
return round(1 - (RSS/TSS),3)
17 changes: 17 additions & 0 deletions tests/test_r2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from matrics_calculator.r2 import r2

def test_r2():
data = {
'math_test': [80, 85, 90, 95],
'science_test': [78, 82, 88, 92],
'final_grade': [84, 87, 91, 94],
'absences': [3, 0, 1, 30]
}

assert r2(1, 0) == None

assert r2([],[1,2,3]) == None

assert r2(data['math_test'], data['final_grade']) == 0.997

assert r2(data['math_test'], data['absences']) == 0.541

0 comments on commit b1eab3a

Please sign in to comment.