Enable config setting sparse_interface
to control sparray and spmatrix creation
#31177
+200
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR sets up a config parameter
sparse_interface
to indicate "sparray" or "spmatrix" outputs, as suggested in #26418.The first commit sets everything up and implements the system for a few modules. Please take a look and provide feedback for whether this is the way to proceed. The next commit(s) will implement these same style of changes throughout the library. If you would prefer they be in separate PRs let me know. (I'll keep Draft status until there is feedback and the full library is convered.)
More specifically, this PR does the following:
sparse_interface
to the config parameters. (I think this name is better than sparse_format because "format" means csr/coo/lil, etc in the sparse world.) The values it can hold are "sparray" or "spmatrix". Update config tests accordingly.utils._sparse.py
with (private) helper functions. The difference is how much checking is done. Tests added too._as_sparse(x_sparse)
, raises unless sparse input. converts to interface chosen by config._select_interface_if_sparse(x)
, allows dense input with no action, sparse input uses_as_sparse
._convert_from_spmatrix_to_sparray(x)
and_convert_from_sparray_to_spmatrix(x)
sklearn/feature_selection/text.py
and adapts tests.sklearn/linear_model/_coordinate_descent.py
no tests change needed.sklearn/manifold/_locally_linear.py
no tests change needed.@thomasjpfan can you see if this does what you had in mind? I tried to pick modules that cover returning sparse, setting estimators to hold sparse, and transforming to sparse, so you can see how this would work.
Let me know if you think
_as_sparse
should be a public function, and if my approach aligns with how you want it. The next steps for this PR are to repeat these type of changes throughout the library.