Performance 检查坐标列表中所有值的最快方法
我有一个坐标列表Performance 检查坐标列表中所有值的最快方法,performance,numpy,Performance,Numpy,我有一个坐标列表a=[(1,2),(1300),(2,3)…] 这些值是1000 x 1000 NumPy阵列的面积坐标 假设我要求这些坐标下的所有值的和。有没有比以下更快的方法: sum([array[i[0],i[1]] for i in a]) 使用a将掩码应用于数组,然后对掩码数组求和。例如: # Prepare sample array and indices a = np.arange(10*10).reshape(10,10) ind = [(1,0), (2, 4), (2,6
a=[(1,2),(1300),(2,3)…]
这些值是1000 x 1000 NumPy
阵列的面积坐标
假设我要求这些坐标下的所有值的和。有没有比以下更快的方法:
sum([array[i[0],i[1]] for i in a])
使用
a
将掩码应用于数组
,然后对掩码数组求和。例如:
# Prepare sample array and indices
a = np.arange(10*10).reshape(10,10)
ind = [(1,0), (2, 4), (2,6), (7,7), (8,9), (9,3)]
# Cast list of coordinates into a form that will work for indexing
indx = np.split(np.array(ind), 2, axis = 1)
# A warning may be raised about not using tuples for indexing. You can use tuple(indx) to avoid that.
np.sum(a[indx])
使用
a
将掩码应用于数组
,然后对掩码数组求和。例如:
# Prepare sample array and indices
a = np.arange(10*10).reshape(10,10)
ind = [(1,0), (2, 4), (2,6), (7,7), (8,9), (9,3)]
# Cast list of coordinates into a form that will work for indexing
indx = np.split(np.array(ind), 2, axis = 1)
# A warning may be raised about not using tuples for indexing. You can use tuple(indx) to avoid that.
np.sum(a[indx])