有产生从无序坐标点的值的2D阵列(即,不是所有LONS和/或拉特被升序或降序),而不使用循环的有效方式?
示例数据
lats = np.array([45.5,45.5,45.5,65.3,65.3,65.3,43.2,43.2,43.2,65.3])
lons = np.array([102.5,5.5,116.2,102.5,5.5,116.2,102.5,5.5,116.2,100])
vals = np.array([3,4,5,6,7,7,9,1,0,4])
Run Code Online (Sandbox Code Playgroud)
示例输出
每列代表一个唯一的经度(102.5,5.5,116.2和100),每列代表一个唯一的纬度(45.5,65.3,&43.2).
([ 3, 4, 5, NaN],
[ 6, 7, 7, 4],
[ 9, 1, 0, NaN])
Run Code Online (Sandbox Code Playgroud)
虽然,它不是那么直接,因为我不一定知道每个lon或lat有多少重复确定阵列的形状.
更新:
我的问题数据排列不正确.我现在已经安排好了,所以它们都是唯一的对,还有一个额外的数据点来演示当NaN存在时如何安排数据.
您发布的示例没有多大意义,并且它不允许任何合理的方式来指定缺少的数据.我在这里猜测,但你可能正在处理的唯一合理的事情似乎是这样的:
>>> 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])
Run Code Online (Sandbox Code Playgroud)
其中的值vals[j]来自纬度lats[j]和经度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])
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式将其排列为数组:
>>> 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.]])
Run Code Online (Sandbox Code Playgroud)