-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from UBC-MDS/r-squared_function
r-squared function added
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" | ||
|