get all unique value from a sparse matrix[python/scipy]
<p>I am trying to make a machine learning lib work together with scipy sparse matrix.</p>

<p>Below code is to detect if there are more than 1 class in <code>y</code> or not.Because it doesn't make sense if there is only 1 class when doing classification.</p>

<pre><code>import numpy as np
y = np.array([0,1,0,1,0,1])
uniques = set(y)  # get {0, 1}

if len(uniques) == 1:
    raise RuntimeError("Only one class detected, aborting...")
</code></pre>

<p>But <code>set(y)</code> not work if <code>y</code> is scipy sparse matrix.</p>

<p>How to efficiently get all unique value if <code>y</code> is scipy sparse matrix?</p>

<p>PS: I know <code>set(y.todense())</code> may work, but is cost too much memory</p>

<p><strong>UPDATE:</strong></p>

<pre><code>&gt;&gt;&gt; y = sp.csr_matrix(np.array([0,1,0,1,0,1]))
&gt;&gt;&gt; set(y.data)
{1}
&gt;&gt;&gt; y.data
array([1, 1, 1])
</code></pre>