/
githubmirror
/
scikit-learn
Обзор
Документация
Войти
/
githubmirror
/
scikit-learn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
sklearn/utils/_sparse.py
39 строк
1 KB
Dan Schult
Enable config setting `sparse_interface` to control sparray and spmatrix creation (#31177)
11 мар 2026, 09:04
Не верифицирован
11 мар 2026, 09:04
0169f83
Код
Авторство
О чём код?
"""Control sparse interface based on config""" # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import scipy as sp from sklearn._config import get_config def _align_api_if_sparse(X): """ Convert to sparse interface as set in config. Input can be dense or sparse. If sparse, convert to sparse_interface indicated by get_config. Otherwise, return X unchanged. """ if not sp.sparse.issparse(X): return X config_sparse_interface = get_config()["sparse_interface"] # there are only two sparse interfaces: sparray and spmatrix if config_sparse_interface == "sparray": if sp.sparse.isspmatrix(X): # Fundamental code to switch to sparray in any format return getattr(sp.sparse, X.format + "_array")(X) return X elif config_sparse_interface == "spmatrix": if sp.sparse.isspmatrix(X): return X # Fundamental code to switch to spmatrix in any format return getattr(sp.sparse, X.format + "_matrix")(X) else: raise ValueError( f'Config "sparse_interface" is {config_sparse_interface}. ' 'It should be either "sparray" or "spmatrix".' )