Skip to content

Allow unknow value for categorial variabels #112

Done

Description

@ycoltier

Hi :)

TTC uses Label encoder for categorical variables. Is it possible to use OrdinalEncoder from scikit-learn ?
This function can have those parameters : "unknown_value=None, encoded_missing_value=nan" .

Many thanks for your work still amazing !

Best regards

Activity

  1. micedre commented on Nov 24, 2026

    @micedre
    Collaborator
  2. claude commented on Nov 24, 2026

    @claude

    Claude finished @micedre's task in 1m 0s —— View job • claude/issue-112-20261124-1336 • Create PR ➔


    Unknown categorical values and OrdinalEncoder support

    • Read the issue and the ValueEncoder code
    • Accept sklearn OrdinalEncoder for categorical features
    • Add an opt-in way to handle unknown and missing values
    • Add tests in tests/test_value_encoder_unknown.py

    What changed (torchTextClassifiers/value_encoder/value_encoder.py):

    • categorical_encoders now accepts OrdinalEncoder as well as DictEncoder and LabelEncoder. Each encoder is type-checked at construction.
    • New ValueEncoder(..., handle_unknown=False) parameter. When it is True, unseen or missing values (including NaN) map to an extra index equal to the number of known categories of that feature. vocabulary_sizes then includes that extra slot, so the embedding layer gets a row for it. When it is False (the default), unknown values still raise a ValueError, and this now also applies to OrdinalEncoder columns.

    Why not use unknown_value / encoded_missing_value directly: those parameters return NaN or -1, which can't be used as an embedding index. Mapping to a dedicated extra index works with all three encoder types.

    Testing: I checked the behaviour with a quick script. For features [a, b] and [x, y], handle_unknown=True gives vocabulary_sizes == [3, 3]. Transforming [["a","z"],["new","y"],[nan,"x"]] returns [[0,2],[2,1],[2,0]]. With handle_unknown=False the same input raises a ValueError. I could not run the new pytest file because pytest isn't installed in this environment.

    Label encoders are unchanged. OrdinalEncoder is only supported for categorical features, not labels.

    | Branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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