Reginald Johnson Reginald Johnson - 1 month ago 16
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
data=numpy.loadtxt("midatl_combined.txt")
hour=data[:,3]
day=data[:,4]
temps=data[:,5]
load=data[:,6]

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

a=str(round(a,2))
b=str(round(b,2))
c=str(round(c,2))
d=str(round(d,2))

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 "midatl.py", 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\
core.py", 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

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)