one_hot_encode

Turns categories into binary columns using one hot encoding.

Usage

This calculator allows the user to transform categorical columns into binary ones. For each category it will create a binary column with 1 if it was this category and 0 if not. It can take into account multiple categories at the same time if they use a specific separator.

Please note that: - Columns existing during preprocessing but missing during prediction are added and filled by False

- Columns existing during prediction but missing during preprocessing are removed

Some algorithm already handle encoding (for example lightgbm) so one_hot_encode is not necessary.

This calculator can be used with the following method:

one_hot_encode

Examples:

  • Transform a product category with 5 different categories into 5 binary columns.

  • This calculator is best used when there is a few different possible categories.


Main Parameters

The bold options represent the default values when the parameters are optional.

  • input_columns list of columns used as input of the calculators: The list of columns that will be used to fill the output column.

  • output_columns_prefix prefix of the columns added when the output columns cannot be listed

  • global (true, false) Should this calculator be performed before data splitting during training for cross-validation

  • steps [optionnal] (training, prediction, postprocessing) List of steps in a pipeline where columns from this calculator are added to the data. Note that when the training option is listed, the calculator is actually added during preprocessing.

  • store_in_model [optionnal] (true, false) Please indicate whether the "calculated" columns by the calculator should be stored in the model or not to avoid recalculating them during prediction. This is only relevant if the calculated columns are added to both training and prediction. Without this parameter, the values will not be stored in the model. The following parameters only make sense if this parameter is set to true.

  • stored_columns [required if store_in_model is true] List indicating the columns to be stored among the output_columns.

  • stored_keys [required if store_in_model is true] List indicating the columns to use for identifying the correct values to join on the data for prediction among the stored values (logically, they are to be chosen from the input_columns).


Specific Parameters

  • separator [optionnal] Char used as separator in the input column. When a separator is provided, the columns to one hot encode need to be of type str.


Examples

  1. A given dataset contains sales data (qty_sold) for several products. These products are characterized by a family (concat_famid) and a color (product_color), which is are categorical column containing several values. The user wants to transform those categorical columns into binary ones.

    calculated_cols:
      ohe_cat_feat:
        method: one_hot_encode
        input_columns:
        - concat_famid
        - product_color
        output_columns_prefix: oh

Input :

item_id
receipt_date
qty_sold
concat_famid
product_color

877988

2024-01-01

50

fam_1

blue

556764

2024-01-01

43

fam_1

red

321132

2024-01-01

18

fam_2

blue

121453

2024-01-01

9

fam_3

green

Result :

item_id
receipt_date
qty_sold
oh_fam_1
oh_fam_2
oh_fam_3
ohe_blue
ohe_red
ohe_green

877988

2024-01-01

50

1

0

0

1

0

0

556764

2024-01-01

43

1

0

0

0

1

0

321132

2024-01-01

18

0

1

0

1

0

0

121453

2024-01-01

9

0

0

1

0

0

1

  1. In this new example a product can have multiple colors which are separated by a comma in the input dataset. The one_hot_encode can handle this kind of use case with the following config :

calculated_cols:
  ohe_cat_feat:
    method: one_hot_encode
    input_columns:
    - concat_famid
    - product_color
    output_columns_prefix: oh
    params:
        separator: ,

Input :

item_id
receipt_date
qty_sold
product_color

877988

2024-01-01

50

blue,red

556764

2024-01-01

43

red,green

321132

2024-01-01

18

blue,red,green

121453

2024-01-01

9

green

Result :

item_id
receipt_date
qty_sold
ohe_blue
ohe_red
ohe_green

877988

2024-01-01

50

1

1

0

556764

2024-01-01

43

0

1

1

321132

2024-01-01

18

1

1

1

121453

2024-01-01

9

0

0

1

Last updated