分散数据帧的快速插值
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%以上的执行时间
我的想法:
- scipy.interpolate.Rbf 如果需要对数据进行内插和外推,似乎是最好的解决方案,但据我了解,它只会创建现有数据的更精细网格,无法在特定坐标处输出内插值
- 在特定坐标 (a,b) 周围创建一个较小的 4x4 矩阵可能会减少每个循环的执行时间,但我确实在如何使用
griddata
和较小的矩阵方面遇到困难。我创建了一个 5x5 矩阵,第一行和第一列是索引,其他 4x4 条目是中间具有特定坐标的数据。
但是我得到一个 TypeError: list indices must be integers or slices, not tuple
,我不明白,因为我没有改变任何其他东西。
示例数据:
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)
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%以上的执行时间
我的想法:
- scipy.interpolate.Rbf 如果需要对数据进行内插和外推,似乎是最好的解决方案,但据我了解,它只会创建现有数据的更精细网格,无法在特定坐标处输出内插值
- 在特定坐标 (a,b) 周围创建一个较小的 4x4 矩阵可能会减少每个循环的执行时间,但我确实在如何使用
griddata
和较小的矩阵方面遇到困难。我创建了一个 5x5 矩阵,第一行和第一列是索引,其他 4x4 条目是中间具有特定坐标的数据。 但是我得到一个TypeError: list indices must be integers or slices, not tuple
,我不明白,因为我没有改变任何其他东西。
示例数据:
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)