过去 4 天我在尝试理解 python 错误时遇到了一个问题:
IndexError: index 206893 is out of bounds for axis 0 with size 206893
应用时,
griddata
和“最近”插值方法使用以下行:
# create a matrix where I will store the first interpolated file
tempnew = np.ones((np.asarray(w1[0,0,:,:]).shape))*np.nan
# The lon, lat coordinate points of the original grid
lonl,latl = np.meshgrid(lon,lat)
points = np.vstack((np.array(lonl).flatten(),np.array(latl).flatten())).transpose()
# The values of the original file
values = np.array([np.asarray(temp[0,0,:,:])]).flatten()
# The dimensions of the grid that I want to interpolate to
lons = np.array(nav_lon)
lats = np.array(nav_lat)
X,Y = np.meshgrid(lons,lats)
# Interpolation
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
提前致谢, 索菲
我在使用
scipy.interpolate.NearestNDInterpolator
类的 Python 代码中遇到了此错误。返回的错误信息不是很清楚。最后,我发现我插入到插值中的值之一的值为 1e184
并导致了此错误消息。将此值重置为0.0
后,我的Python脚本成功运行。