[Numpy-discussion] Runtime error in numpy.polyfit

William Carithers wccarithers at lbl.gov
Wed May 19 16:24:07 EDT 2010


Hi Josef,

I did the same test, namely opening a new window and plugging in the
printout values by hand and polyfit worked just fine. Here's the terminal
output:

>>> import numpy as n
>>> y = n.array([ 864.,  865.,  866.,  867.,  868.])
>>> a = n.array([ 5.24860191,  6.0217514 ,  6.11434555,  6.09198856,
5.73753977])
>>> ypoly = n.polyfit(y,a,2)
>>> ypoly
array([ -1.69296264e-01,   2.93325941e+02,  -1.27049334e+05])

I wonder if the step of printing plus cut and paste is doing some kind of
implicit type conversion. Maybe the original problem has to do with data
types? In the original code arcImage is integer data so the avalues array is
constructed from
 avalues = n.log(n.array([...list of integers...]))

Should I be doing some kind of casting first?

Thanks,
Bill





On 5/19/10 1:09 PM, "josef.pktd at gmail.com" <josef.pktd at gmail.com> wrote:

> On Wed, May 19, 2010 at 3:51 PM, William Carithers <wccarithers at lbl.gov>
> wrote:
>> Thanks David and Josef. Replies interspersed below.
>> 
>> 
>> On 5/19/10 12:24 PM, "josef.pktd at gmail.com" <josef.pktd at gmail.com> wrote:
>> 
>>> On Wed, May 19, 2010 at 3:18 PM, David Goldsmith
>>> <d.l.goldsmith at gmail.com> wrote:
>>>> Charles H.: is this happening because he's calling the old version of
>>>> polyfit?
>>>> 
>>>> William: try using numpy.polynomial.polyfit instead, see if that works.
>> 
>> It says  ypoly = n.polynomial.polyfit(yvalues, avalues, 2)
>> AttributeError: 'module' object has no attribute 'polynomial'
>> 
>> Is this because I'm using a relatively old (numpy-1.2.1) version?
>>>> 
>>>> DG
>>>> 
>>>> On Wed, May 19, 2010 at 11:03 AM, William Carithers <wccarithers at lbl.gov>
>>>> wrote:
>>>>> 
>>>>> I'm trying to do a simple 2nd degree polynomial fit to two arrays of 5
>>>>> entries. I get a runtime error:
>>>>> RuntimeError: more argument specifiers than keyword list entries
>>>>> (remaining
>>>>> format:'|:calc_lwork.gelss')  in the lstsq module inside numpy.polyfit.
>>>>> 
>>>>> Here's the code snippet:
>>>>> def findPeak(self, ydex, xdex):
>>>>>        # take a vertical slice
>>>>>        vslice = []
>>>>>        for i in range(-1,10,1) :
>>>>>            vslice.append(arcImage[ydex+i][xdex])
>>>>>        vdex = n.where(vslice == max(vslice))
>>>>>        ymax = ydex -1 + vdex[0][0]
>>>>>        # approximate gaussian fit by parabolic fit to logs
>>>>>        yvalues = n.array([ymax-2, ymax-1, ymax, ymax+1, ymax+2])
>>>>> 
>>>>> 
>>>>> svalues=n.array([arcImage[ymax-2][xdex],arcImage[ymax-1][xdex],arcImage[ym
>>>>> ax
>>>>> ][xdex],arcImage[ymax+1][xdex], arcImage[ymax+2][xdex]])
>>>>>        avalues = n.log(svalues)
>>>>>        ypoly = n.polyfit(yvalues, avalues, 2)
>>>>> 
>>>>> And the traceback:
>>>>> File "/Users/williamcarithers/BOSS/src/calibrationModel.py", line 345, in
>>>>> findPeak
>>>>>    ypoly = n.polyfit(yvalues, avalues, 2)
>>>>>  File
>>>>> 
>>>>> "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/pytho
>>>>> n/
>>>>> numpy/lib/polynomial.py", line 503, in polyfit
>>>>>    c, resids, rank, s = _lstsq(v, y, rcond)
>>>>>  File
>>>>> 
>>>>> "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/pytho
>>>>> n/
>>>>> numpy/lib/polynomial.py", line 46, in _lstsq
>>>>>    return lstsq(X, y, rcond)
>>>>>  File
>>>>> 
>>>>> "/Library/Python/2.6/site-packages/scipy-0.7.1-py2.6-macosx-10.6-universal
>>>>> .e
>>>>> gg/scipy/linalg/basic.py", line 545, in lstsq
>>>>>    lwork = calc_lwork.gelss(gelss.prefix,m,n,nrhs)[1]
>>>>> RuntimeError: more argument specifiers than keyword list entries
>>>>> (remaining
>>>>> format:'|:calc_lwork.gelss')
>>>>> 
>>>>> This is such a simple application of polyfit and the error occurs in the
>>>>> guts of lstsq, so I'm completely stumped. Any help would be greatly
>>>>> appreciated.
>>> 
>>> which version of numpy and the arguments to polyfit would be useful
>>> information,e.g.
>>> 
>>> print repr(yvalues)
>>> print repr(avalues)
>>> 
>>> before the call to polyfit
>> 
>> Hi Josef,
>> 
>> I'm using numpy-1.2.1
> 
> I don't remember whether 1.2.1 was fully python 2.6 compatible. I
> would recommend upgrading if possible.
> 
> I don't have any problems with more recent versions of scipy and numpy
> 
>>>> import numpy as np
>>>> y = np.array([ 864.,  865.,  866.,  867.,  868.])
>>>> x = np.array([ 5.24860191,  6.0217514 ,  6.11434555,  6.09198856,
>>>> 5.73753977],dtype=np.float32)
>>>> np.polyfit(y, x, 2)
> array([ -1.69296265e-01,   2.93325942e+02,  -1.27049335e+05])
> 
> 
> I didn't know numpy will use the scipy version of linalg for this.
> Do the scipy.test() pass?
> 
> My guess would be that there are some incompatibilities with your
> python/numpy/scipy versions.
> 
> Josef
> 
> 
>> 
>> Here are the arrays
>> array([ 864.,  865.,  866.,  867.,  868.])
>> array([ 5.24860191,  6.0217514 ,  6.11434555,  6.09198856,  5.73753977],
>> dtype=float32)
>> 
>> thanks
>>> 
>>> Josef
>>> 
>>> 
>>>>> 
>>>>> Thanks,
>>>>> Bill Carithers
>>>>> 
>>>>> 
>>>>> _______________________________________________
>>>>> NumPy-Discussion mailing list
>>>>> NumPy-Discussion at scipy.org
>>>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>> 
>>>> 
>>>> 
>>>> --
>>>> Mathematician: noun, someone who disavows certainty when their uncertainty
>>>> set is non-empty, even if that set has measure zero.
>>>> 
>>>> Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
>>>> lies, prevents mankind from committing a general suicide.  (As interpreted
>>>> by Robert Graves)
>>>> 
>>>> _______________________________________________
>>>> NumPy-Discussion mailing list
>>>> NumPy-Discussion at scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>> 
>>>> 
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> 
>> 
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list