Skip to content

added handle_unknown feature to OrdinalEncoder class - #15108

Closed
pedro-munoz wants to merge 2 commits into
scikit-learn:masterfrom
pedro-munoz:ordinal_encoder_new_values
Closed

pedro-munoz wants to merge 2 commits into
scikit-learn:masterfrom
pedro-munoz:ordinal_encoder_new_values

Conversation

@pedro-munoz

Copy link
Copy Markdown

Reference Issues/PRs

What does this implement/fix? Explain your changes.

This change adds the functionality of handling new values in the OrdinalEncoder class.
It implements the handle_unknown variable on construction as the OneHotEncoder does.
Returns -1 when a new class is seen by the encoder.

Any other comments?

import pandas as pd

df = pd.DataFrame(['ola', 'k', 'ase'])
df_test = pd.DataFrame(['ase', 'k', 'ase'])
df_test_unknown = pd.DataFrame(['tu', 'k', 'ases'])

from sklearn.preprocessing import OrdinalEncoder

encoder = OrdinalEncoder(handle_unknown='ignore')
encoder.fit(df)
print(f'Good shape: {encoder.transform(df_test)}')
print(f'Bad shape: {encoder.transform(df_test_unknown)}')

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant