代码之家  ›  专栏  ›  技术社区  ›  Jame

如果满足条件,如何将数组的值设置为数字?

  •  0
  • Jame  · 技术社区  · 6 年前

    我有一个矩阵 2x4x3x3 ,其中2表示两个矩阵的组合 A1 A2 是的。对于每个矩阵 A1/A2 (尺寸 4x3x3 ),我只选择大于 0.3 (小于 0.3分 打电话 不确定位置 )表演 argmax 获得矩阵 C 是的。在矩阵c中,我将在 不确定位置 到255。我用python中的两种方法完成了它,如下所示。我正在寻找第三种更短的方法来设置 不确定位置 到255。我们能要吗?谢谢

    代码是

    import numpy as np
    A1=np.array([[[0.4,0.2,0.3],
                [0.4,0.5,0.6],
                [0.7,0.8,0.2]],
                [[0.4,0.5,.3],
                [0.2,0.5,0.2],
                [0.3,0.2,0.1]],
                [[0.5,0.1,0.6],
                [0.8,0.1,0.5],
                [0.5,0.1,0.4]],
                [[0.6,0.1,0.1],
                [0.2,0.1,0.9],
                [0.9,0.1,0.1]]])
    A2=np.array([[[0.8,0.1,0.6],
                [0.4,0.6,0.6],
                [0.7,0.8,0.2]],
                [[0.4,0.8,.3],
                [0.2,0.8,0.2],
                [0.3,0.0,0.1]],
                [[0.5,0.1,0.6],
                [0.8,0.0,0.5],
                [0.5,0.1,0.4]],
                [[0.6,0.1,0.1],
                [0.2,0.1,0.1],
                [0.9,0.1,0.1]]])
    
    A1= np.expand_dims(A1,0)
    A2= np.expand_dims(A2,0)
    A= np.concatenate([A1,A2],axis=0)  
    B = A< 0.3
    
    #First way
    C=np.argmax(A, axis=1)
    C[B[:,0,:,:]]=255
    C[B[:,1,:,:]]=255
    C[B[:,2,:,:]]=255
    C[B[:,3,:,:]]=255
    print (C)
    
    #Second way
    C=np.argmax(A, axis=1)
    for i in range (4):
        C[B[:,i,:,:]] = 255
    print (C)
    
    #Third way
    C=np.argmax(A, axis=1)
    C[B[:,0:3,:,:]]=255
    print (C)
    

    你也可以运行它 Online Code

    0 回复  |  直到 6 年前