## code for coefficient of determination

A forum for general discussion of the Python programming language.

### code for coefficient of determination

I am trying to define a function to compute the coefficient of determination.here is what I have done so far.I appreciate any help to know what I am doing wrong

Code: Select all
`def r2( y, f ) :    from numpy import mean                            # importing required libraries    from cmath import sqrt    n = len(y)    ybar = mean(y)                                 # mean of y    sigma_y^2 = (1.0/n)*sum((y-ybar)**2)    # variance of y    m = (mean(x*y)-(mean(x)*mean(y)))/(mean(x**2)-(mean(x)**2))    b = mean(y) - m*mean(x)    f = m*x + b    sigma_f^2 = (1.0/n)*sum((f-y)**2)    R^2 = 1 - (sigma_f^2/sigma_y^2)    return R^2`
Last edited by stranac on Sat Nov 02, 2013 11:42 pm, edited 1 time in total.
somayeh3333

Posts: 1
Joined: Sat Nov 02, 2013 11:15 pm

### Re: code for coefficient of determination

Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: code for coefficient of determination

Code: Select all
`sigma_y^2 = ...R^2 = ...`
are not Python. Also, I don't think mean(x*y) works at all (and if it does I'd be surprised if it does what you want).

In other words: you cannot "translate" any formula to what you think it should look like in Python. You need to know at least some language before trying to write in it.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm