首页 > 解决方案 > 分散数据帧的快速插值

问题描述

TL;DR:问题:有没有一种快速的方法可以在特定坐标处插入分散的二维数据集?

如果是这样,有人可以提供一个示例,其中提供了“当前解决方案”中使用的示例数据和变量(因为我自己实现它显然很愚蠢)。


问题:

我需要在特定坐标点内插(如果可能的话还外推)一个分散数据的 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

不

标签: pythonpython-3.xinterpolation

解决方案


感谢@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)

推荐阅读