简体   繁体   English

如何反转 scipy.interpolate.interp1d 行为

[英]How to invert scipy.interpolate.interp1d behaviour

From the documentation of scipy.interpolate.interp1d:从 scipy.interpolate.interp1d 的文档

x and y are arrays of values used to approximate some function f: y = f(x). x 和 y 是用于逼近某个函数 f 的值数组:y = f(x)。 This class returns a function whose call method uses interpolation to find the value of new points.此类返回一个函数,其调用方法使用插值来查找新点的值。

Parameters参数

x(N,) array_like: A 1-D array of real values. x(N,) array_like:实数值的一维数组。

y(…,N,…) array_like: A ND array of real values. y(...,N,...) array_like:一个 ND 实数值数组。 The length of y along the interpolation axis must be equal to the length of x. y 沿插值轴的长度必须等于 x 的长度。

So basically, it assumes that I have some x for which I have calculated multiple y=f(x) that I want to interpolate.所以基本上,它假设我有一些x ,我已经计算了多个我想要插值的y=f(x) But my case is exactly opposite: I have a single y array that I want to interpolate for many different x arrays.但我的情况正好相反:我有一个y数组,我想为许多不同的x数组进行插值。 In other words, I have the situation:换句话说,我有这样的情况:

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)

But this gives an error because the shapes do not match.但这会产生错误,因为形状不匹配。 Is there a way to achieve what I want?有没有办法实现我想要的?

You'd need to loop over the 1d slices of the x array.您需要遍历x数组的一维切片。

If you only need a linear interpolation, np.interp is faster then interp1d如果你只需要一个线性插值, np.interp更快然后interp1d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM