_safe_indexing#
- sklearn.utils._safe_indexing(X,indices,*,axis=0)[source]#
Return rows, items or columns of X using indices.
Warning
This utility is documented, butprivate. This means thatbackward compatibility might be broken without any deprecationcycle.
- Parameters:
- Xarray-like, sparse-matrix, list, pandas.DataFrame, pandas.Series
Data from which to sample rows, items or columns.
listare onlysupported whenaxis=0.- indicesbool, int, str, slice, array-like
If
axis=0, boolean and integer array-like, integer slice,and scalar integer are supported.- If
axis=1: to select a single column,
indicescan be ofinttype forallXtypes andstronly for dataframe. The selected subsetwill be 1D, unlessXis a sparse matrix in which case it willbe 2D.to select multiples columns,
indicescan be one of thefollowing:list,array,slice. The type used inthese containers can be one of the following:int, ‘bool’ andstr. However,stris only supported whenXis a dataframe.The selected subset will be 2D.
- If
- axisint, default=0
The axis along which
Xwill be subsampled.axis=0will selectrows whileaxis=1will select columns.
- Returns:
- subset
Subset of X on axis 0 or 1.
Notes
CSR, CSC, and LIL sparse matrices are supported. COO sparse matrices arenot supported.
Examples
>>>importnumpyasnp>>>fromsklearn.utilsimport_safe_indexing>>>data=np.array([[1,2],[3,4],[5,6]])>>>_safe_indexing(data,0,axis=0)# select the first rowarray([1, 2])>>>_safe_indexing(data,0,axis=1)# select the first columnarray([1, 3, 5])
