Skip to content

Commit

Permalink
Merge pull request #17 from UBC-MDS/r-squared_function
Browse files Browse the repository at this point in the history
r-squared function added
  • Loading branch information
yajing03 authored Jan 11, 2025
2 parents 9f18926 + 0cad3bd commit cda3aec
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/matrics_calculator/R_squared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# r-squared value calcualtion
def r2(df, predictor, response):
"""
Calculates r-squared using linear regression.
Computes the r-squared value (coefficient of determination) using the provided dataframe,
predictor column names, and response column name.
Parameters
----------
df : dataframe
Dataframe containing necessary predictor values and response value.
predictor : list or string
Predictor values to be used in calculating r-sqaured value.
response : list or string
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
--------
>>> r2(student_grades, math_test, final_grade)
0.88
>>> r2(student_grades, [math_test, science_test], absences)
-0.32
"""

0 comments on commit cda3aec

Please sign in to comment.