# Polyfit

Greg Willden gregwillden at gmail.com
Thu Oct 12 14:59:40 CDT 2006

```Hi All,
I'm using numpy.polyfit and it is giving me some really strange results when
evaluated near 0.  So I compared it with polyfit in matplotlib and the code
and docstrings are nearly identical.  However the slight differences in the
code make a huge difference on my data.

Here is the Numpy version with line numbers added:
1    x = NX.asarray(x)+0.
2   y = NX.asarray(y)+0.
3    y = NX.reshape(y, (len(y), 1))
4    X = vander(x, N+1)
5   c, resids, rank, s = _lstsq(X, y)
6    c.shape = (N+1,)
7    return c

And here is the MPL version with line numbers added:
1    x = asarray(x)+0.
2    y = asarray(y)+0.
3    y = reshape(y, (len(y),1))
4    X = Matrix(vander(x, N+1))
5    Xt = Matrix(transpose(X))
6    c = array(linear_algebra.inverse(Xt*X)*Xt*y)  # convert back to array
7    c.shape = (N+1,)
8    return c

So lines 1-4 are basically the same.
The MPL version produces a much more stable representation of my data

Can someone please comment on the differences?
Should these two be exactly the same?
If not, what are the advantages to each algorithm?

I'm attaching my x and y points dumped to files.
Here is the test code using "ipython -pylab"
import numpy
semilogy(numpy.polyval(numpy.polyfit(x,y,3),arange(3604)));hold(True)
semilogy(polyval(polyfit(x,y,3),arange(3604))) #Use the MPL versions
scatter(x,y)

Regards,
Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061012/7e8242a8/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: x.data
Type: application/octet-stream
Size: 4451 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061012/7e8242a8/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: y.data
Type: application/octet-stream
Size: 8771 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061012/7e8242a8/attachment-0001.obj
-------------- next part --------------
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier