Qu

one_hot

Builtin index · Statistics & ML

result = one_hot(labels)

Input: labels · Output: result

What it is

One-hot encoding of a length-N label vector (numbers or a list of strings) with one column per distinct label in sorted order (numbers ascending, strings in byte order).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = one_hot(labels)
labels vector, number, or stringnot documented yet
1 —row i has its 1 in the column of labels[i]'s class
Returns a record {matrix, classes}, the shape the other multi-output builtins use: matrix is N x K of 0 and 1 (row i has its 1 in the column of labels[i]'s class) and classes the K labels in column order, a Vec for numbers and a list for strings. Errors: an empty vector, NaN, strings mixed with other values.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

colors = ("red", "green", "blue", "green", "red", "blue", "green", "red")
e = label_encode(colors)
print("classes: {e.classes}")
print("codes:   {e.codes}")
print("back:    {label_decode(e.codes, e.classes)}")
oh = one_hot(colors)
print("one_hot is {rows(oh.matrix)} x {cols(oh.matrix)}, columns = {oh.classes}")
output
classes: ("blue", "green", "red")
codes:   [2, 1, 0, 1, 2, 0, 1, 2]
back:    ("red", "green", "blue", "green", "red", "blue", "green", "red")
one_hot is 8 x 3, columns = ("blue", "green", "red")

from statistics-ml