2
$\begingroup$

I'm using scikits linear model for modelling two input variables and one output variable. I suspect that the two input variables have a quadratic relationship with the output variable. So I created the data set ($a^2$, $b^2$, $ab$, $a$, $b$, $1$) for the two original input variables $a$ and $b$, then used the scikits linear model for this data which gave the coefficients:

[ 0.21746023  0.3457555  -0.15043191 -0.15318758 -0.27072946  0.        ]

the .predict() method produces very good predictions however, when I run the code

for i in data:
    print i[0]*coeffs[0]+i[1]*coeffs[1]+i[2]*coeffs[2]+i[3]*coeffs[3]+i[4]*coeffs[4]+i[5]*coeffs[5]

The outputs do not match the predictions at all. Here is my full code:

def import_data():
    data=np.genfromtxt("input_file")
    y=data[:,2]
    all_data=[(a**2,b**2,a*b,a,b,1) for (a,b,c) in data]
    return np.array(all_data),y

def main():
    data,y=import_data()

    regr = linear_model.LinearRegression()
    regr.fit(data, y)
    coeffs=regr.coef_
    print coeffs
    for i in data:
        print i[0]*coeffs[0]+i[1]*coeffs[1]+i[2]*coeffs[2]+i[3]*coeffs[3]+i[4]*coeffs[4]+i[5]*coeffs[5]
    print regr.score(data, y)
    print regr.predict(data)

Could someone tell me where my code is wrong?

$\endgroup$

1 Answer 1

1
$\begingroup$

I found the error, so I guess I'll answer my own question. Apparently the intercept term is not part of the coefficient vector, its a separate thing, so that has to be added onto the linear combination. So here is the correct code

for i in data:
        print i[0]*coeffs[0]+i[1]*coeffs[1]+i[2]*coeffs[2]+i[3]*coeffs[3]+i[4]*coeffs[4]+regr.intercept_
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.