|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + StdFeats() |
| 7 | +
|
| 8 | +Standardizes the columns of the table based on scientific types: |
| 9 | +
|
| 10 | +* `Continuous`: `ZScore` |
| 11 | +* `Categorical`: `Identity` |
| 12 | +* `Unknown`: `Identity` |
| 13 | +""" |
| 14 | +struct StdFeats <: StatelessFeatureTransform end |
| 15 | + |
| 16 | +isrevertible(::Type{StdFeats}) = true |
| 17 | + |
| 18 | +_stdfun(x) = _stdfun(elscitype(x), x) |
| 19 | +_stdfun(::Type, x) = identity, identity |
| 20 | +function _stdfun(::Type{Continuous}, x) |
| 21 | + μ = mean(x) |
| 22 | + σ = std(x, mean=μ) |
| 23 | + stdfun = x -> zscore(x, μ, σ) |
| 24 | + revfun = y -> revzscore(y, μ, σ) |
| 25 | + stdfun, revfun |
| 26 | +end |
| 27 | + |
| 28 | +function applyfeat(::StdFeats, feat, prep) |
| 29 | + cols = Tables.columns(feat) |
| 30 | + names = Tables.columnnames(cols) |
| 31 | + |
| 32 | + tuples = map(names) do name |
| 33 | + x = Tables.getcolumn(cols, name) |
| 34 | + stdfun, revfun = _stdfun(x) |
| 35 | + stdfun(x), revfun |
| 36 | + end |
| 37 | + |
| 38 | + columns = first.(tuples) |
| 39 | + fcache = last.(tuples) |
| 40 | + |
| 41 | + 𝒯 = (; zip(names, columns)...) |
| 42 | + newfeat = 𝒯 |> Tables.materializer(feat) |
| 43 | + |
| 44 | + newfeat, fcache |
| 45 | +end |
| 46 | + |
| 47 | +function revertfeat(::StdFeats, newfeat, fcache) |
| 48 | + cols = Tables.columns(newfeat) |
| 49 | + names = Tables.columnnames(cols) |
| 50 | + |
| 51 | + columns = map(names, fcache) do name, revfun |
| 52 | + y = Tables.getcolumn(cols, name) |
| 53 | + revfun(y) |
| 54 | + end |
| 55 | + |
| 56 | + 𝒯 = (; zip(names, columns)...) |
| 57 | + 𝒯 |> Tables.materializer(newfeat) |
| 58 | +end |
0 commit comments