mat*_*att 7 python interpolation numpy scipy
我想在给定y值的固定向量的情况下在矩阵的每一行(x值)之间进行插值.我正在使用python,基本上我需要类似scipy.interpolate.interp1d但x值是矩阵输入的东西.我通过循环实现了这一点,但我希望尽可能快地进行操作.
编辑
下面是我现在正在做的代码示例,请注意我的矩阵有更多的行数量级:
import numpy as np
x = np.linspace(0,1,100).reshape(10,10)
results = np.zeros(10)
for i in range(10):
results[i] = np.interp(0.1,x[i],range(10))
Run Code Online (Sandbox Code Playgroud)
正如@Joe Kington 建议的那样,您可以使用map_coordinates:
import scipy.ndimage as nd
# your data - make sure is float/double
X = np.arange(100).reshape(10,10).astype(float)
# the points where you want to interpolate each row
y = np.random.rand(10) * (X.shape[1]-1)
# the rows at which you want the data interpolated -- all rows
r = np.arange(X.shape[0])
result = nd.map_coordinates(X, [r, y], order=1, mode='nearest')
Run Code Online (Sandbox Code Playgroud)
以上,对于以下情况y:
array([ 8.00091648, 0.46124587, 7.03994936, 1.26307275, 1.51068952,
5.2981205 , 7.43509764, 7.15198457, 5.43442468, 0.79034372])
Run Code Online (Sandbox Code Playgroud)
请注意,每个值指示将为每行插入该值的位置。
给出以下内容result:
array([ 8.00091648, 10.46124587, 27.03994936, 31.26307275,
41.51068952, 55.2981205 , 67.43509764, 77.15198457,
85.43442468, 90.79034372])
Run Code Online (Sandbox Code Playgroud)
考虑到 d 数据的性质以及对其进行插值的arange列 ( ),这是有意义的。y