Skip to content

MNT Ignore DepecationWarning for sparse_matrix removal in scipy - #34614

Merged
betatim merged 1 commit into
scikit-learn:mainfrom
StefanieSenger:filter_scipy_sparse_deprecation
Jul 31, 2026
Merged

betatim merged 1 commit into
scikit-learn:mainfrom
StefanieSenger:filter_scipy_sparse_deprecation

Conversation

@StefanieSenger

Copy link
Copy Markdown
Member

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:

  • Research and understanding

@betatim

betatim commented Jul 31, 2026

Copy link
Copy Markdown
Member

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).

Comment thread sklearn/utils/_testing.py
Comment on lines +1527 to +1529
# 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd write this:

Suggested change
# 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

@jeremiedbb

Copy link
Copy Markdown
Member

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).

We have a config key sparse_interface to return sparse matrices or arrays (defaults to matrices). It could be interesting to run the whole test suite with the config set to arrays to see if we're still producing spmatrices somewhere. If not, it means we're good w.r.t. the transition steps imo.

@betatim

betatim commented Jul 31, 2026

Copy link
Copy Markdown
Member

I asked AI to run the tests with sparse_interface="sparray" to answer the question. It looks like things mostly work. We have some test that pass a csr_matrix in, which scikit-learn then operates on and triggers these deprecation warnings. But sparse_interface doesn't influence this, at least from reading its docstring it doesn't sound like a flag the leads to all sparse things being converted, it only influences what scikit-learn returns.

It also found new failures if you do this. Apparently spmatrix converted the type of the indices to int32, but with sparray that doesn't happen. The different type for the indices leasd to problems in cython code later.

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}")

@lesteve

lesteve commented Aug 2, 2026

Copy link
Copy Markdown
Member

Follow-up PR to fix the remaining problems with sparse DeprecationWarning in #34632.

@StefanieSenger
StefanieSenger deleted the filter_scipy_sparse_deprecation branch September 1, 2026 11:53
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 8, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 9, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚠️ CI failed on Unit tests Linux x86-64 pylatest_pip_scipy_dev (last failure: Jul 31, 2026) ⚠️

4 participants