convert poly1d to string ipython

一个人想着一个人 提交于 2019-12-13 03:00:03

问题


I have a polynomial equation expressed as a poly1d object and want to take it and use it to label the scatter plot I'm making. Is there any to do this? %d doesn't work unless you specify only one element of the polynomial, such as below

this works:

polynomial = 1.211e-16 x^2 + 3.619e-16 x + 22.53 (as poly1d object)
s = polynomial(0)
print 'the polynomial is %d'%(s)

but trying

s = polynomial
print 'the polynomial is %d'%(s)

runs an error: %d format: a number is required, not poly1d

any thoughts?


回答1:


Using %d as the format key means the value has to be a number. When you evaluate the polynomial on an input, that's what returned. However, the object itself is not a number, so it cannot be printed that way. The solution is to use the %s formatting key, which will insert whatever is returned by calling str() on the object:

polynomial = np.poly1d([1.211e-16, 3.619e-16, 22.53])
print "The polynomial is: \n\n %s" % polynomial

Which prints

The polynomial is: 

            2
1.211e-16 x + 3.619e-16 x + 22.53


来源:https://stackoverflow.com/questions/22119255/convert-poly1d-to-string-ipython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!