watex.utils.cattarget#
- watex.utils.cattarget(arr, /, func=None, labels=None, rename_labels=None, coerce=False, order='strict')[source]#
Categorize array to hold the given identifier labels.
Classifier numerical values according to the given label values. Labels are a list of integers where each integer is a group of unique identifier of a sample in the dataset.
- Parameters:
arr (array-like |pandas.Series) – array or series containing numerical values. If a non-numerical values is given , an errors will raises.
func (Callable,) – Function to categorize the target y.
labels (int, list of int,) – if an integer value is given, it should be considered as the number of category to split ‘y’. For instance
label=3applied on the first ten number, the labels values should be[0, 1, 2]. If labels are given as a list, items must be self-contain in the target ‘y’.rename_labels (list of str;) – list of string or values to replace the label integer identifier.
coerce (bool, default =False,) – force the new label names passed to rename_labels to appear in the target including or not some integer identifier class label. If coerce is
True, the target array holds the dtype of new_array.
- Returns:
arr – The category array with unique identifer labels
- Return type:
Arraylike |pandas.Series
Examples
>>> from watex.utils.mlutils import cattarget >>> def binfunc(v): if v < 3 : return 0 else : return 1 >>> arr = np.arange (10 ) >>> arr ... array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> target = cattarget(arr, func =binfunc) ... array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1], dtype=int64) >>> cattarget(arr, labels =3 ) ... array([0, 0, 0, 1, 1, 1, 2, 2, 2, 2]) >>> array([2, 2, 2, 2, 1, 1, 1, 0, 0, 0]) >>> cattarget(arr, labels =3 , order =None ) ... array([0, 0, 0, 0, 1, 1, 1, 2, 2, 2]) >>> cattarget(arr[::-1], labels =3 , order =None ) ... array([0, 0, 0, 1, 1, 1, 2, 2, 2, 2]) # reverse does not change >>> cattarget(arr, labels =[0 , 2, 4] ) ... array([0, 0, 0, 2, 2, 4, 4, 4, 4, 4])