`np.any` returns an object from an object array instead of boolean?

Viewed 76

Ok, in trying to answer this question I came across something very strange.

matrix = np.zeros(10000)
matrix[np.random.choice(10000, 100)] = np.random.rand(100)
matrix = matrix.reshape(10, 1000)

from scipy.sparse import lil_matrix
l = lil_matrix(matrix.T)
l.rows

Out: array([[], [], [], ..., [], [], []], dtype=object)

Ok, so I want to know which rows have data, so I tried:

np.any(l.rows)

Out: [8]

. . . what?

out = np.any(l.rows)
type(out)

Out: list

It's a list. With an 8 in it. Which seems . . . random. What is going on?

After playing around it seems it returns the first object in the array that's not [].

np.random.seed(9)
matrix = np.zeros(10000)
matrix[np.random.choice(10000, 100)] = np.random.rand(100)
matrix = matrix.reshape(10, 1000)

from scipy.sparse import lil_matrix
l = lil_matrix(matrix.T)
l.rows

Out: array([[], [], [5], ..., [], [], []], dtype=object)

np.any(l.rows)
Out: [5]

But considering np.any is only supposed to output boolean or np.array of boolean, this is a very strange result. Does anyone know why this happens?

1 Answers

I found it. Apparently it's been on the Easy Fix list since 2014, but finally has someone working on it since last week.

Should have figured I'm not the first dummy to try something like that.

Also, the correct usage in this case would be:

l[l.rows.astype(bool)]
Out: 
<97x10 sparse matrix of type '<class 'numpy.float64'>'
    with 100 stored elements in LInked List format>
Related