Context
SciPy is now favoring sparse arrays (i.e. scipy.sparse.sparray and its subclasses) over sparse matrices (i.e. scipy.sparse.spmatrix and its subclasses) to enlarge the scope of matrices to $n$-dimensional data-structures since SciPy 1.8 (see scipy/scipy#14822).
Sparse matrices now subclass their associated sparse arrays (see scipy/scipy#18440).
scikit-learn has been supporting sparse matrices but now also needs to support SciPy sparse arrays.
Proposed solutions
Ordered by preference:
- Use
scipy.sparse.issparse and the format attribute everywhere and not use isinstance at all
- Use
isinstance on private compatibility class defined to be scipy.sparse.spmatrix or scipy.sparse.sparray conditionally on SciPy's version (i.e. use scipy.sparse.sparray if available)
- Rely on duck-typing or the class name to check for sparse arrays
cc @ivirshup
Context
SciPy is now favoring sparse arrays (i.e.$n$ -dimensional data-structures since SciPy 1.8 (see scipy/scipy#14822).
scipy.sparse.sparrayand its subclasses) over sparse matrices (i.e.scipy.sparse.spmatrixand its subclasses) to enlarge the scope of matrices toSparse matrices now subclass their associated sparse arrays (see scipy/scipy#18440).
scikit-learn has been supporting sparse matrices but now also needs to support SciPy sparse arrays.
Proposed solutions
Ordered by preference:
scipy.sparse.issparseand theformatattribute everywhere and not useisinstanceat allisinstanceon private compatibility class defined to bescipy.sparse.spmatrixorscipy.sparse.sparrayconditionally on SciPy's version (i.e. usescipy.sparse.sparrayif available)cc @ivirshup