问题
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