在并行Python中插值许多曲线

问题描述 投票:0回答:1

假设我有一个 N 行 M 列的 numpy 数组。每行包含 M 个间隔内 N 个函数中每个函数的值。我有一个向量 X,其中包含计算函数时的 x 值。我还有另一个 numpy 数组,它是长度为 N 的一维向量。我想对 N 个函数中的每一个进行插值,以在向量中的每个 N 点处找到它们各自的值。例如,如果我有

X = np.array([0.2,0.4,0.6,0.8])
matrix = np.array([[1,2,3,4],
                   [2,4,6,8],
                   [1,3,5,7]]) # N = 3, M = 4
vector = np.array([0.3, 0.6, 0.5])

def f(X, matrix, vector):
   # some code
   return interpolated_values

interpolated_values
应包含
[1.5, 6, 4]
,因为这些是
matrix
每行在
X
的对应值处线性插值时所具有的值。

我知道我可以使用循环来做到这一点。但是,如果 N 很大,这会很慢。函数

f
中应该包含什么来并行执行此插值?

python arrays numpy interpolation
1个回答
0
投票

根据您的需求,

interp
功能会很有用。

import numpy as np

def f(X, matrix, vector):
    # Use numpy.apply_along_axis to apply numpy.interp to each row of matrix
    interpolated_values = np.apply_along_axis(np.interp, 0, X, matrix, vector)
    return interpolated_values

© www.soinside.com 2019 - 2024. All rights reserved.