我正在尝试对 2D 数据进行插值以查找点 (X,Y) 处的 Z 值,就像它是 2D 查找表一样。
import numpy as np
import pandas as pd
from scipy.interpolate import RegularGridInterpolator
import io
xi, yi = 100, 100; # test points I want to evaluate the interpolator object at
# the 2D data copied from google sheets and pasted into Jupyter notebook generates this dataframe:
values = pd.read_csv(io.StringIO('''
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,221,220,219,218,217,217,215,215,215,217,217,218,218,219,220,221,222,222,222
223,223,223,223,223,223,223,223,223,219,218,217,215,215,210,210,210,215,217,217,217,218,219,220,221,222,222
223,223,223,223,223,223,223,223,223,218,217,217,215,210,207,207,207,210,215,215,217,218,219,219,220,221,222
223,223,223,223,223,223,223,223,218,217,223,215,210,207,205,205,207,207,210,210,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,218,217,216,215,207,205,203,203,205,207,207,210,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,218,217,216,215,207,205,203,203,205,207,210,212,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,223,218,217,217,215,207,205,205,207,210,212,215,217,218,219,220,221,222,223
223,223,223,223,223,223,223,223,223,223,218,218,217,215,207,207,215,215,215,217,218,219,220,221,222,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
'''), header=None);
# these are the X and Y values corresponding to the X and Y dimensions of the data (values).
X = np.linspace(0, 2600, values.shape[1]);
Y = np.linspace(0, 1700, values.shape[0]);
Yi, Xi = np.meshgrid(Y, X);
interp = RegularGridInterpolator((Y, X), values); # X and Y are switched here because Y corresponds to the rows of the dataframe and X corresponds to the columns but I'm matching the SciPy syntax.
interp(np.array([[xi], [yi]]).T) # this is the portion I'm having trouble with
我不明白我放入 interp 对象中的参数的格式是什么。从 documentation 来看,它说输入是 ndarray 的元组。但是这个示例显示了在 np.array 和列表元组上评估 interp 对象。我已经尝试过这些以及它们之间所有内容的组合,但我仍然没有得到正确的结果...感谢您的帮助。
上述代码返回以下错误:
InvalidIndexError: (array([1]), array([1]))
我试图返回单个点的插值(例如,for 循环的每次迭代)。或者如果有必要的话,在 for 循环之后立即对所有点进行操作。
您指向的文档正在使用数组。
您正在使用数据框。基于 'How to Perform interpolation on 2d grid from dataframe in python?',您可以使用数据框的
values
属性:
import numpy as np
import pandas as pd
from scipy.interpolate import RegularGridInterpolator
import io
xi, yi = 100, 100; # test points I want to evaluate the interpolator object at
# the 2D data copied from google sheets and pasted into Jupyter notebook generates this dataframe:
values = pd.read_csv(io.StringIO('''
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,221,220,219,218,217,217,215,215,215,217,217,218,218,219,220,221,222,222,222
223,223,223,223,223,223,223,223,223,219,218,217,215,215,210,210,210,215,217,217,217,218,219,220,221,222,222
223,223,223,223,223,223,223,223,223,218,217,217,215,210,207,207,207,210,215,215,217,218,219,219,220,221,222
223,223,223,223,223,223,223,223,218,217,223,215,210,207,205,205,207,207,210,210,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,218,217,216,215,207,205,203,203,205,207,207,210,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,218,217,216,215,207,205,203,203,205,207,210,212,215,217,218,219,220,221,222
223,223,223,223,223,223,223,223,223,218,217,217,215,207,205,205,207,210,212,215,217,218,219,220,221,222,223
223,223,223,223,223,223,223,223,223,223,218,218,217,215,207,207,215,215,215,217,218,219,220,221,222,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223
'''), header=None);
# these are the X and Y values corresponding to the X and Y dimensions of the data (values).
X = np.linspace(0, 2600, values.shape[1]);
Y = np.linspace(0, 1700, values.shape[0]);
Yi, Xi = np.meshgrid(Y, X);
interp = RegularGridInterpolator((Y, X), values.values); # X and Y are switched here because Y corresponds to the rows of the dataframe and X corresponds to the columns but I'm matching the SciPy syntax.
或者,您可以将 DataFrame 转换为 numpy 数组,然后。 转置数组以匹配预期形状 (
Y
, X
),添加 values_array = values.to_numpy().T
,然后也调整插值调用 interp = RegularGridInterpolator((X, Y), values_array)
。