`np.any` returns 来自对象数组而不是布尔值的对象?

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

好的,在尝试回答时 我遇到了一些非常奇怪的事情。

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)

好的,所以我想知道哪些行有数据,所以我尝试了:

np.any(l.rows)

Out: [8]

。 . . 什么?

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

Out: list

这是一个列表。里面有一个8。这似乎。 . .随机的。怎么回事?

经过一番研究,似乎 returns 数组中的第一个 object 不是 []

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]

但是考虑到np.any只应该输出booleannp.array布尔值,这是一个很奇怪的结果。有谁知道为什么会这样?

我找到了。显然它自上周以来一直在 Easy Fix list since 2014, but finally has someone working on it 上。

我应该知道我不是第一个尝试类似事情的假人。

此外,在这种情况下正确的用法是:

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