You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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" .
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.
Activity
micedre commentedon Nov 24, 2026
@claude
claude commentedon Nov 24, 2026
Claude finished @micedre's task in 1m 0s —— View job •
claude/issue-112-20261124-1336• Create PR ➔Unknown categorical values and
OrdinalEncodersupportValueEncodercodeOrdinalEncoderfor categorical featurestests/test_value_encoder_unknown.pyWhat changed (
torchTextClassifiers/value_encoder/value_encoder.py):categorical_encodersnow acceptsOrdinalEncoderas well asDictEncoderandLabelEncoder. Each encoder is type-checked at construction.ValueEncoder(..., handle_unknown=False)parameter. When it isTrue, unseen or missing values (includingNaN) map to an extra index equal to the number of known categories of that feature.vocabulary_sizesthen includes that extra slot, so the embedding layer gets a row for it. When it isFalse(the default), unknown values still raise aValueError, and this now also applies toOrdinalEncodercolumns.Why not use
unknown_value/encoded_missing_valuedirectly: those parameters returnNaNor-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=Truegivesvocabulary_sizes == [3, 3]. Transforming[["a","z"],["new","y"],[nan,"x"]]returns[[0,2],[2,1],[2,0]]. Withhandle_unknown=Falsethe same input raises aValueError. I could not run the new pytest file because pytest isn't installed in this environment.Label encoders are unchanged.
OrdinalEncoderis only supported for categorical features, not labels.| Branch