Skip to content

OneHotEncoder throws unhelpful error messages when tranform called prior to fit #12395

@dillongardner

Description

@dillongardner

Description

OneHotEncoder throws an AttributeError instead of a NotFittedError when tranform is called prior to fit

  • if transform is called prior to being fit an AttributeError is thrown
  • if categories includes arrays of of unicode type

Steps/Code to Reproduce

import numpy as np
from sklearn.preprocessing import OneHotEncoder

categories = sorted(['Dillon', 'Joel', 'Earl', 'Liz'])
X = np.array(['Dillon', 'Dillon', 'Joel', 'Liz', 'Liz', 'Earl']).reshape(-1, 1)

ohe = OneHotEncoder(categories=[sorted(categories)])
ohe.transform(X)
# Throws AttributeError: 'OneHotEncoder' object has no attribute '_legacy_mode'

Expected Results

NotFittedError: This OneHotEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

Actual Results

Throws AttributeError: 'OneHotEncoder' object has no attribute '_legacy_mode'

Versions

System
------
    python: 3.6.3 (default, Oct  4 2017, 06:09:38)  [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]
executable: /Users/dillon/Envs/mewtwo/bin/python3.6
   machine: Darwin-18.0.0-x86_64-i386-64bit

BLAS
----
    macros: NO_ATLAS_INFO=3, HAVE_CBLAS=None
  lib_dirs:
cblas_libs: cblas

Python deps
-----------
       pip: 18.1
setuptools: 39.0.1
   sklearn: 0.20.0
     numpy: 1.14.2
     scipy: 1.0.1
    Cython: None
    pandas: 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions