![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
Nov. 29, 2011
7:14 a.m.
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