python - numpy interp 函数 - 如何找到给定 y 值的 x 值?

标签 python arrays numpy

所以我有一组 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 生成函数 y_interp = f(x_interp) 的插值= f(x),其中 x.size = y.size,x_interp.size = y_interp.size。

如果你想要 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/

相关文章:

python - 无法从 Json 中取出单个值

java - 如何在 java spring boot 中将字节数组作为内存文件返回?

python - scipy 中的向量化输入方式 logsumexp

python - pytest assert_used_with 不适用于 Django 管理命令

python - 如何更改 pandas groupby.agg 函数的输入参数?

javascript - 替换数组中的一个字符串

c# - 从字节数组或流中获取文件名

python - 用 * 运算符 reshape

python - 在Python中找到大稀疏矩阵的2个最大特征值

python - 我可以使用 ENV 变量关闭 Python (PiP) SSL 证书验证吗?