Return rows, items or columns of X using indices.
Warning
This utility is documented, but private. This means that backward compatibility might be broken without any deprecation cycle.
Data from which to sample rows, items or columns. list are only supported when axis=0.
axis=0, boolean and integer array-like, integer slice, and scalar integer are supported.axis=1:indices can be of int type for all X types and str only for dataframe. The selected subset will be 1D, unless X is a sparse matrix in which case it will be 2D.indices can be one of the following: list, array, slice. The type used in these containers can be one of the following: int, ‘bool’ and str. However, str is only supported when X is a dataframe. The selected subset will be 2D.The axis along which X will be subsampled. axis=0 will select rows while axis=1 will select columns.
Subset of X on axis 0 or 1.
CSR, CSC, and LIL sparse matrices are supported. COO sparse matrices are not supported.
>>> import numpy as np >>> from sklearn.utils import _safe_indexing >>> data = np.array([[1, 2], [3, 4], [5, 6]]) >>> _safe_indexing(data, 0, axis=0) # select the first row array([1, 2]) >>> _safe_indexing(data, 0, axis=1) # select the first column array([1, 3, 5])
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.utils._safe_indexing.html