Reginald Johnson Reginald Johnson - 1 year ago 100
Python Question

Multipolyfit IndexError: tuple index out of range

I am trying to do multivariable non-linear regression with python. Hour, day, and temps are the independent variables, and load is the dependent variable.

I have installed numpy and multipolyfit.

The python code is the following:

import numpy, multipolyfit

a,b,c,d=numpy.polyfit(temps, load, 3)


print "load = " +a+" * temp^3 + " +b+" * temp^2 + " +c+" * temp + " +d

a, b, c, d, e=multipolyfit.multipolyfit(hour, day, temps, load, 3)

The output is

load = -3.91 * temp^3 + 547.89 * temp^2 + -25367.77 * temp + 416600.52
Traceback (most recent call last):
File "", line 84, in <module>
a, b, c, d, e=multipolyfit.multipolyfit(hour, day, temps, load, 3)
File "c:\Python27\lib\site-packages\multipolyfit-0.0.1-
py2.7.egg\multipolyfit\", line 54, in multipolyfit
num_covariates = xs.shape[1]
IndexError: tuple index out of range

The one-variable regression works fine, but the multi-variable regression does not.Why am I getting this error?

Answer Source

You aren't calling multipolyfit in the right way. The docstring suggests that it should be used as multipolyfit.multipolyfit(xs,y,deg) where xs : array_like, shape (M, k) are your hour, day and temps. This suggests that they need to be passed as a single array, as follows:

multipolyfit.multipolyfit(np.vstack((hour,day,temps)).T ,load,3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download