如何将此python循环转换为矢量编码?

时间:2017-07-02 01:54:20

标签: python numpy

如何将此循环代码转换为矢量符号?我尝试了很多东西,包括试图获得逻辑_但它没有广播。

import numpy as np

coord_mask = np.zeros((10, 5), dtype=np.bool)
latx = np.random.choice(a=[False, True], size=10)
laty = np.random.choice(a=[False, True], size=5)

for i in range(0, coord_mask.shape[0]):
    for j in range(0, coord_mask.shape[1]):
        coord_mask[i, j] = latx[i] * laty[j]

print(coord_mask)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

对我而言

coord_mask = np.outer(latx, laty) 

应该做的伎俩。

答案 1 :(得分:2)

选择:

In [629]: coord_mask = np.zeros((10, 5), dtype=np.bool)
     ...: latx = np.random.choice(a=[False, True], size=10)
     ...: laty = np.random.choice(a=[False, True], size=5)
     ...: 
     ...: for i in range(0, coord_mask.shape[0]):
     ...:     for j in range(0, coord_mask.shape[1]):
     ...:         coord_mask[i, j] = latx[i] * laty[j]
     ...:         
In [630]: coord_mask
Out[630]: 
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

广播乘法:( Nonelatx转换为(n,1)列矩阵,该矩阵多次a(m,)laty(等效(1,m)),产生(n,m)结果。这是一个非常方便,功能强大的numpy工具。

In [631]: latx[:,None]*laty
Out[631]: 
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

outer

In [632]: np.outer(latx, laty)
Out[632]: 
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)
点积的einsum推广:

In [633]: np.einsum('i,j',latx, laty)
Out[633]: 
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

使用broadcasting方法,您可以替换另一个二进制操作,例如&

In [634]: latx[:,None] & laty
Out[634]: 
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)