Skip to content

Interaction transformer #476

@olivierlabayle

Description

@olivierlabayle

Hi,

For my project I need to be able to generate interactions of variables up to a specific order. I couldn't find an implementation yet in the Julia ecosystem so have a custom implementation. I was wondering if that would be of general interest and if this package is the correct place to share it?

For now it looks like the following:

using MLJBase
using Combinatorics
using Tables

mutable struct InteractionTransformer <: Static 
    order::Int
    colnames::Union{Nothing, Vector{Symbol}}
end
InteractionTransformer(;order=2, colnames=nothing) = InteractionTransformer(order, colnames)

interactions(columns, order) = 
    collect(Iterators.flatten(combinations(columns, i) for i in 2:order))

actualcolumns(colnames::Nothing, table) = Tables.columnnames(table)

function actualcolumns(colnames::Vector{Symbol}, table)
    diff = setdiff(model.colnames, Tables.columnnames(table))
    diff != [] && throw(ArgumentError(string("Columns ", join([x for x in diff], ", "), " are not in the dataset")))
    return colnames
end

function interaction(columns, variables...)
    .*((Tables.getcolumn(columns, var) for var in variables)...)
end

function MLJBase.transform(model::InteractionTransformer, _, X)
    colnames = actualcolumns(model.colnames, X)
    interactions_ = interactions(colnames, model.order)
    interaction_colnames = Tuple(Symbol(join(inter, "_")) for inter in interactions_)
    columns = Tables.Columns(X)
    interaction_table = NamedTuple{interaction_colnames}([interaction(columns, inter...) for inter in interactions_])
    return merge(Tables.columntable(X), interaction_table)
end

X = (A = [1, 2, 3], B = [4, 5, 6], C = [7, 8, 9])

model = InteractionTransformer(order = 3)

MLJBase.transform(model, nothing, X)

If this is welcome I am happy to start a PR and take specific guidance if any.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions