Having difficulty using polyfit function

5,486 views
Skip to first unread message

slybro

unread,
Jan 10, 2009, 11:29:47 PM1/10/09
to sage-support
I am having trouble using the polyfit function. Here are the
commands:

import numpy as np
import scipy as sc

vp = np.array([1.0, 5.0, 10.0, 20.0, 40.0, 60.0, 100.0, 200.0, 400.0,
760.0])

T = np.array([-36.7, -19.6, -11.5, -2.6, 7.6, 15.4, 26.1, 42.2, 60.6,
80.1])

(a,b,c,d) = np.polyfit(vp,T,3)

and I get the following error message which I don't understand.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/notebook/sage_notebook/worksheets/admin/7/code/7.py",
line 7, in <module>
(a,b,c,d) = np.polyfit(vp,T,_sage_const_3 )
File "/usr/local/sage/local/lib/python2.5/site-packages/
zope.interface-3.3.0-py2.5-linux-i686.egg/", line 1, in <module>

File "/usr/local/sage/local/lib/python2.5/site-packages/numpy/lib/
polynomial.py", line 493, in polyfit
rcond = len(x)*finfo(x.dtype).eps
File "/usr/local/sage/local/lib/python2.5/site-packages/numpy/lib/
getlimits.py", line 98, in __new__
raise ValueError, "data type %r not inexact" % (dtype)
ValueError: data type <type 'numpy.object_'> not inexact

I have checked www.scipy.org with the associated documentation, the
sage help documentation, the numpy docs and it appears that I am using
the function correctly. I'm stumped... I'm interested in developing
an online class using sage for chemical engineering calculations.
Thanks.

Mike Hansen

unread,
Jan 11, 2009, 12:08:15 AM1/11/09
to sage-s...@googlegroups.com
Hello,

On Sat, Jan 10, 2009 at 8:29 PM, slybro <sntve...@gmail.com> wrote:
>
> I am having trouble using the polyfit function. Here are the
> commands:
>
> import numpy as np
> import scipy as sc
>
> vp = np.array([1.0, 5.0, 10.0, 20.0, 40.0, 60.0, 100.0, 200.0, 400.0,
> 760.0])
>
> T = np.array([-36.7, -19.6, -11.5, -2.6, 7.6, 15.4, 26.1, 42.2, 60.6,
> 80.1])
>
> (a,b,c,d) = np.polyfit(vp,T,3)

You must explicitly make the Numpy array's have dtype float as numpy
does not automatically convert Sage's RealNumber to a float. If you
don't do this, the array's dtype will be "object'.

sage: vp = np.array([1.0, 5.0, 10.0, 20.0, 40.0, 60.0, 100.0, 200.0,
400.0, 760.0],dtype=float)
sage: T = np.array([-36.7, -19.6, -11.5, -2.6, 7.6, 15.4, 26.1, 42.2,
60.6, 80.1], dtype=float)
sage: np.polyfit(vp,T,3)
array([ 1.13148994e-06, -1.49004659e-03, 6.12784745e-01,
-2.13934587e+01])

There are other ways to achieve a similar effect such as turning off
the Sage preparser with "preparser(False)" or making Sage's RealNumber
an alias of float ("RealNumber = float").

--Mike

Jason Grout

unread,
Jan 11, 2009, 12:11:17 AM1/11/09
to sage-s...@googlegroups.com
slybro wrote:
> I am having trouble using the polyfit function. Here are the
> commands:
>
> import numpy as np
> import scipy as sc
>
> vp = np.array([1.0, 5.0, 10.0, 20.0, 40.0, 60.0, 100.0, 200.0, 400.0,
> 760.0])
>
> T = np.array([-36.7, -19.6, -11.5, -2.6, 7.6, 15.4, 26.1, 42.2, 60.6,
> 80.1])
>
> (a,b,c,d) = np.polyfit(vp,T,3)
>
> and I get the following error message which I don't understand.
>


I'm pretty sure this has to do with Sage giving you the sage floating
point numbers by default. Your example seems to work if you declare
your arrays like:



vp = np.array([1.0, 5.0, 10.0, 20.0, 40.0, 60.0, 100.0, 200.0,
400.0,760.0],dtype=float)
T = np.array([-36.7, -19.6, -11.5, -2.6, 7.6, 15.4, 26.1, 42.2, 60.6,
80.1],dtype=float)

The "dtype" at the end makes sure that the numbers are python floating
point numbers, rather than Sage floating point numbers.

Thanks,

Jason

Steve Yarbro

unread,
Jan 11, 2009, 12:38:55 PM1/11/09
to sage-s...@googlegroups.com, mha...@gmail.com
Mike:

Outstanding!  Thank you, I appreciate the help.

Regards,
slybro

Harald Schilly

unread,
Jan 11, 2009, 1:27:28 PM1/11/09
to sage-support
On Jan 11, 5:29 am, slybro <sntventu...@gmail.com> wrote:
> I am having trouble using the polyfit function. ...

few days ago i did this quick example: http://sagenb.org/home/pub/141/

h

Steve Yarbro

unread,
Jan 11, 2009, 6:04:03 PM1/11/09
to sage-s...@googlegroups.com, harald....@gmail.com
Hi:

My thanks to Harald S., this was an excellent example.

This example worked great and solved another issue I was having using list_plot with the results.  How do you figure out what object type is required for input to other functions?  For example, using zip() to produce a list to use with list_plot (which worked great!)?  I was getting an error with the numpy arrays.  The dtype=float fixed the issue with the input to polyfit(), but it still produced an error with list_plot() and plot().  I am using command?<tab> to bring up the help docs.

slybro

mabshoff

unread,
Jan 11, 2009, 6:13:56 PM1/11/09
to sage-support


On Jan 11, 3:04 pm, "Steve Yarbro" <sntventu...@gmail.com> wrote:
> Hi:

Hi Steve,

> My thanks to Harald S., this was an excellent example.
>
> This example worked great and solved another issue I was having using
> list_plot with the results.  How do you figure out what object type is
> required for input to other functions?  For example, using zip() to produce
> a list to use with list_plot (which worked great!)?  I was getting an error
> with the numpy arrays.  The dtype=float fixed the issue with the input to
> polyfit(), but it still produced an error with list_plot() and plot().  I am
> using command?<tab> to bring up the help docs.

I think this is a known bug and there is even a patch for it, but
AFAIK it never made it into trac. If you post a short example that
illustrates the bug for you we can open a ticket and someone will
hopefully find the patch.

> slybro
>

Cheers,

Michael

Harald Schilly

unread,
Jan 11, 2009, 6:17:54 PM1/11/09
to sage-support
On Jan 12, 12:04 am, "Steve Yarbro" <sntventu...@gmail.com> wrote:
> My thanks to Harald S., this was an excellent example.

glad to help you, this example is just a working one, maybe there are
better ways to do the same thing... i.e. a more "sage"-way where you
use numpy's polyfit only implicitly.

> For example, using zip() to produce
> a list to use with list_plot (which worked great!)?  I was getting an error
> with the numpy arrays.  

I can't really answer your question. At first, you have to know that
there is a so called "preparser". This is a generally helpful thing to
work with Sage, but in this case not so good. Here, it converts basic
python number types to sage number types. This is not good if you want
to build a numpy array, there you want basic python types. In
conclusion, there is more than one type of floating point number ...

The zip command is very basic python and combines two lists into a
single list of tuples, x1,x2,x3 and y1, y2, y3 -> (x1,y1),(x2,y2), ...
and those are the coordinates of the points for list_plot. I'm not
aware of any special things that happen to the number types!?

Third, what exactly did you do when you got that other error? Please
provide us here with a small example so we can pinpoint the problem.

Harald

Steve Yarbro

unread,
Jan 11, 2009, 9:49:28 PM1/11/09
to sage-s...@googlegroups.com, Michael...@mathematik.uni-dortmund.de


On Sun, Jan 11, 2009 at 4:13 PM, mabshoff <> wrote:

Hi Michael:

Thanks for the help.  The error is very likely my lack of skill with sage (an excellent piece of work BTW).  I have put together an example as you suggested.  The support for sage is very good.  Thank you.  The example URL is  http://sagenb.org:8000/home/pub/148/.   When I use the zip(), the list_plot works.

Thanks
Steve


On Jan 11, 3:04 pm, "Steve Yarbro" <sntventu...@gmail.com> wroteMichael...@mathematik.uni-dortmund.de:

mabshoff

unread,
Jan 14, 2009, 6:53:25 AM1/14/09
to sage-support


On Jan 11, 6:49 pm, "Steve Yarbro" <sntventu...@gmail.com> wrote:
> On Sun, Jan 11, 2009 at 4:13 PM, mabshoff <> wrote:
>
> > Hi Michael:
>
> Thanks for the help.  The error is very likely my lack of skill with sage
> (an excellent piece of work BTW).  I have put together an example as you
> suggested.  The support for sage is very good.  Thank you.  The example URL
> is  http://sagenb.org:8000/home/pub/148/.   When I use the zip(), the
> list_plot works.

