如何在Python中产生条件矩阵?

时间:2019-07-19 14:47:39

标签: python numpy vectorization

我正在做一个模拟,我想建立一个numpy矩阵来表示模拟的状态。

例如,通过仿真,我得到了一个矩阵 A =

matrix( [[0.        , 0.024     , 0.088     , 0.154     , 0.206      ],
         [0.        , 0.3300    , 0.654     , 1         , 0.5        ],
         [0.        , 0.1770    , 0.371     , 0.5149487 , 0.610      ],
         [0.        , 0.        , 0.5       , 0.8       , 0.9        ],
         [0.        , 0.        , 1         , 0.9       , 0.8        ]])

If A[i,j]>=1:
  B[i,j]=1
else:
  B[i,j]=0

If in one row, one element >=1, the following elements in that row all equal to 1.如果要在不使用for循环的情况下实现此目标,该怎么办?

我想要得到的B是:

matrix([[0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 1, 1, 1]])

2 个答案:

答案 0 :(得分:2)

您可以检查哪些值大于或等于1,并将cumsum的设置axis设置为1,将dtype设置为{{1} }:

bool

答案 1 :(得分:2)

在与1进行比较之后,我们可以使用np.maximum.accumulate

输入:

In [79]: a
Out[79]: 
matrix([[0.       , 0.024    , 0.088    , 0.154    , 0.206    ],
        [0.       , 0.33     , 0.654    , 1.       , 0.5      ],
        [0.       , 0.177    , 0.371    , 0.5149487, 0.61     ],
        [0.       , 0.       , 0.5      , 0.8      , 0.9      ],
        [0.       , 0.       , 1.       , 0.9      , 0.8      ]])

导致解决方案的步骤:

# Compare against 1
In [93]: a>=1
Out[93]: 
matrix([[False, False, False, False, False],
        [False, False, False,  True, False],
        [False, False, False, False, False],
        [False, False, False, False, False],
        [False, False,  True, False, False]])

# Get accumulated max along each row. Thus, it makes sure that once we
# encounter a match(True), it's maintained till the end.
In [91]: np.maximum.accumulate(a>=1,axis=1)
Out[91]: 
matrix([[False, False, False, False, False],
        [False, False, False,  True,  True],
        [False, False, False, False, False],
        [False, False, False, False, False],
        [False, False,  True,  True,  True]])

# View as int8/uint8 dtype. It's meant for memory efficiency to have
# the final output as int dtype
In [92]: np.maximum.accumulate(a>=1,axis=1).view('i1')
Out[92]: 
matrix([[0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 1, 1, 1]], dtype=int8)

在所有提议的解决方案上对大型数据集(给定样本沿行和列重复10000x)进行计时(因为似乎我们关心性能)-

# Repeated along rows
In [106]: ar = np.repeat(a,10000,axis=0)

In [108]: %timeit (ar >= 1.).cumsum(axis=1, dtype=bool).view('i1')
     ...: %timeit np.maximum.accumulate(ar>=1,axis=1).view('i1')
582 µs ± 1.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
593 µs ± 15 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# Repeated along rows and cols
In [109]: ar = np.repeat(np.repeat(a,1000,axis=0),1000,axis=1)

In [110]: %timeit (ar >= 1.).cumsum(axis=1, dtype=bool).view('i1')
     ...: %timeit np.maximum.accumulate(ar>=1,axis=1).view('i1')
77.9 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
77.3 ms ± 628 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)