[SciPy-User] creating sparse indicator arrays

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Nov 29 10:14:03 EST 2011


Is there a simple or fast way to create a sparse indicator array, `a`
below, without going through the dense matrix first?

>>> from scipy import sparse
>>> g = np.array([0, 0, 1, 1])   #categories, integers,
>>> u = np.arange(2)    #unique's,  range(number_categories)

>>> g[:,None] == u
array([[ True, False],
       [ True, False],
       [False,  True],
       [False,  True]], dtype=bool)

this is the one I want:

>>> a = sparse.csc_matrix((g[:,None] == u))
>>> a
<4x2 sparse matrix of type '<type 'numpy.int8'>'
	with 4 stored elements in Compressed Sparse Column format>
>>> a.todense()
matrix([[1, 0],
        [1, 0],
        [0, 1],
        [0, 1]], dtype=int8)


Thanks,

Josef



More information about the SciPy-User mailing list