反向输出polyfit numpy [英] Reverse output of polyfit numpy

查看:112
本文介绍了反向输出polyfit numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了numpy的polyfit,并为两个数组x和y获得了很好的拟合(使用7阶多项式)。我的关系就是这样;

I have used numpy's polyfit and obtained a very good fit (using a 7th order polynomial) for two arrays, x and y. My relationship is thus;

y(x) = p[0]* x^7 + p[1]*x^6 + p[2]*x^5 + p[3]*x^4 + p[4]*x^3 + p[5]*x^2 + p[6]*x^1 + p[7]

其中p是polyfit输出的多项式数组。

where p is the polynomial array output by polyfit.

有没有一种方法可以轻松地逆转此方法,所以我有以下形式的解决方案:

Is there a way to reverse this method easily, so I have a solution in the form of,

x(y) = p[0]*y^n + p[1]*y^n-1 + .... + p[n]*y^0


推荐答案


  1. 通常没有简单的方法。任意多项式的封闭式解决方案对于第七项的多项式不可用订购。

可以在反方向进行拟合,但只能在原始多项式的单调变化区域上进行。如果原始多项式在您感兴趣的域上具有最小值或最大值,则即使y是x的函数,x也不能是y的函数,因为它们之间没有一对一的关系。

Doing the fit in the reverse direction is possible, but only on monotonically varying regions of the original polynomial. If the original polynomial has minima or maxima on the domain you are interested in, then even though y is a function of x, x cannot be a function of y because there is no 1-to-1 relation between them.

如果您(i)可以通过重新执行拟合过程来确定,并且(ii)可以一次在您的拟合的单个单调区域上进行分段工作,那么您可以像这样的东西:

If you are (i) OK with redoing the fitting procedure, and (ii) OK with working piecewise on single monotonic regions of your fit at a time, then you could do something like this:

-

import numpy as np

# generate a random coefficient vector a
degree = 1
a = 2 * np.random.random(degree+1) - 1

# an assumed true polynomial y(x)
def y_of_x(x, coeff_vector):
    """
    Evaluate a polynomial with coeff_vector and degree len(coeff_vector)-1 using Horner's method.
    Coefficients are ordered by increasing degree, from the constant term at coeff_vector[0], 
        to the linear term at coeff_vector[1], to the n-th degree term at coeff_vector[n]
    """
    coeff_rev = coeff_vector[::-1]
    b = 0
    for a in coeff_rev:
        b = b * x + a
    return b


# generate some data
my_x = np.arange(-1, 1, 0.01)
my_y = y_of_x(my_x, a)


# verify that polyfit in the "traditional" direction gives the correct result
#     [::-1] b/c polyfit returns coeffs in backwards order rel. to y_of_x()
p_test = np.polyfit(my_x, my_y, deg=degree)[::-1]

print p_test, a

# fit the data using polyfit but with y as the independent var, x as the dependent var
p = np.polyfit(my_y, my_x, deg=degree)[::-1]

# define x as a function of y
def x_of_y(yy, a): 
    return y_of_x(yy, a)  


# compare results
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(my_x, my_y, '-b', x_of_y(my_y, p), my_y, '-r')

注意:此代码不检查单调性,而只是假设它。

通过计算学位 c的值,您应该看到代码只对<$的所有随机值有效 degree = 1 时,c $ c> a 。偶尔对其他学位也可以,但是当有很多最小值/最大值时则不行。对于 degree>来说,它永远做不完美。 1 ,因为用平方根函数近似抛物线并不总是有效,等等。

By playing around with the value of degree, you should see that see the code only works well for all random values of a when degree=1. It occasionally does OK for other degrees, but not when there are lots of minima / maxima. It never does perfectly for degree > 1 because approximating parabolas with square-root functions doesn't always work, etc.

这篇关于反向输出polyfit numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