你也可以选择不那么老套的,但可能更慢:
m = m.multiply(m >= 10)
要了解发生了什么:
>>> m = scipy.sparse.csr_matrix((1000, 1000), dtype=np.int)
>>> m[np.random.randint(0, 1000, 20),
np.random.randint(0, 1000, 20)] = np.random.randint(0, 100, 20)
>>> m.data
array([92, 46, 99, 24, 75, 16, 49, 60, 87, 64, 91, 37, 30, 32, 25, 40, 99,
9, 3, 84])
>>> m >= 10
<1000x1000 sparse matrix of type '<type 'numpy.bool_'>'
with 18 stored elements in Compressed Sparse Row format>
>>> m = m.multiply(m >= 10)
>>> m
<1000x1000 sparse matrix of type '<type 'numpy.int32'>'
with 18 stored elements in Compressed Sparse Row format>
>>> m.data
array([92, 46, 99, 24, 75, 16, 49, 60, 87, 64, 91, 37, 30, 32, 25, 40, 99,
84])