watex.utils.get_target#
- watex.utils.get_target(ar, /, tname, drop_target=True, columns=None, as_frame=False)[source]#
Extract target from multidimensional array or dataframe.
- Parameters
ar (arraylike2d or pd.DataFrame) – Array that supposed to contain the target value.
tname (int/str, list of int/str) – index or the name of the target; if
intis passed it should range ranged less than the columns number of the array i.e. a shape[1] in the case of np.ndarray. If the list of indexes or names are given, the return target should be in two dimensional array.drop_target (bool, default=True) – Remove the target array in the 2D array or dataframe in the case the target exists and returns a data exluding the target array.
columns (list, default=False.) – composes the dataframe when the array is given rather than a dataframe. The list of column names must match the number of columns in the two dimensional array, otherwise an error occurs.
as_frame (bool, default=False,) – returns dataframe/series or the target rather than array when the array is supplied. This seems useful when column names are supplied.
- Returns
t, ar – Return the targets and the array/dataframe of the target.
- Return type
array-like/pd.Series , array-like/pd.DataFrame
Examples
>>>> import numpy as np >>> import pandas as pd >>> from watex.utils.mtutils import get_target >>> ar = np.random.randn ( 3, 3 ) >>> df0 = pd.DataFrame ( ar, columns = [‘x1’, ‘x2’, ‘tname’]) >>> df= df0.copy() >>> get_target (df, ‘tname’, drop_target= False ) ( tname
0 -0.542861 1 0.781198,
x1 x2 tname
0 -1.424061 -0.493320 -0.542861 1 0.416050 -1.156182 0.781198)
>>> get_target (df, [ 'tname', 'x1']) # drop is True by default ( tname x1 0 -0.542861 -1.424061 1 0.781198 0.416050, x2 0 -0.493320 1 -1.156182) >>> df = df0.copy() >>> # when array is passed >>> get_target (df.values , '2', drop_target= False ) (array([[-0.54286148], [ 0.7811981 ]]), array([[-1.42406091, -0.49331988, -0.54286148], [ 0.41605005, -1.15618243, 0.7811981 ]])) >>> get_target (df.values , 'tname') # raise error ValueError: 'tname' ['tname'] is not valid...