One-hot encoding can convert the multiple values of a feature into multiple binary features. The binary features are mutually exclusive, and only one feature can be enabled at a time. After one-hot encoding, the output data consists of key-value pairs in the sparse format.

Overview

The One Hot Encoding component provides the training and prediction features.
  • Training feature:
    • Input nodes: The first (left) input node of this component is the input of training data, and the second (right) input node does not need to be set during the training.
    • Output nodes: This component has two output nodes. The left one is the encoded output table, and the right one is the output model table. The output model table is used to perform one-hot encoding for new data of the same type.
  • Prediction feature:

    The second (right) input node of the One Hot Encoding component is used to import a one-hot encoding model. An existing one-hot encoding model can be used to encode new data.

Configure the component

You can use one of the following methods to configure the One Hot Encoding component.

Method 1: Configure the component on the pipeline page

You can configure the parameters of the One Hot Encoding component on the pipeline page of Machine Learning Designer of Machine Learning Platform for AI (PAI). Machine Learning Designer is formerly known as Machine Learning Studio. The following table describes the parameters.
TabParameterDescription
Fields SettingBinarization ColumnRequired. The fields that require binarization.
Other Reserved FeaturesThe features that are reserved and exported in the key-value format. The selected fields are exported as features in the key-value format. The fields must be of the DOUBLE type. They are not subject to one-hot encoding and are encoded from 0.
Appended ColumnsOptional. The columns that are appended to the output table.
Parameters SettingLifecycleThe lifecycle of the output table. Default value: 7.
Output table typeThe type of the output table. Valid values: KV and Table. If the number of the features that require discretization is large, we recommend that you set this parameter to KV. If you set this parameter to Table, the output table can contain a maximum of 1,024 columns. If the number of the exported columns exceeds the value, an error is reported.
CoresThe number of cores.
Memory Size per NodeThe memory size of each core. Unit: MB.
Delete Encoding of Last EnumerationIf you select this check box, the linear independence of the encoded data is ensured.
Ignore Empty ElementsIf you select this check box, empty elements are not encoded.

Method 2: Use PAI commands

Configure the component parameters by using PAI commands. You can use the SQL Script component to call PAI commands. For more information, see SQL Script.
PAI -name one_hot
  -project algo_public
    -DinputTable=one_hot_test
    -DbinaryCols=f0,f1,f2
    -DmodelTable=one_hot_model
    -DoutputTable=one_hot_output
    -Dlifecycle=28;
ParameterRequiredDescriptionDefault value
inputTableYesThe name of the input table.No default value
inputTablePartitionsNoThe partitions that are selected from the input table for training.All partitions of the input table
binaryColsYesThe fields that require one-hot encoding. These fields must be enumeration features, and their data types are not limited. No default value
reserveColsNoThe selected fields that are exported as features in the key-value format. The fields must be of the DOUBLE type. They are not subject to one-hot encoding and are encoded from 0. Empty string
appendColsNoThe selected fields that are exported to the output table the same as they are in the input table. No default value
outputTableYesThe output table that is generated after one-hot encoding. The encoding result is stored in the key-value format. No default value
inputModelTableNoThe input model table for one-hot encoding.
Note The value of the inputModelTable or outputModelTable parameter must be a non-empty string.
Empty string
outputModelTableNoThe output model table for one-hot encoding.
Note The value of the inputModelTable or outputModelTable parameter must be a non-empty string.
Empty string
lifecycleNoThe lifecycle of the output table.7
dropLastYesSpecifies whether to delete the encoding result of the last enumerator. If this parameter is set to true, the linear independence of the encoded data is ensured. false
outputTableTypeYesThe type of the output table. Valid values: kv and table. If the number of the features that require discretization is large, we recommend that you set this parameter to kv. If you set this parameter to table, the output table can contain a maximum of 1,024 columns. If the number of the exported columns exceeds the value, an error is reported. kv
ignoreNullYesSpecifies whether to ignore empty elements in the data that requires encoding. If this parameter is set to true, empty elements are not encoded. false
coreNumNoThe number of cores. Determined by the system
memSizePerCoreNoThe memory size of each core. Unit: MB. Valid values: [2048,64 × 1024]. Determined by the system
Note
  • The value of the inputModelTable or outputModelTable parameter must be a non-empty string. If the value of the inputModelTable parameter is a non-empty string, the table that is specified by the parameter is a non-empty model table.
  • For the columns that require on-hot encoding, you can specify tens of millions of values for discretization.
  • If you use the trained model as the model for encoding next time, you cannot change the values of the dropLast, ignoreNull, and reserveCols parameters. This is because the output results generated based on the settings of these parameters are integrated into the model. If you want to modify the three parameters, you must train the model again.
  • We recommend that you export the output table in the key-value format. If you use the table format, you can export a maximum of 1,024 columns. If the number of the exported columns exceeds the value, an error is reported and the encoding fails.
  • By default, the output table in the key-value format that is generated after one-hot encoding is encoded from 0.
  • If you use the trained model to encode new data and cannot find the discrete magnitude of the data in the model mapping table, the discrete magnitude is ignored. This indicates that the discrete magnitude is not encoded. If you need to encode the discrete magnitude, you must train the model mapping table again.

