分散数据帧的快速插值

Fast interpolation of a scattered DataFrame

TL;DR: 问题:有没有一种快速的方法可以在特定坐标处对分散的 2D 数据集进行插值?

如果可以的话,有人可以提供一个示例,其中包含 "Current Solution" 中使用的示例数据和变量(因为我自己实现它显然很愚蠢)。


问题:

我需要在特定坐标点内插(如果可能的话还外推)分散数据的 DataFrame(大小 = (34, 18))。 DataFrame 始终保持不变。

插值需要快,因为它在一个循环中完成超过 10.000 次。

插值的坐标是事先不知道的,因为它们在每个循环中都会改变。


当前解:

def Interpolation(a, b):

    #import external modules
    import pandas as pd
    from scipy import interpolate

    #reading .xlsx file into DataFrame
    file  = pd.ExcelFile(file_path)
    mr_df = file.parse('Model_References')
    matrix = mr_df.set_index(mr_df.columns[0])

    #interpolation at specific coordinates
    matrix = Matrix.stack().reset_index().values
    value = interpolate.griddata(matrix[:,0:2], matrix[:,2], (a, b), method='cubic')

    return(value)

这种方法不能长时间使用,仅#interpolation at specific coordinates下的两行代码就占了95%以上的执行时间


我的想法:


示例数据:

          0.0     0.1     0.2     0.3
0.0      -407    -351    -294    -235
0.0001   -333    -285    -236    -185
0.0002   -293    -251    -206    -161
0.00021  -280    -239    -196    -151

多亏了@Jdog的评论我才弄明白:

使用scipy.interpolate.RectBivariateSpline在循环之前创建一次样条曲线并使用scipy.interpolate.RectBivariateSpline.ev读取特定坐标将插值的执行时间从255秒减少到289毫秒。

def Interpolation(mesh, a, b):

    #interpolation at specific coordinates
    value = mesh.ev(stroke, current)

    return(value)

#%%

#import external modules
import pandas as pd
from scipy import interp

#reading .xlsx file into DataFrame
file  = pd.ExcelFile(file_path)
mr_df = file.parse('Model_References')
matrix = mr_df.set_index(mr_df.columns[0])

mesh = interp.RectBivariateSpline(a_index, b_index, matrix)

for iterations in loop:
    value = Interpolation(mesh, a, b)