polyfit numpy的反向输出
Reverse output of polyfit numpy
我使用了 numpy 的 polyfit 并获得了两个数组 x 和 y 的非常好的拟合(使用 7 阶多项式)。我的关系是这样的;
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 输出的多项式数组。
有没有办法轻松逆转这个方法,所以我有一个解决方案,
x(y) = p[0]*y^n + p[1]*y^n-1 + .... + p[n]*y^0
不,总的来说没有简单的方法。任意多项式的封闭形式解 are not available 用于七阶多项式。
反向拟合是可能的,但仅限于原始多项式的单调变化区域。如果原始多项式在你感兴趣的域上有最小值或最大值,那么即使y是x的函数,x也不能是y的函数,因为它们之间没有一对一的关系。
如果您 (i) 可以重做拟合程序,并且 (ii) 可以一次在拟合的单个单调区域上分段工作,那么您可以这样做:
-
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')
注意:此代码不检查单调性,而只是假设它。
通过尝试 degree
的值,您应该看到当 degree=1
时,代码仅适用于 a
的所有随机值。它偶尔对其他度数也可以,但当有很多最小值/最大值时就不行了。对于 degree > 1
它永远不会完美,因为用平方根函数逼近抛物线并不总是有效,等等
我使用了 numpy 的 polyfit 并获得了两个数组 x 和 y 的非常好的拟合(使用 7 阶多项式)。我的关系是这样的;
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 输出的多项式数组。
有没有办法轻松逆转这个方法,所以我有一个解决方案,
x(y) = p[0]*y^n + p[1]*y^n-1 + .... + p[n]*y^0
不,总的来说没有简单的方法。任意多项式的封闭形式解 are not available 用于七阶多项式。
反向拟合是可能的,但仅限于原始多项式的单调变化区域。如果原始多项式在你感兴趣的域上有最小值或最大值,那么即使y是x的函数,x也不能是y的函数,因为它们之间没有一对一的关系。
如果您 (i) 可以重做拟合程序,并且 (ii) 可以一次在拟合的单个单调区域上分段工作,那么您可以这样做:
-
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')
注意:此代码不检查单调性,而只是假设它。
通过尝试 degree
的值,您应该看到当 degree=1
时,代码仅适用于 a
的所有随机值。它偶尔对其他度数也可以,但当有很多最小值/最大值时就不行了。对于 degree > 1
它永远不会完美,因为用平方根函数逼近抛物线并不总是有效,等等