You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SciPy RegularGridInterpolator进行2D插值的输入参数问题

2D查找表插值问题解决(基于SciPy的RegularGridInterpolator)

问题描述

尝试将2D数据作为查找表,使用RegularGridInterpolator插值获取点(X,Y)处的Z值,但代码运行报错:

InvalidIndexError: (array([1]), array([1]))

需要实现单点(如循环迭代中)或批量点的插值计算。

原始代码如下:

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

错误原因

  1. 数据类型不匹配:RegularGridInterpolator的values参数要求传入numpy数组,而原始代码中直接传入了pandas DataFrame对象,导致内部索引处理出错。
  2. 插值点顺序错误:插值器初始化时使用的网格顺序是(Y, X)(对应数据的行和列),因此传入的插值点必须是先Y坐标,后X坐标,原始代码中顺序颠倒。
  3. 插值点形状错误:单点插值需要传入形状为(2,)或(1,2)的数组,批量点需要(N,2)的数组,原始代码的形状处理不符合要求。

修正后的解决方案

1. 单点插值实现

import numpy as np
import pandas as pd
from scipy.interpolate import RegularGridInterpolator
import io

xi, yi = 100, 100  # 目标点(X,Y)

# 读取数据并转为numpy数组
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,2
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:46:03