【问题标题】:Mask a 2D Numpy Array by array of indexes like np.in1d for 2d arrays通过索引数组(如二维数组的 np.in1d)屏蔽 2D Numpy 数组
【发布时间】:2015-03-17 16:33:38
【问题描述】:
    np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,9,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,10,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,12,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,11],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

我正在尝试找到如何获取一个像上面这样的 numpy 数组,然后在一个高性能操作中用我想要归零的元素索引对其进行屏蔽

[0,1] [1,4] [4,7] [7,8] [8,11]

所以我剩下的就是

np.array(
[[0,0,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,0,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

类似于 np.in1d 的功能但对于 2d 数组?我可以遍历每个元素,但数组可以非常庞大,因此矢量单操作掩码是最好的。是否可以?如果这是一个愚蠢的问题,我相信我会被告知!

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    您可以通过以下方式直接访问这些索引

    indexes = [[0,1], [1,4], [4,7], [7,8], [8,11]]
    indexes =zip(*indexes)
    >>[(0, 1, 4, 7, 8), (1, 4, 7, 8, 11)]
    a[indexes[0], indexes[1]]=0
    >>
    [[ 0  0  0  2  0  0  0  0  0  0  0  0]
     [ 0  0 15  0  0  0  0  0  0  0  0  0]
     [ 0  0  0  0  0 18  0  0  0  0  0  0]
     [ 0  0  0  0 27  0 20  0  0  0  0  0]
     [ 0  0  0  0  0 20  0  0  0  0  0  0]
     [ 0  0  0  0  0  0  0  0  8  0  0  0]
     [ 0  0  0  0  0  0  0 14  0 14  0  0]
     [ 0  0  0  0  0  0  0  0  0  0 25  0]
     [ 0  0  0  0  0  0  0  0  0  0  0  0]
     [ 0  0  0  0  0  0  0  0  0  0 15  0]
     [ 0  0  0  0  0  0  0  0  0  0  0  7]
     [ 0  0  0  0  0  0  0  0  0  0  0  0]]
    

    【讨论】:

    • 如果你把indexes在压缩后变成tuple,你可以跳过索引,即indexes = tuple(zip(*indexes)); a[indexes] = 0
    【解决方案2】:

    我想你在找这个

    a = np.array(
    [[0,13,0,2,0,0,0,0,0,0,0,0],
     [0,0,15,0,9,0,0,0,0,0,0,0],
     [0,0,0,0,0,18,0,0,0,0,0,0],
     [0,0,0,0,27,0,20,0,0,0,0,0],
     [0,0,0,0,0,20,0,10,0,0,0,0],
     [0,0,0,0,0,0,0,0,8,0,0,0],
     [0,0,0,0,0,0,0,14,0,14,0,0],
     [0,0,0,0,0,0,0,0,12,0,25,0],
     [0,0,0,0,0,0,0,0,0,0,0,11],
     [0,0,0,0,0,0,0,0,0,0,15,0],
     [0,0,0,0,0,0,0,0,0,0,0,7],
     [0,0,0,0,0,0,0,0,0,0,0,0]])
    
    b = np.array([[0,1],[1,4],[4,7],[7,8],[8,11]])
    
    # get x coordinates in an array
    c1 = b[:,0]
    # get y coordinates in an array
    c2 = b[:,1]
    a[c1[:,None],c2] = 0
    
    a 
    array([[ 0,  0,  0,  2,  0,  0,  0,  0,  0,  0,  0,  0],
           [ 0,  0, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0, 18,  0,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0, 27,  0, 20,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0, 20,  0,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,  0],
           [ 0,  0,  0,  0,  0,  0,  0, 14,  0, 14,  0,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 15,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  7],
           [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]])
    

    【讨论】:

    • b 转换为数组后,a[tuple(b.T)] = 0 可能是完成任务的最紧凑方式。
    猜你喜欢
    • 2021-12-30
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多