Multipolyfit IndexError:元组索引超出范围

时间:2016-10-25 13:38:48

标签: python python-2.7 numpy

我正在尝试用python进行多变量非线性回归。小时,天和临时是自变量,而负载是因变量。

我安装了numpy和multipolyfit。

python代码如下:

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)

输出

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

单变量回归工作正常,但多变量回归没有。为什么我得到这个错误?

1 个答案:

答案 0 :(得分:0)

您没有以正确的方式致电multipolyfit。文档字符串表明它应该用作multipolyfit.multipolyfit(xs,y,deg),其中xs : array_like, shape (M, k)是您的hourdaytemps。这表明它们需要作为单个数组传递,如下所示:

multipolyfit.multipolyfit(np.vstack((hour,day,temps)).T ,load,3)