OneHotEncoder throws unhelpful error messages when tranform called prior to fit
See original GitHub issueDescription
OneHotEncoder throws an AttributeError
instead of a NotFittedError
when tranform is called prior to fit
- if
transform
is called prior to being fit anAttributeError
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
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
preprocessing error trying to use OneHot Encoder Python
Call OneHotEncoder without preprocessing before the name. So just do ohe = OneHotEncoder() . The problem is in your import, what you have...
Read more >sklearn.preprocessing.OneHotEncoder
Specifies the way unknown categories are handled during transform . 'error' : Raise an error if an unknown category is present during transform....
Read more >How to Use the ColumnTransformer for Data Preparation
Each transformer is a three-element tuple that defines the name of the transformer, the transform to apply, and the column indices to apply...
Read more >5. Preprocessing Categorical Features and Column Transformer
Can we make a pipeline and fit it with our X_train that has this column now? ... categorical values in our feature table,...
Read more >How do I encode categorical features using scikit-learn?
In this video, you'll learn how to use OneHotEncoder and ColumnTransformer to encode your categorical features and prepare your feature ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m fine with not requiring fitting if categories is provided. (There’s an estimator tag for that!)
A bit more nuanced than expected. Sorry for the delay.