_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.list are onlysupported whenaxis=0.

indicesbool, int, str, slice, array-like
  • Ifaxis=0, boolean and integer array-like, integer slice,and scalar integer are supported.

  • Ifaxis=1:
    • to select a single column,indices can be ofint type forallX types andstr only for dataframe. The selected subsetwill be 1D, unlessX is a sparse matrix in which case it willbe 2D.

    • to select multiples columns,indices can be one of thefollowing:list,array,slice. The type used inthese containers can be one of the following:int, ‘bool’ andstr. However,str is only supported whenX is a dataframe.The selected subset will be 2D.

axisint, default=0

The axis along whichX will be subsampled.axis=0 will selectrows whileaxis=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 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])
On this page

This Page