code for coefficient of determination

A forum for general discussion of the Python programming language.

code for coefficient of determination

Postby somayeh3333 » Sat Nov 02, 2013 11:22 pm

I am trying to define a function to compute the coefficient of 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.
Reason: Added code tags. Locked.
Posts: 1
Joined: Sat Nov 02, 2013 11:15 pm

Re: code for coefficient of determination

Postby micseydel » Sun Nov 03, 2013 10:23 pm

You should read this and then provide more information that would help us to help you.
Join the #python-forum IRC channel on!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
Posts: 1992
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: code for coefficient of determination

Postby ochichinyezaboombwa » Wed Nov 06, 2013 4:08 am

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.
Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Return to General Discussions

Who is online

Users browsing this forum: dthtfu, Yahoo [Bot] and 4 guests