所以我有一组 x 值(按递增顺序)和相应的 y 值。 Numpy 的 interp 函数接收 X 值以及 x 和 y 数组。对于给定的 Y 值,如何获得 X 的值?例如,如果 y = 0,x = ?。
干杯!
代码:
j = (((1840/(2*pi))**0.5)*exp(phi)) - 1.0 #y axis, phi is a 1-D array
t = linspace(0, 40, 100) #x axis
a = interp(<x-value>,t,j) # this gives me the corresponding y value!
那么我应该怎么做才能得到给定 y 值的 x 值!!
最佳答案
y_interp = np.interp(x_interp, x, y)
根据先前的插值 y 生成函数
,其中 x.size = y.size,x_interp.size = y_interp.size。y_interp = f(x_interp)
的插值= f(x)
如果你想要 x 作为 y 的函数,你必须构造反函数。正如@NPE 指出的那样,您必须确保 x 和 y 始终在增加。一个简单的检查方法是使用
np.all(np.diff(x) > 0)
np.all(np.diff(y) > 0)
现在找到反函数实际上非常简单:您必须颠倒 x 和 y 的角色(因为我们处理的是插值)。
用我的符号:x_value = np.interp(y_value, x, y)
。
用你的符号:x_value = interp(y_value, j, t)
关于python - numpy interp 函数 - 如何找到给定 y 值的 x 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439107/