Skip to content

Commit

Permalink
r-squared function added
Browse files Browse the repository at this point in the history
  • Loading branch information
alto10002 committed Jan 11, 2025
1 parent 9f18926 commit 0cad3bd
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 0cad3bd

Please sign in to comment.