小编hir*_*228的帖子

在scipy稀疏矩阵上将掩码为True的元素设置为零的有效方法

我有两个scipy_sparse_csr_matrix'a'和scipy_sparse_csr_matrix(boolean)'mask',我想将'a'的元素设置为零,其中mask的元素为True.

例如

>>>a
<3x3 sparse matrix of type '<type 'numpy.int32'>'
    with 4 stored elements in Compressed Sparse Row format>
>>>a.todense()
matrix([[0, 0, 3],
        [0, 1, 5],
        [7, 0, 0]])

>>>mask
<3x3 sparse matrix of type '<type 'numpy.bool_'>'
    with 4 stored elements in Compressed Sparse Row format>
>>>mask.todense()
matrix([[ True, False,  True],
        [False, False,  True],
        [False,  True, False]], dtype=bool)
Run Code Online (Sandbox Code Playgroud)

然后我想获得以下结果.

>>>result
<3x3 sparse matrix of type '<type 'numpy.int32'>'
    with 2 stored elements in Compressed Sparse Row format>
>>>result.todense()
matrix([[0, 0, 0], …
Run Code Online (Sandbox Code Playgroud)

python mask matrix scipy sparse-matrix

9
推荐指数
1
解决办法
2166
查看次数

标签 统计

mask ×1

matrix ×1

python ×1

scipy ×1

sparse-matrix ×1