在numpy中获取polyfit的反函数 [英] Get the inverse function of a polyfit in numpy
本文介绍了在numpy中获取polyfit的反函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已通过以下方式将二阶多项式拟合到多个x/y点:
I have fit a second order polynomial to a number of x/y points in the following way:
poly = np.polyfit(x, y, 2)
如何在python中反转此函数,以获取对应于特定y值的两个x值?
How can I invert this function in python, to get the two x-values corresponding to a specific y-value?
推荐答案
下面是一个示例,显示了如何组合poly
我的回答在 numpy.polyval()的反函数.
Here's an example that shows how you can combine your poly
with my answer at Inverse function of numpy.polyval().
首先提供一些数据:
In [44]: x
Out[44]: array([0, 1, 2, 3, 4, 5, 6, 7])
In [45]: y
Out[45]: array([ 9, 4, 0, -1, -1, 4, 8, 16])
为数据拟合多项式:
In [46]: poly = np.polyfit(x, y, 2)
查找多项式具有值y0
In [47]: y0 = 4
为此,创建一个poly1d
对象:
In [48]: p = np.poly1d(poly)
并找到p - y0
的根:
In [49]: (p - y0).roots
Out[49]: array([ 5.21787721, 0.90644711])
检查:
In [54]: x0 = (p - y0).roots
In [55]: p(x0)
Out[55]: array([ 4., 4.])
这篇关于在numpy中获取polyfit的反函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文