Repository navigation
MNT Ignore DepecationWarning for sparse_matrix removal in scipy - #34614
Conversation
|
Looks good to me. From looking at a few of the cases where the warning is raised it seems like the suggested fix from the warning is to use a different function that will return a different sparse matrix class. We should probably create an issue to discuss how to perform this transition and coordinate when we can do it. There is https://docs.scipy.org/doc/scipy/reference/sparse.migration_to_sparray.html which seems useful (I've only scanned it briefly). |
| # TODO: remove once the version where scipy has removed all the sparse matrix | ||
| # classes is our min version; note that scipy did not yet announce when this | ||
| # will happen, only that it won't be earlier than v2.2. |
There was a problem hiding this comment.
I'd write this:
| # TODO: remove once the version where scipy has removed all the sparse matrix | |
| # classes is our min version; note that scipy did not yet announce when this | |
| # will happen, only that it won't be earlier than v2.2. | |
| # TODO: remove once we have transitioned all of scikit-learn to use the new | |
| # sparse array from scipy instead of the sparse matrix. | |
| # See https://docs.scipy.org/doc/scipy/reference/sparse.migration_to_sparray.html |
We have a config key |
|
I asked AI to run the tests with It also found new failures if you do this. Apparently Here is a way to reproduce the failure: """Reproducer: sparse_interface="sparray" yields int64 index dtypes.
Run with the interface to use as the only argument:
python repro_int64_indices.py spmatrix
python repro_int64_indices.py sparray
"""
import io
import sys
import warnings
import numpy as np
import scipy
import scipy.sparse as sp
import sklearn
from sklearn.datasets import dump_svmlight_file, load_svmlight_file
from sklearn.random_projection import SparseRandomProjection
from sklearn.tree import DecisionTreeRegressor
warnings.filterwarnings("ignore", message=r"(?s).*All sparse matrix classes")
mode = sys.argv[1]
sklearn.set_config(sparse_interface=mode)
print(f"### scipy {scipy.__version__}, sparse_interface={mode!r}")
# 1. The scipy behaviour the rest depends on: when built from a
# (data, indices, indptr) tuple, csr_matrix downcasts the index arrays to
# int32 while csr_array keeps whatever it was given.
data, indices, indptr = np.ones(3), np.arange(3), np.arange(4)
print("\n[1] built from int64 (data, indices, indptr) tuple")
print(" csr_matrix ->", sp.csr_matrix((data, indices, indptr)).indices.dtype)
print(" csr_array ->", sp.csr_array((data, indices, indptr)).indices.dtype)
# 2. SparseRandomProjection.components_ is built from such a tuple.
print("\n[2] SparseRandomProjection.components_ -> DecisionTreeRegressor.fit")
srp = SparseRandomProjection(n_components=30, random_state=0)
X = srp.fit(np.zeros((10, 20))).components_
print(f" {type(X).__name__}: indices={X.indices.dtype}, indptr={X.indptr.dtype}")
try:
DecisionTreeRegressor().fit(X, np.zeros(X.shape[0]))
print(" fit ok")
except ValueError as e:
print(f" fit FAILED: {e}")
# 3. load_svmlight_file builds the matrix from an int64 indptr coming out of
# the Cython reader, and dump_svmlight_file needs int32 indices.
print("\n[3] load_svmlight_file -> astype -> dump_svmlight_file")
buf = io.BytesIO()
dump_svmlight_file(np.eye(3), np.arange(3), buf)
buf.seek(0)
X, y = load_svmlight_file(buf)
print(f" {type(X).__name__}: indices={X.indices.dtype}, indptr={X.indptr.dtype}")
X = X.astype(np.float64)
print(f" after astype: indices={X.indices.dtype}, indptr={X.indptr.dtype}")
try:
dump_svmlight_file(X, y, io.BytesIO())
print(" dump ok")
except ValueError as e:
print(f" dump FAILED: {e}") |
|
Follow-up PR to fix the remaining problems with sparse DeprecationWarning in #34632. |
Reference Issues/PRs
closes #34608
What does this implement/fix? Explain your changes.
This ignores the scipy DepecationWarning on deprecation all sparse_matrix classes that pops up on the nightly build.
AI usage disclosure
I used AI assistance for: