numpy - get all unique value from a sparse matrix[python/scipy] -


i trying make machine learning lib work scipy sparse matrix.

below code detect if there more 1 class in y or not.because doesn't make sense if there 1 class when doing classification.

import numpy np y = np.array([0,1,0,1,0,1]) uniques = set(y)  # {0, 1}  if len(uniques) == 1:     raise runtimeerror("only 1 class detected, aborting...") 

but set(y) not work if y scipy sparse matrix.

how efficiently unique value if y scipy sparse matrix?

ps: know set(y.todense()) may work, cost memory

update:

>>> y = sp.csr_matrix(np.array([0,1,0,1,0,1])) >>> set(y.data) {1} >>> y.data array([1, 1, 1]) 

sparse matrices store values in different ways, there .data attribute contains nonzero values.

set(y.data) 

might need. should work coo, csr, csc. others many need convert matrix format (e.g. y.tocoo).

if not work, give more details on matrix format , problems.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -