MCPcopy
hub / github.com/scikit-learn/scikit-learn / _align_api_if_sparse

Function _align_api_if_sparse

sklearn/utils/_sparse.py:11–39  ·  view source on GitHub ↗

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.

(X)

Source from the content-addressed store, hash-verified

9
10
11def _align_api_if_sparse(X):
12 """
13 Convert to sparse interface as set in config.
14
15 Input can be dense or sparse.
16 If sparse, convert to sparse_interface indicated by get_config.
17 Otherwise, return X unchanged.
18 """
19 if not sp.sparse.issparse(X):
20 return X
21
22 config_sparse_interface = get_config()["sparse_interface"]
23
24 # there are only two sparse interfaces: sparray and spmatrix
25 if config_sparse_interface == "sparray":
26 if sp.sparse.isspmatrix(X):
27 # Fundamental code to switch to sparray in any format
28 return getattr(sp.sparse, X.format + "_array")(X)
29 return X
30 elif config_sparse_interface == "spmatrix":
31 if sp.sparse.isspmatrix(X):
32 return X
33 # Fundamental code to switch to spmatrix in any format
34 return getattr(sp.sparse, X.format + "_matrix")(X)
35 else:
36 raise ValueError(
37 f'Config "sparse_interface" is {config_sparse_interface}. '
38 'It should be either "sparray" or "spmatrix".'
39 )

Callers 15

_transform_sparseMethod · 0.90
_get_predictionsMethod · 0.90
_sparse_random_matrixFunction · 0.90
inverse_transformMethod · 0.90
transformMethod · 0.90
transformMethod · 0.90
kneighbors_graphMethod · 0.90
_get_maskFunction · 0.90
_random_choice_cscFunction · 0.90
inverse_transformMethod · 0.90
_get_feature_importancesFunction · 0.90

Calls 1

get_configFunction · 0.90

Tested by 11

fitMethod · 0.72
partial_fitMethod · 0.72
decision_functionMethod · 0.72
predict_probaMethod · 0.72
fitMethod · 0.72
partial_fitMethod · 0.72
decision_functionMethod · 0.72
fitMethod · 0.72
partial_fitMethod · 0.72
decision_functionMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…