Skip to content

Instantly share code, notes, and snippets.

@Mengyuz
Created June 5, 2015 15:20
Show Gist options
  • Save Mengyuz/b0eac658fe7d5fe52e62 to your computer and use it in GitHub Desktop.
Save Mengyuz/b0eac658fe7d5fe52e62 to your computer and use it in GitHub Desktop.
import numpy as np
import scipy
import matplotlib.pyplot as plt
import sys
def compute_r_squared(data, predictions):
'''
In exercise 5, we calculated the R^2 value for you. But why don't you try and
and calculate the R^2 value yourself.
Given a list of original data points, and also a list of predicted data points,
write a function that will compute and return the coefficient of determination (R^2)
for this data. numpy.mean() and numpy.sum() might both be useful here, but
not necessary.
Documentation about numpy.mean() and numpy.sum() below:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html
'''
# your code here
r_squared = 1-np.sum((data-predictions)**2)/np.sum((data-np.mean(data))**2)
return r_squared
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment