Thanks to visit codestin.com
Credit goes to github.com

Skip to content

OneHotEncoder cuts predefined classes #25171

Closed
@EliasSchede

Description

@EliasSchede

Describe the bug

When having predefined categories for the OneHotEncoder the categories get cut off. This lead to an error when trying to transform samples with the categories present....

Steps/Code to Reproduce

from sklearn.preprocessing import OneHotEncoder
import numpy as np
from sklearn.preprocessing import OneHotEncoder

# I know all my categories in advance and there will be no other
# Using only one sample to call fit since I need to call at some point

list_of_bounds = [["as","mmas","eas","ras","acs"], ["1", "2"]]

one_sample = ["as", "1"]

o_h_enc = OneHotEncoder(categories=list_of_bounds)
o_h_enc.fit(np.array(one_sample).reshape(1, -1) )


print(o_h_enc.categories_)
# [array(['as', 'mm', 'ea', 'ra', 'ac'], dtype='<U2'), array(['1', '2'], dtype='<U2')]
# The first category should be dtype object and contain the full names i.e.
# [array(["as","mmas","eas","ras","acs"], dtype='object'), array(['1', '2'], dtype='<U2')]

o_h_enc.transform(np.array(["mmas", "1"]).reshape((1, -1)))

Expected Results

[array(["as","mmas","eas","ras","acs"], dtype='object'), array(['1', '2'], dtype='<U2')]

Actual Results

Versions

Python dependencies:
      sklearn: 1.2.0
          pip: 21.1.2
   setuptools: 57.0.0
        numpy: 1.23.4
        scipy: 1.9.3
       Cython: None
       pandas: None
   matplotlib: 3.6.2
       joblib: 1.2.0
threadpoolctl: 3.1.0

Built with OpenMP: True
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions