question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OneHotEncoder throws unhelpful error messages when tranform called prior to fit

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jnothmancommented, Nov 7, 2018

I’m fine with not requiring fitting if categories is provided. (There’s an estimator tag for that!)

1reaction
dillongardnercommented, Oct 23, 2018

A bit more nuanced than expected. Sorry for the delay.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found