# All the ways to do one-hot encoding

**URL:** https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807
**Category:** General Usage
**Created:** [July 17, 2021, 10:42am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807 "2021-07-17T10:42:01Z")
**Posts on this page:** 11
**Page:** 2

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 12, 2021, 1:15am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/22 "2021-11-12T01:15:59Z")

</div>

Maybe is something like this what you want?

```julia
julia> struct OneHotCode{T} <: AbstractVector{T}
           i::T
           length::T
       end

julia> Base.getindex(o::OneHotCode,i) = i == o.i ? 1 : 0

julia> Base.size(o::OneHotCode) = (o.length,)

julia> Base.length(o::OneHotCode) = o.length

julia> function conv_seq(seq::String)
           seq_out = OneHotCode{Int8}[]
           for char in seq
               char == 'A' ? hot = 1 :
               char == 'T' ? hot = 2 :
               char == 'C' ? hot = 3 :
               char == 'G' ? hot = 4 :
               char == 'N' ? hot = 5 :
               error("Ilegal base")
               push!(seq_out,OneHotCode(Int8(hot),Int8(5)))
           end
           return seq_out
       end
conv_seq (generic function with 1 method)

julia> s = "TGTCCGGCTCACCCACATAACCATATATATATATATAGTATATAATAACCATTAACCAATATATATGGTTATGTGGCAACATCATTAATTTANAGAGATTTTACTATGGAATAATTGTGTGAATNCAGATTTTCAAGGCTCAAAAGAATATTTTTTGTTGACAGAAAAAAGAATAATCAATATACTGTAT"
"TGTCCGGCTCACCCACATAACCATATATATATATATAGTATATAATAACCATTAACCAATATATATGGTTATGTGGCAACATCATTAATTTANAGAGATTTTACTATGGAATAATTGTGTGAATNCAGATTTTCAAGGCTCAAAAGAATATTTTTTGTTGACAGAAAAAAGAATAATCAATATACTGTAT"

julia> conv_seq(s)
190-element Vector{OneHotCode{Int8}}:
 [0, 1, 0, 0, 0]
 [0, 0, 0, 1, 0]
 ⋮
 [1, 0, 0, 0, 0]
 [0, 1, 0, 0, 0]

```

A student of mine played with something like that to represent aminoacids in a ML code.

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [November 12, 2021, 4:30am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/23 "2021-11-12T04:30:10Z")

</div>

Looking at all this, maybe one-hot should be added to base?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 12, 2021, 8:24am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/24 "2021-11-12T08:24:26Z")

</div>

No, these things belong in packages.

---

<div class="post-metadata">

### Author: ![jamaas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamaas/32/30689_2.png) [@jamaas](https://discourse.julialang.org/u/jamaas)
#### Post date: [November 15, 2021, 2:56pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/25 "2021-11-15T14:56:11Z")

</div>

Thanks for this, yes it is exactly what I need. My only question is whether it is possible to get the same result using one of canned functions already built into a package such as Flux, MLBase, or MLJ ?

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [November 15, 2021, 3:39pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/26 "2021-11-15T15:39:52Z")

</div>

In MLJ, by specifying that some feature or some target of your data is either `Multiclass` or `OrderedFactor` (\*) you don’t have to bother with OHE… MLJ passes this information down to the model interface which does the most appropriate encoding there for the model you intend to use without requiring you to roll your own.

(\*) depending on your case, so for the ATGC case it would be `Multiclass{4}` and for some scoring it would be `OrderedFactor`. See also [ScientificTypes.jl](https://github.com/JuliaAI/ScientificTypes.jl).

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 15, 2021, 10:32pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/27 "2021-11-15T22:32:38Z")

</div>

> [@tlienart](#):
>
> you don’t have to bother with OHE

this is nice.

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [November 16, 2021, 4:56pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/28 "2021-11-16T16:56:59Z")

</div>

[One-Hot Encoding · Flux](https://fluxml.ai/Flux.jl/stable/data/onehot/#Flux.onehotbatch) ?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 4, 2021, 4:58pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/29 "2021-12-04T16:58:46Z")

</div>

> [@lostella](#):
>
> ```julia
> unique(x) .== reshape(x, (1, size(x)…))
> 
> ```

Thanks! I think I would only add a `sort`, so that the mapping is more intuitive.

```julia
sort(unique(x)) .== reshape(x, (1, size(x)…))

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [April 6, 2022, 4:41pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/30 "2022-04-06T16:41:15Z")

</div>

```julia
x=["a","b","a","c","c","b"]
julia> map(x->isequal(x...), Base.product(x, unique(x)))
6×3 Matrix{Bool}:
 1 0 0
 0 1 0
 1 0 0
 0 0 1
 0 0 1
 0 1 0

```

```julia
julia> Base.splat(isequal).(Base.product(x, unique(x)))
6×3 BitMatrix:
 1 0 0
 0 1 0
 1 0 0
 0 0 1
 0 0 1
 0 1 0

```

```julia
mapreduce(u->tuple.(u[1],findall(==(u[2]),x)),vcat, enumerate(unique(x)))
6-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (1, 3)
 (2, 2)
 (2, 6)
 (3, 4)
 (3, 5)
mapreduce(((i,u),)->tuple.(i,findall(==(u),x)).=>1,vcat, enumerate(unique(x)))
6-element Vector{Pair{Tuple{Int64, Int64}, Int64}}:
 (1, 1) => 1
 (1, 3) => 1
 (2, 2) => 1
 (2, 6) => 1
 (3, 4) => 1
 (3, 5) => 1

```

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [April 7, 2022, 1:36am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/31 "2022-04-07T01:36:46Z")

</div>

FYI: [https://github.com/JuliaRegistries/General/pull/58049](https://github.com/JuliaRegistries/General/pull/58049)

---

<div class="post-metadata">

### Author: ![camilogarciabotero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/camilogarciabotero/32/35000_2.png) [@camilogarciabotero](https://discourse.julialang.org/u/camilogarciabotero)
#### Post date: [October 20, 2024, 4:24pm UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/32 "2024-10-20T16:24:16Z")

</div>

I created a simple package to perform this on `BioSequence` types. It seems to be one of the most efficient to do it:

> **[GitHub - camilogarciabotero/BioVossEncoder.jl: A small Julia package to represent BioSequences as...](https://github.com/camilogarciabotero/BioVossEncoder.jl)**
>
> A small Julia package to represent BioSequences as a Voss matrix

PD: Sorry for necroposting…

[Previous page](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807.md?page=1)
