logo

How to Perform One-Hot Encoding in Julia Flux 📂Machine Learning

How to Perform One-Hot Encoding in Julia Flux

Overview

One-hot encoding is the process of mapping data to standard basis vectors based on its classification. Flux provides functions for this.

Code1

onehot()

  • onehot(x, labels, [default])

Returns x .== labels. However, it does not return the exact same result, but returns a type called OneHotVector. For encoding multiple data points, use onehotbatch() below.

julia> 3 .== [1,3,4]
3-element BitVector:
 0
 1
 0

julia> Flux.onehot(3, [1,3,4])
3-element OneHotVector(::UInt32) with eltype Bool:
 ⋅
 1
 ⋅

julia> Flux.onehot(3, 1:6)
6-element OneHotVector(::UInt32) with eltype Bool:
 ⋅
 ⋅
 1
 ⋅
 ⋅
 ⋅

julia> Flux.onehot(:c, [:a,:b,:c])
3-element OneHotVector(::UInt32) with eltype Bool:
 ⋅
 ⋅
 1

If a default value is specified, elements not in the labels are labeled with the default value.

julia> Flux.onehot(5, [1,2,3], 3)
3-element OneHotVector(::UInt32) with eltype Bool:
 ⋅
 ⋅
 1

onehotbatch()

  • onehotbatch(xs, labels, [default])

Used for encoding multiple data points at once. The usage is the same as onehot().

julia> Flux.onehotbatch([1,2,5], [1,2,3], 3)
3×3 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
 1  ⋅  ⋅
 ⋅  1  ⋅
 ⋅  ⋅  1

julia> Flux.onehotbatch(['a', 'b', 'c', 'h'], 'a':'c', 'c')
3×4 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
 1  ⋅  ⋅  ⋅
 ⋅  1  ⋅  ⋅
 ⋅  ⋅  1  1

onecold()

  • onecold(y::AbstractArray, labels = 1:size(y,1))

This is the reverse operation of onehot(), onehotbatch(). It decodes the one-hot encoding. It can be used even if the input is not a OneHotVector.

julia> x1 = Flux.onehot(2, 1:3)
3-element OneHotVector(::UInt32) with eltype Bool:
 ⋅
 1
 ⋅

julia> Flux.onecold(x1)
2

julia> x2 = Flux.onehot('a', ['a', 'b', 'c'])
3-element OneHotVector(::UInt32) with eltype Bool:
 1
 ⋅
 ⋅

julia> Flux.onecold(x2, 'a':'c')
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> Flux.onecold([0.1, 0.2, 0.5], [:a, :b, :c])
:c

3×3 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
 1  ⋅  ⋅
 ⋅  1  ⋅
 ⋅  ⋅  1

julia> x3 = Flux.onehotbatch([1,2,5], [1,2,3], 3)
3×3 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
 1  ⋅  ⋅
 ⋅  1  ⋅
 ⋅  ⋅  1

julia> Flux.onecold(x3)
3-element Vector{Int64}:
 1
 2
 3

Environment

  • OS: Windows10
  • Version: Julia 1.7.1, Flux 0.12.8