【问题标题】:How to invert scipy.interpolate.interp1d behaviour如何反转 scipy.interpolate.interp1d 行为
【发布时间】:2021-12-14 08:29:45
【问题描述】:

来自 scipy.interpolate.interp1d 的documentation

x 和 y 是用于逼近某个函数 f 的值数组:y = f(x)。该类返回一个函数,其调用方法使用插值来查找新点的值。

参数

x(N,) array_like:一维实数值数组。

y(…,N,…) array_like:一个 N 维实数值数组。 y沿插值轴的长度必须等于x的长度。

所以基本上,它假设我有一些x,我已经计算了多个我想要插值的y=f(x)。但我的情况正好相反:我有一个 y 数组,我想对许多不同的 x 数组进行插值。换句话说,我有这样的情况:

import numpy as np
from scipy.interpolate import interp1d

n_to_interpolate = 10

x = np.cumsum(np.random.rand(n_to_interpolate,100),axis=1)
y = np.linspace(0,1,100)

interp1d(x,y)

但这会产生错误,因为形状不匹配。有没有办法实现我想要的?

【问题讨论】:

  • 您能否澄清一下:我有一个 y 数组,我想对许多不同的 x 数组进行插值。换句话说?
  • @DaniMesejo 我计算了一些长度为 100 的输出 y。但是,有许多长度为 100 的可能输入 x 与此输出匹配。对于每个输入 x,我想运行 interp1d(没有 for 循环)(然后我将使用它进行插值)
  • y = np.linspace(0,1,n_to_interpolate),我的意思是,你需要有相同长度的 x 和 y 向量。
  • @Suthiro 不,这是不正确的,我想用 y 插入 x 的第二轴。
  • 所以你想要 10 个函数?

标签: python arrays scipy interpolation


【解决方案1】:

您需要遍历 x 数组的一维切片。

如果只需要线性插值,np.interpinterp1d

【讨论】:

  • 这是我目前的解决方案,但速度很慢。我目前使用np.interp,但它没有任何矢量化(因此一次只有一个插值)。我去了interp1d,因为它可以进行多次插值,但不幸的是,这与我想要的相反
猜你喜欢
  • 1970-01-01
  • 2013-09-10
  • 2021-07-24
  • 2019-01-24
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多