Example

  1. Execute the following SQL statements to generate training data:
    PAI -project projectxlib4
      -name one_hot
      -DinputTable=one_hot_yh
      -DbinaryCols=f0,f2,f4
      -DoutputModelTable=one_hot_model_8
      -DoutputTable=one_hot_in_table_1_output_8
      -DdropLast=false
      -DappendCols=f0,f2,f4
      -DignoreNull=false
      -DoutputTableType=table
      -DreserveCols=f3
      -DcoreNum=4
      -DmemSizePerCore=2048;
  2. Import the data listed in the following table for a test.
    f0f1f2f3f4
    12prefix11970-09-15 12:50:220.1true
    12prefix31971-01-22 03:15:330.4false
    NULLprefix31970-01-01 08:00:000.2NULL
    3NULL1970-01-01 08:00:000.3false
    34NULL1970-09-15 12:50:220.4NULL
    3prefix11970-09-15 12:50:220.2true
    3prefix11970-09-15 12:50:220.3false
    3prefix31970-01-01 08:00:000.2true
    3prefix31971-01-22 03:15:330.1false
    NULLprefix31970-01-01 08:00:000.3false

    In the preceding input table, the f0 column is of the BIGINT type, the f1 column is of the STRING type, the f2 column is of the DATETIME type, the f3 column is of the DOUBLE type, and the f4 column is of the BOOLEAN type.

  3. Obtain the following model mapping table in the test result.
    col_namecol_valuemapping
    _reserve_f30
    f0121
    f032
    f0343
    f0null4
    f2222222222225
    f2333333333336
    f247
    f408
    f419
    f4null10
    The top row in the model mapping table is the reserve row, and the column name is fixed to reserve. This row stores reserve information. The remaining rows correspond to the mapping information of the encoding.
    • Encoded table in the table format
      f0f1f3f4_reserve__f3_0f0_12_1f0_3_2f0_34_3f0_null_4f2_22222222_5f2_33333333_6f2_4_7f4_0_8f4_1_9f4_null_10
      12prefix10.1true0.11.00.00.00.01.00.00.00.01.00.0
      12prefix30.4false0.41.00.00.00.00.01.00.01.00.00.0
      NULLprefix30.2NULL0.20.00.00.01.00.00.01.00.00.01.0
      3NULL0.3false0.30.01.00.00.00.00.01.01.00.00.0
      34NULL0.4NULL0.40.00.01.00.01.00.00.00.00.01.0
      3prefix10.2true0.20.01.00.00.01.00.00.00.01.00.0
      3prefix10.3false0.30.01.00.00.01.00.00.01.00.00.0
      3prefix30.2true0.20.01.00.00.00.00.01.00.01.00.0
      3prefix30.1false0.10.01.00.00.00.01.00.01.00.00.0
      NULLprefix30.3false0.30.00.00.01.00.00.01.01.00.00.0
    • Encoded table in the key-value format
      f0f1f3f4kv
      12prefix10.1true0:0.1,1:1,5:1,9:1
      12prefix30.4false0:0.4,1:1,6:1,8:1
      NULLprefix30.2NULL0:0.2,4:1,7:1,10:1
      3NULL0.3false0:0.3,2:1,7:1,8:1
      34NULL0.4NULL0:0.4,3:1,5:1,10:1
      3prefix10.2true0:0.2,2:1,5:1,9:1
      3prefix10.3false0:0.3,2:1,5:1,8:1
      3prefix30.2true0:0.2,2:1,7:1,9:1
      3prefix30.1false0:0.1,2:1,6:1,8:1
      NULLprefix30.3false0:0.3,4:1,7:1,8:1

Scalability test

Test data: The number of samples is 200 million, and the number of enumerators is 100,000. The test data is listed in the following table.
f0f1
94prefix3689
9664prefix5682
2062prefix5530
9075prefix9854
9836prefix1764
5140prefix1149
3455prefix7272
2508prefix7139
7993prefix1551
5602prefix4606
3132prefix5767
The test result is listed in the following table.
core numtrain timepredict timeAcceleration ratio
584s181s1/1
1060s93s1.4/1.95
2046s56s1.8/3.23
Usage notes when you perform the test in the console:
  • Use the One Hot Encoding component to encode data. The following figure shows the experiment process. Process
  • Use the trained model of the One Hot Encoding component to encode data. The following figure shows the experiment process. Experiment