Skip to content Skip to sidebar Skip to footer

Numpy.any(axis=i) For Scipy.sparse

import numpy a = numpy.array([ [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], ]) numpy.any(a, axis=0) numpy.any(a, axis=1) produces arra

Solution 1:

you can use sum instead of any on bool arrays

import numpy
a = numpy.array([
    [0, 1, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
])

from scipy import sparse
a = sparse.csr_matrix(a.astype(bool))
# Use sum instead of any on a bool arrayprint(a.sum(axis=0).astype(bool))
print(a.sum(axis=1).flatten().astype(bool))

output:

[[ True  True  True False]][[ True  True  True False False]]

If you want to do 'all' that would be a little tricky since scipy doesn't appear to have an implementation for 'prod'. But this post has an answer for that case.

Post a Comment for "Numpy.any(axis=i) For Scipy.sparse"