PolynomialTransformer silently overwrites an existing column when a generated monomial name already exists

Open Beginner friendly
#120 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active

Research direction

Start with src/transformers/other_transformers/polynomial_transformer.jl, especially the column-name construction and merge near lines 97-104. Review test/transformers/other_transformers/polynomial_transformer.jl and the two minimal examples in the issue. Done means existing columns are preserved or a clash raises an informative error, duplicate monomial names identify the conflicting monomials, and regression tests cover both cases.

Written by the indexing model from the issue text.

Description

PolynomialTransformer names each new column by joining the feature names with _ and then merges the result into the input table:

https://github.com/JuliaAI/MLJTransforms.jl/blob/d3e3db364f30bef397eddeae7f30a5e7967757fc/src/transformers/other_transformers/polynomial_transformer.jl#L97-L104

new_features = Tuple(Symbol(join(premon, "_")) for premon in premonomials)
...
return merge(Tables.columntable(X), table_addendum) |> materializer

Nothing checks whether a generated name is already a column of X, so merge just replaces that column with the product. The original data is gone and there's no warning or error. Column names like A_B aren't unusual, and the easiest way to hit this is to run the transformer on a table that already went through a PolynomialTransformer (or the deprecated InteractionTransformer, which has the same merge).

A related problem: since the names are just joined with _, two different monomials can get the same name, e.g. a * b_c and a_b * c both become a_b_c. That case does at least error, but the message comes from the NamedTuple constructor and doesn't say what went wrong.

Minimal example:

using MLJBase, MLJTransforms

# 1. an existing column happens to have a generated name
X = (A = [1.0, 2.0, 3.0], B = [4.0, 5.0, 6.0], A_B = [100.0, 200.0, 300.0])
Xt = transform(machine(PolynomialTransformer(degree=2, features=[:A, :B])), X)
@show keys(Xt)
@show X.A_B Xt.A_B

# 2. two different monomials get the same name
Z = (a = [1.0, 2.0], b_c = [3.0, 4.0], a_b = [5.0, 6.0], c = [7.0, 8.0])
try
    transform(machine(PolynomialTransformer(interactions_only=true)), Z)
catch e
    showerror(stdout, e); println()
end

Output:

keys(Xt) = (:A, :B, :A_B, :A_A, :B_B)
X.A_B = [100.0, 200.0, 300.0]
Xt.A_B = [4.0, 10.0, 18.0]
duplicate field name in NamedTuple: "a_b_c" is not unique

Expected: the user's A_B column should still be [100.0, 200.0, 300.0] in the output. Either throw an informative ArgumentError naming the clashing column(s), or give the new column a non-clashing name (with a warning). For case 2, an error that says which monomials ended up with the same name.

Actual: A_B is silently replaced by A .* B, and case 2 fails with a generic NamedTuple error.

The current tests in test/transformers/other_transformers/polynomial_transformer.jl only use inputs whose column names can't clash, so this isn't covered.

Versions: MLJTransforms v0.2.4 (dev at d3e3db3), MLJBase v1.15.5, Julia 1.13.4.

Dominant language
Julia
Stars
1
Forks
5
Avg merge
6h 44m
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaAI/MLJTransforms.jl

All issues in JuliaAI/MLJTransforms.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.