The ML.ONE_HOT_ENCODER function

This document describes the ML.ONE_HOT_ENCODER function, which lets you encode a string expression using a one-hot or dummy encoding scheme.

The encoding vocabulary is sorted alphabetically. NULL values and categories that aren't in the vocabulary are encoded with an index value of 0. If you use dummy encoding, the dropped category is encoded with a value of 0.

When used in the TRANSFORM clause, the vocabulary and dropped category values calculated during training, along with the top k and frequency threshold values that you specified, are automatically used in prediction.

Syntax

ML.ONE_HOT_ENCODER(string_expression [, drop] [, top_k] [, frequency_threshold]) OVER()

Arguments

ML.ONE_HOT_ENCODER takes the following arguments:

Output

ML.ONE_HOT_ENCODER returns an array of struct values, in the form ARRAY<STRUCT<INT64, FLOAT64>>. The first element in the struct provides the index of the encoded string expression, and the second element provides the value of the encoded string expression.

Example

The following example performs dummy encoding on a set of string expressions. It limits the encoding vocabulary to the ten categories that occur the most frequently in the data and that also occur zero or more times.

SELECT f, ML.ONE_HOT_ENCODER(f, 'most_frequent', 10, 0) OVER () AS output
FROM UNNEST([NULL, 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd']) AS f
ORDER BY f;

The output looks similar to the following:

+------+-----------------------------+
|  f   | output.index | output.value |
+------+--------------+--------------+
| NULL |  0           |  1.0         |
| a    |  1           |  1.0         |
| b    |  2           |  1.0         |
| b    |  2           |  1.0         |
| c    |  3           |  0.0         |
| c    |  3           |  0.0         |
| c    |  3           |  0.0         |
| d    |  4           |  1.0         |
| d    |  4           |  1.0         |
+------+-----------------------------+

What's next