首页 > 解决方案 > 索引错误:索引 206893 超出轴 0 的范围,大小为 206893,griddata 问题

问题描述

在过去 4 天里,我遇到了一个问题,试图理解一个 python 错误:

`enter code here`IndexError: index 206893 is out of bounds for axis 0 with size 206893

应用时,griddata 和“最近”插值方法使用以下几行:

创建一个矩阵,我将在其中存储第一个插值文件

tempnew    = np.ones((np.asarray(w1[0,0,:,:]).shape))*np.nan

原始网格的经纬度坐标点

lonl,latl  = np.meshgrid(lon,lat)
points     = np.vstack((np.array(lonl).flatten(),np.array(latl).flatten())).transpose()

原始文件的值

values     = np.array([np.asarray(temp[0,0,:,:])]).flatten()

我要插值的网格的尺寸

lons       = np.array(nav_lon)
lats       = np.array(nav_lat)
X,Y        = np.meshgrid(lons,lats)

插值

tempnew    = griddata(points,values, (X,Y), method = "nearest",fill_value=-3)

这里是我上面使用的每个变量的维度:

 #tempnew.shape: (728, 312) #(Dimensions of tempnew is (lats,lons))

 #lat.shape: (661,) #(original latitude)

 #lon.shape: (313,) #(original longitude)

 #points.shape: (206893, 2)

 #values.shape: (206893,)

 #X.shape: (728, 312)

 #Y.shape: (728, 312)

你能帮助我吗?* 我想在这里指出,原始文件网格是常规(A 型)网格数据,而我要插入到的网格不是常规(C 网格数据)

错误如下所示:

In [36]: tempnew    = sp.interpolate.griddata(points,values, (X,Y), method = "nearest
...: ",fill_value=-3)                                                            
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-36-0d0b46a3542f> in <module>
----> 1 tempnew    = sp.interpolate.griddata(points,values, (X,Y), method = 
"nearest",fill_value=-3)

~/software/anaconda3/envs/mhw/lib/python3.7/site-packages/scipy/interpolate/ndgriddata.py in 
griddata(points, values, xi, method, fill_value, rescale)
217     elif method == 'nearest':
218         ip = NearestNDInterpolator(points, values, rescale=rescale)
--> 219         return ip(xi)
220     elif method == 'linear':
221         ip = LinearNDInterpolator(points, values, fill_value=fill_value,

~/software/anaconda3/envs/mhw/lib/python3.7/site-packages/scipy/interpolate/ndgriddata.py in 
__call__(self, *args)
 79         xi = self._scale_x(xi)
 80         dist, i = self.tree.query(xi)
 ---> 81         return self.values[i]
 82 
 83 

IndexError: index 206893 is out of bounds for axis 0 with size 206893

提前致谢, 索菲

标签: pythonarraysindexinginterpolationbounds

解决方案


我在使用scipy.interpolate.NearestNDInterpolator该类的 Python 代码中遇到了这个错误。返回的错误信息不是很清楚。最后,我发现我插入到插值中的一个值的值为1e184并导致了此错误消息。将此值重置为 后0.0,我的 Python 脚本运行成功。


推荐阅读