Check If Scipy Sparse Matrix Entry Exists
I initialize an empty sparse matrix using S = scipy.sparse.lil_matrix((n,n),dtype=int) As expected print S doesn't show anything, since nothing has been assigned. Yet if I test:
Solution 1:
You can check for stored values with
def get_items(s):
s_coo = s.tocoo()
return set(zip(s_coo.row, s_coo.col))
Demo:
>>> n = 100
>>> s = scipy.sparse.lil_matrix((n,n),dtype=int)
>>> s[10, 12] = 1
>>> (10, 12) in get_items(s)
True
Note that for other types of sparse matrices, 0 can be expicetely set:
>>> s = scipy.sparse.csr_matrix((n,n),dtype=int)
>>> s[12, 14] = 0
>>> (12, 14) in get_items(s)
True
Post a Comment for "Check If Scipy Sparse Matrix Entry Exists"