【问题标题】:reverse effects of boolean masking in numpynumpy中布尔掩码的反向效果
【发布时间】:2016-12-30 20:32:28
【问题描述】:

设置

np.random.seed(314)
x = np.random.rand(10, 4)
mask np.array([True, False, False, True])

x

array([[ 0.91687358,  0.58854191,  0.26504775,  0.78320538],
       [ 0.91800106,  0.82735501,  0.72795148,  0.26048042],
       [ 0.9117634 ,  0.26075656,  0.76637602,  0.26153114],
       [ 0.12229137,  0.38600554,  0.84008124,  0.27817936],
       [ 0.06991369,  0.63310965,  0.58476603,  0.58123194],
       [ 0.6772054 ,  0.6871551 ,  0.43892737,  0.3209265 ],
       [ 0.57055222,  0.47984862,  0.86107434,  0.83480474],
       [ 0.10576611,  0.06040804,  0.59688219,  0.79239497],
       [ 0.22635574,  0.5352008 ,  0.13606616,  0.37224445],
       [ 0.15197674,  0.42982185,  0.79270622,  0.40695651]])

我可以像这样屏蔽 x:

y = x[:, mask]
y

array([[ 0.91687358,  0.78320538],
       [ 0.91800106,  0.26048042],
       [ 0.9117634 ,  0.26153114],
       [ 0.12229137,  0.27817936],
       [ 0.06991369,  0.58123194],
       [ 0.6772054 ,  0.3209265 ],
       [ 0.57055222,  0.83480474],
       [ 0.10576611,  0.79239497],
       [ 0.22635574,  0.37224445],
       [ 0.15197674,  0.40695651]])

问题

给定ymask 我如何生成:

array([[ 0.91687358,  0.        ,  0.        ,  0.78320538],
       [ 0.91800106,  0.        ,  0.        ,  0.26048042],
       [ 0.9117634 ,  0.        ,  0.        ,  0.26153114],
       [ 0.12229137,  0.        ,  0.        ,  0.27817936],
       [ 0.06991369,  0.        ,  0.        ,  0.58123194],
       [ 0.6772054 ,  0.        ,  0.        ,  0.3209265 ],
       [ 0.57055222,  0.        ,  0.        ,  0.83480474],
       [ 0.10576611,  0.        ,  0.        ,  0.79239497],
       [ 0.22635574,  0.        ,  0.        ,  0.37224445],
       [ 0.15197674,  0.        ,  0.        ,  0.40695651]])

【问题讨论】:

    标签: python numpy mask


    【解决方案1】:

    解决方案

    z = np.zeros((y.shape[0], len(mask)))
    z[:, mask] = y
    
    z
    
    array([[ 0.91687358,  0.        ,  0.        ,  0.78320538],
           [ 0.91800106,  0.        ,  0.        ,  0.26048042],
           [ 0.9117634 ,  0.        ,  0.        ,  0.26153114],
           [ 0.12229137,  0.        ,  0.        ,  0.27817936],
           [ 0.06991369,  0.        ,  0.        ,  0.58123194],
           [ 0.6772054 ,  0.        ,  0.        ,  0.3209265 ],
           [ 0.57055222,  0.        ,  0.        ,  0.83480474],
           [ 0.10576611,  0.        ,  0.        ,  0.79239497],
           [ 0.22635574,  0.        ,  0.        ,  0.37224445],
           [ 0.15197674,  0.        ,  0.        ,  0.40695651]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2020-04-23
      • 2019-10-29
      • 2018-05-27
      相关资源
      最近更新 更多