3

scipy.sparse.issparse is used in this post.

Is it possible to specify your own distance function using scikit-learn K-Means Clustering?

However, I have no idea how it works. I already find the document, which is empty. http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.issparse.html

from scipy.sparse import issparse

issparse([0, 0, 0])
>> False

issparse([[1, 0, 0], [0, 0, 0]])
>> False

It always return False. How can I make it return True?

Community
  • 1
  • 1
akiniwa
  • 617
  • 8
  • 18

3 Answers3

6

issparse has nothing to do with how many elements the input has. Rather, scipy.sparse defines a number of types optimized for representing sparse matrices, and issparse determines whether the input is a sparse matrix object.

In [1]: import scipy.sparse

In [2]: scipy.sparse.issparse(scipy.sparse.bsr_matrix([[1, 0], [0, 1]]))                          
Out[2]: True
user2357112
  • 260,549
  • 28
  • 431
  • 505
3

This page defines the spmatrix class

class spmatrix(object):
    """ This class provides a base class for all sparse matrices.

At the bottom of the page, it defines issparse as

def isspmatrix(x):
    return isinstance(x, spmatrix)

issparse = isspmatrix

So, if I'm reading it correctly, issparse is true when x is an instance of spmatrix.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

The function only tests if the variable is a subclass of spmatrix. e.g. il_matrix or dok_matrix

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304