JcM*_*aco 13 python interpolation numpy scientific-computing scipy
有没有更好的方法可以找到哪个X给了我在SciPy中寻找的Y?我刚开始使用SciPy,我对每个功能都不太熟悉.
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
x = [70, 80, 90, 100, 110]
y = [49.7, 80.6, 122.5, 153.8, 163.0]
tck = interpolate.splrep(x,y,s=0)
xnew = np.arange(70,111,1)
ynew = interpolate.splev(xnew,tck,der=0)
plt.plot(x,y,'x',xnew,ynew)
plt.show()
t,c,k=tck
yToFind = 140
print interpolate.sproot((t,c-yToFind,k)) #Lowers the spline at the abscissa
Run Code Online (Sandbox Code Playgroud)
ihu*_*ton 18
scipy中的UnivariateSpline类使得splines更加pythonic.
x = [70, 80, 90, 100, 110]
y = [49.7, 80.6, 122.5, 153.8, 163.0]
f = interpolate.UnivariateSpline(x, y, s=0)
xnew = np.arange(70,111,1)
plt.plot(x,y,'x',xnew,f(xnew))
Run Code Online (Sandbox Code Playgroud)
要在y处找到x,请执行以下操作:
yToFind = 140
yreduced = np.array(y) - yToFind
freduced = interpolate.UnivariateSpline(x, yreduced, s=0)
freduced.roots()
Run Code Online (Sandbox Code Playgroud)
我认为用y来插值x可能会有效,但它需要一个不同的路线.它可能更接近更多点.