_safe_indexing (original) (raw)

sklearn.utils._safe_indexing(X, indices, *, axis=0)[source]#

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.

Parameters:

Xarray-like, sparse-matrix, list, pandas.DataFrame, pandas.Series

Data from which to sample rows, items or columns. list are only supported when axis=0.

indicesbool, int, str, slice, array-like

axisint, default=0

The axis along which X will be subsampled. axis=0 will select rows while axis=1 will select columns.

Returns:

subset

Subset of X on axis 0 or 1.

Notes

CSR, CSC, and LIL sparse matrices are supported. COO sparse matrices are not supported.

Examples

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