您发布的示例几乎没有意义,并且不允许任何合理的方式来指定丢失的数据。我在这里猜测,但您可能正在处理的唯一合理的事情似乎是这样的:
>>> lats = np.array([43.2, 43.2, 43.2, 45.5, 45.5, 45.5, 65.3, 65.3, 65.3])
>>> lons = np.array([5.5, 102.5, 116.2, 5.5, 102.5, 116.2, 5.5, 102.5, 116.2])
>>> vals = np.array([3, 4, 5, 6, 7, 7, 9, 1, 0])
其中 in 的值vals[j]
来自 latitudelats[j]
和 longitude lons[j]
,但数据可能会被打乱,如:
>>> indices = np.arange(9)
>>> np.random.shuffle(indices)
>>> lats = lats[indices]
>>> lons = lons[indices]
>>> vals = vals[indices]
>>> lats
array([ 45.5, 43.2, 65.3, 45.5, 43.2, 65.3, 45.5, 65.3, 43.2])
>>> lons
array([ 5.5, 116.2, 102.5, 116.2, 5.5, 116.2, 102.5, 5.5, 102.5])
>>> vals
array([6, 5, 1, 7, 3, 0, 7, 9, 4])
您可以将其排列成一个数组,如下所示:
>>> lat_vals, lat_idx = np.unique(lats, return_inverse=True)
>>> lon_vals, lon_idx = np.unique(lons, return_inverse=True)
>>> vals_array = np.empty(lat_vals.shape + lon_vals.shape)
>>> vals_array.fill(np.nan) # or whatever yor desired missing data flag is
>>> vals_array[lat_idx, lon_idx] = vals
>>> vals_array
array([[ 3., 4., 5.],
[ 6., 7., 7.],
[ 9., 1., 0.]])