Skip to content

Commit

Permalink
r2 updated
Browse files Browse the repository at this point in the history
  • Loading branch information
alto10002 committed Jan 14, 2025
1 parent cda3aec commit 1e1a6c7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
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 len(predictor) == 0 or len(response) == 0:
print('Input cannot be empty')
return None

if not isinstance(predictor, list) or not isinstance(predictor, list):
print('Input must be lists')
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 1e1a6c7

Please sign in to comment.