Ok. Can you add an example when the code works and I will make it into
a ticket, i.e. before and after. The zip() should probably go
somewhere deeper into the plotting code, but somebody may fix this at
Sage Days 12.

> Thanks
> Steve
>
>

Cheers,

Michael

Steve Yarbro

unread,
Jan 14, 2009, 11:41:04 PM1/14/09
to sage-s...@googlegroups.com, Michael...@mathematik.uni-dortmund.de
Hi Michael:

I published an example at http://sagenb.org:8000/home/pub/156.  Thanks.

Steve

Harald Schilly

unread,
Jan 15, 2009, 6:51:28 AM1/15/09
to sage-support
On Jan 15, 5:41 am, "Steve Yarbro" <sntventu...@gmail.com> wrote:
> Hi Michael:
>
> I published an example athttp://sagenb.org:8000/home/pub/156. Thanks.

Hello, aside from the actual problem, this fit looks quite wrong. I
played around a bit and fitted a logarithmic model in R. R is not very
well embedded and I forgot how to extract the values from a list (i.e.
I don't know how to access the values of the actual fit and had to
copy them by hand *g*), but here is how it looks:

http://sagenb.org:8000/home/pub/158/

Harald

Jason Grout

unread,
Jan 15, 2009, 9:44:45 PM1/15/09
to sage-s...@googlegroups.com
Steve Yarbro wrote:
>
>
> On Sun, Jan 11, 2009 at 4:13 PM, mabshoff <> wrote:
>
>
> Hi Michael:
>
>
> Thanks for the help. The error is very likely my lack of skill with
> sage (an excellent piece of work BTW). I have put together an example
> as you suggested. The support for sage is very good. Thank you. The
> example URL is http://sagenb.org:8000/home/pub/148/. When I use the
> zip(), the list_plot works.
>


Yes, you need to use zip in your case. list_plot takes a list of
points, like this:

list_plot([ (x1,y1), (x2,y2), (x3,y3)])

You were trying to pass it two lists, one of x coordinates and one of y
coordinates. This is how you would do things in matlab, but not Sage:

list_plot([x1, x2, x3], [y1, y2, y3])

The zip command just converts between these:

zip([x1,x2,x3], [y1,y2,y3]) is [(x1,y1), (x2,y2), (x3,y3)]


You can see the documentation for list_plot and zip by typing the
command name followed by a question mark:

list_plot?

zip?


Having said that, this confusion points to a bug in the list_plot
documentation. It should probably mention this and show how to use the
zip command.

Thanks,

Jason

Steve Yarbro

unread,
Jan 15, 2009, 10:44:29 PM1/15/09
to sage-s...@googlegroups.com, harald....@gmail.com
Hi Harald:

Thanks for the great example.  The fit is much better.  I've been working problems from Cutlip and Shacham, "Problem Solving in Chemical Engineering with Numerical Methods".  They have worked 10 selected problems with Polymath, MATLAB, Mathematica, Maple and Excel.  I've been working the same set with Sage.  For this problem, 1.3, the first attempt was using the polyfit function in MATLAB.  I was using the Sage polyfit for comparison.  The log fit is much better.  Thanks for the example, it helps alot.

Regards,
Steve

Steve Yarbro

unread,
Jan 15, 2009, 10:59:23 PM1/15/09
to sage-s...@googlegroups.com, jason...@creativetrax.com
Hi Jason:

Thanks for the help.  I teach a MATLAB for engineers online course at the local univ so I tend to use MATLAB syntax by habit.  However, I'm interested in swapping out and going to Sage/Python for the same type of early programming class for engineers.  Thanks for the input, that helps alot.

Regards,
Steve

Harald Schilly

unread,
Jan 16, 2009, 7:02:37 AM1/16/09
to Steve Yarbro, sage-s...@googlegroups.com
Hello, i've updated my published worksheet from above.
To clearify this, the polyfit is actually numpy's and the glm
(generalized linear model) is from R. Sage just enables you to use
both of them (more or less seamless). I don't know any chemical
problems, i've just some background in experimental physics, but i
think at least this example is quite good for beginners to understand
what to do. Do you know the actual formula for this process? the log
was just a guess based on the data, but it would be cool to get the
real thing and guess the correct parameters.
What I really want to say, it would be really beneficial to sage and
new users to have these examples documented online somewhere. Would
you be interested in writing some sort of introduction like, "chemical
engineering with sage"? (If you provide the examples, I can help)

Harald

Reply all
Reply to author
Forward
0 new messages