python - Fastest way from logic matrix to list of sets -
i need convert sparse logic matrix list of sets, each list[i] contains set of rows nonzero values column[i]. following code works, i'm wondering if there's faster way this. actual data i'm using approx 6000x6000 , more sparse example.
import numpy np = np.array([[1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 0], [1, 0, 1, 0, 1, 1], [1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0]]) rows,cols = a.shape c = np.nonzero(a) d = [set() j in range(cols)] in range(len(c[0])): d[c[1][i]].add(c[0][i]) print d
if represent sparse array csc_matrix
, can use indices
, indptr
attributes create sets.
for example,
in [93]: out[93]: array([[1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 0], [1, 0, 1, 0, 1, 1], [1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0]]) in [94]: scipy.sparse import csc_matrix in [95]: c = csc_matrix(a) in [96]: c.indptr out[96]: array([ 0, 5, 8, 12, 16, 20, 23], dtype=int32) in [97]: c.indices out[97]: array([0, 2, 3, 4, 5, 1, 3, 4, 1, 2, 6, 7, 1, 3, 4, 6, 1, 2, 6, 7, 0, 2, 3], dtype=int32) in [98]: d = [set(c.indices[c.indptr[i]:c.indptr[i+1]]) in range(c.shape[1])] in [99]: d out[99]: [{0, 2, 3, 4, 5}, {1, 3, 4}, {1, 2, 6, 7}, {1, 3, 4, 6}, {1, 2, 6, 7}, {0, 2, 3}]
for list of arrays instead of sets, don't call set()
:
in [100]: [c.indices[c.indptr[i]:c.indptr[i+1]] in range(len(c.indptr)-1)] out[100]: [array([0, 2, 3, 4, 5], dtype=int32), array([1, 3, 4], dtype=int32), array([1, 2, 6, 7], dtype=int32), array([1, 3, 4, 6], dtype=int32), array([1, 2, 6, 7], dtype=int32), array([0, 2, 3], dtype=int32)]
Comments
Post a Comment