我使用维度( Xpos
)屏蔽了纬度(Ypos
)和经度(125,800,000
)的数组。
我想计算数组中纬度和经度的差异。这是数组Xpos
(Ypos
类似)。
masked_array( data = [ [-2.0551843643188477, -2.637551784515381, -2.720881223678589, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ],
[-2.3242127895355225, -2.804257869720459, -2.8825504779815674, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ],
[-2.073770523071289, -2.6198980808258057, -2.708889961242676, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ],
...,
[-3.517531633377075, -2.908338785171509, -2.9069409370422363, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ],
[-3.688690662384033, -3.0086288452148438, -3.010164260864258, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ],
[-3.520817518234253, -2.943941116333008, -2.941941738128662, ..., 2.2812530994415283, 2.281250476837158, 2.281254768371582 ]
],
mask = [ [ False, False, False, ..., False, False, False ],
[ False, False, False, ..., False, False, False],
[ False, False, False, ..., False, False, False],
...,
[ False, False, False, ..., False, False, False],
[ False, False, False, ..., False, False, False],
[ False, False, False, ..., False, False, False]
],
fill_value = 1e+20,
dtype = float32
)
这是我的代码,它可以运行,但需要超长时间来计算。
Dist= np.zeros((len(XposApr),len(XposApr[0])))
DiffLon=np.zeros((len(XposApr),len(XposApr[0])))
DiffLat=np.zeros((len(XposApr),len(XposApr[0])))
for i in range (1,len(XposApr),12):
for j in range (0,len(XposApr[0])):
DiffLon[i][j]=(XposApr[i][j]-XposApr[i-1][j])
DiffLat[i][j]=(YposApr[i][j]-YposApr[i-1][j])
我真的不知道如何制作着名的单行,这是我尝试的,但不起作用:
DistLon = [XposApr[i][j]-XposApr[i-1][j] for i in enumerate (XposApr) and j in enumerate (XposApr[0])]
是否可以制作一个oneliner或其他代码,使计算更快? 提前谢谢!
答案 0 :(得分:0)
是否可以制作oneliner或其他代码,使计算更快?
直到光荣的 numba
-team决定将支持掩码阵列的操作转移到前置刻录机上,来自numba.jit( ... )
的JIT加速速度不足(Masked array support #2103)
让我们试试numpy
- 原生步骤:
内环可能首先退出慢速GIL驱动的运动:
for i in range ( 1, len( XposApr ), 12 ):
#or j in range ( 0, len( XposApr[0] ) ):
DiffLon[i][:] = ( XposApr[i][:] - XposApr[i-1][:] )
DiffLat[i][:] = ( YposApr[i][:] - YposApr[i-1][:] )
然而,如果我正确地阅读了布局,可以通过这样的方式解决整个问题:
DiffLon[1:len( XposApr ):12] = ( XposApr[1:len( XposApr ):12]
- XposApr[0:len( XposApr )-1:12]
)
DiffLat[1:len( XposApr ):12] = ( YposApr[1:len( XposApr ):12]
- YposApr[0:len( XposApr )-1:12]
)
我相信@Divakar或@cᴏʟᴅsᴘᴇᴇᴅ,真正的numpy
- 这里的大师会准备好告诉你更聪明,更有效的切片/矢量化技巧来做同样的事,请耐心等待:o)
答案 1 :(得分:0)
感谢您的回答和@patrick的有用评论,我相信这也可以解决我的问题:
Xapr=np.array(XposApr)
Yapr=np.array(YposApr)
for i in range (1,len(XposApr)):
DiffLon[i]=(Xapr[i]-Xapr[i-1])
DiffLat[i]=(Yapr[i]-Yapr[i-1])