Skip to content

Better error message when passing un-sortable data to the Encoders #12621

Closed

Description

@readyready15728

Description

scikit-learn's handling of errors where an unexpected / unusable value appears in input leaves something to be desired. Errors are cryptic and confusing.

Steps/Code to Reproduce

Example:

import pandas as pd
from sklearn.preprocessing import LabelEncoder

# Simulate missing value in data
feature_with_missing_value = pd.Series([1, 2, 3, '?', 42, 69])
LabelEncoder().fit_transform(feature_with_missing_value.values)

Something similar also happened when I absentmindedly concatenated two CSV files without ensuring only one header appeared at the top of the file and attempted to use LabelEncoder with the second header sandwiched in the middle.

I have also encountered this issue I believe with pipelines and missing values before but this was a while ago and I eventually figured out what was happening so unfortunately I can't replicate that error.

Expected Results

A reasonable error message that addresses the issue in a clear and direct manner. Here is an example of what that would look like:

https://www.kaggle.com/c/titanic/discussion/26976

Error in predict.randomForest(rf, extractFeatures(test)) : missing values in newdata

Because scikit-learn's algorithms currently only accept numerical input (AFAIK), any non-numerical data should be treated as missing values or otherwise seen as aberrant.

Actual Results

Traceback (most recent call last):
  File "poop.py", line 5, in <module>
    LabelEncoder().fit_transform(feature_with_missing_value)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/label.py", line 112, in fit_transform
    self.classes_, y = np.unique(y, return_inverse=True)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/arraysetops.py", line 223, in unique
    return _unique1d(ar, return_index, return_inverse, return_counts)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/arraysetops.py", line 280, in _unique1d
    perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: unorderable types: str() < int()

Versions

Home:

Linux-4.4.0-137-generic-i686-with-Ubuntu-16.04-xenial
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609]
NumPy 1.14.2
SciPy 1.0.1
Scikit-Learn 0.19.1

Google Cloud:

System
------
   machine: Linux-4.14.33+-x86_64-with-debian-9.5
    python: 3.5.3 (default, Sep 27 2018, 17:25:39)  [GCC 6.3.0 20170516]
executable: /usr/bin/python3
BLAS
----
  lib_dirs:
    macros:
cblas_libs: cblas
Python deps
-----------
setuptools: 40.6.2
     scipy: 1.1.0
       pip: 9.0.1
    Cython: None
     numpy: 1.15.4
    pandas: 0.23.4
   sklearn: 0.20.0

Activity

  1. amueller commented on Nov 20, 2018

    @amueller
    Member

    Thanks for the report. Can confirm the same issue in master. It's actually unrelated to missing values. We try hard to give good error messages so your feedback is much appreciated.
    If you can reproduce the other issue, please let us know.
    Btw, the chance that you need to use a LabelEncoder is pretty slim (which is not an excuse for bad error messages). If it's actually a label, you don't need to encode it. If it's the data, you should be using OneHotEncoder (in version 0.20).

  2. amueller commented on Nov 20, 2018

    @amueller
    Member

    It looks like we tried to fix it in 0.20 but not successfully.
    The error is now in

    63         uniques = sorted(set(values))
    

    ping @jorisvandenbossche

  3. readyready15728 commented on Nov 20, 2018

    @readyready15728
    Author

    Btw, the chance that you need to use a LabelEncoder is pretty slim (which is not an excuse for bad error messages). If it's actually a label, you don't need to encode it. If it's the data, you should be using OneHotEncoder (in version 0.20).

    Someone on Stack Overflow said that LabelEncoder can outperform OneHotEncoder when used with XGBoost. I don't know whether or not that is true, but I achieved extreme, nearly 100% accuracy out of sample after using LabelEncoder so I think it's safe to assume it didn't get in the way.

  4. jorisvandenbossche commented on Nov 20, 2018

    @jorisvandenbossche
    Member

    It looks like we tried to fix it in 0.20 but not successfully.

    I don't think we "tried" to fix it, but in any case the same error happens both in 0.19 or in 0.20, it's indeed now coming from uniques = sorted(set(values)), while before it was coming from sorting in np.unique.

    So basically, we currently require that the categories are sortable.
    Only when you pass the categories manually (in the OneHotEncoder), I think you can overcome that constraint. I am not sure it is worth trying to 'fix' that constraint (as it mainly comes up with such mixed data as in the OP example).

    Regarding the error message: it would indeed be nice to have a more informative error message.
    We could catch the error when sorting, inspect the error if it is actually about sorting, and re-raise with a more informative error message. Although that is maybe a bit hacky?

  5. readyready15728 commented on Nov 20, 2018

    @readyready15728
    Author

    A somewhat ugly solution would be better than none at all.

  6. amueller commented on Nov 20, 2018

    @amueller
    Member

    @readyready15728 LabelEncoder is not an alternative to OneHotEncoder. If you want to encode the features as ordinal, you should use OrdinalEncoder. LabelEncoder is meant for outputs. (OrdinalEncoder is likely to raise the same error right now, though).

    @jorisvandenbossche I'm fine with catching it there.

  7. readyready15728 commented on Nov 20, 2018

    @readyready15728
    Author

    I added what you said about OrdinalEncoder to a comment thread on Stack Overflow and hopefully some people will see it. It really is surprising how often it's recommended to use LabelEncoder; the material on DataCamp does the very same thing.

  8. amueller commented on Nov 20, 2018

    @amueller
    Member

    That's because we took too long to fix it, it's only really working in 0.20 iirc.

  9. added
    good first issue
    on Nov 23, 2018
  10. changed the title [-]Better error messages needed for aberrant values[/-] [+]Better error message when passing un-sortable data to the Encoders[/+] on Nov 23, 2018
  11. romankarski commented on Nov 28, 2018

    @romankarski

    Hi,
    I am new in committing and this Issue looks interesting. Would You mind if i will try help You ? I also need this to pass my python classes :) If you would like to help me a little, will be great. I've started looking on scikit-learn/sklearn/preprocessing/label.py
    is that good for start ?

  12. amueller commented on Nov 28, 2018

    @amueller
    Member

    @romankarski as you can see above there is already a pull request referencing this issue at #12640

  13. amueller commented on Nov 28, 2018

    @amueller
    Member

    @jorisvandenbossche you added the help wanted after the PR went up, is there some issue with the PR?

  14. romankarski commented on Nov 28, 2018

    @romankarski

    @amueller Oh, i missed it

  15. 9 remaining items

  16. janvanrijn commented on Jan 14, 2019

    @janvanrijn
    Contributor

    Correct, but for that I'll have to pull it into the column transformer (and have a separate imputer component for the categorical and numeric indices, which the OpenML connector doesn't like)

    But it seems that there are several potential solutions on the table, I will work around this. Thanks for your input @jorisvandenbossche , really appreciated

  17. ShyamPandya commented on May 24, 2020

    @ShyamPandya

    Hi, @amueller @jorisvandenbossche , is this issue still up for grab? I see it's open since a year and there is a stale PR as well. If it is to be fixed, can I get the summary of what was decided as the fix for it? Thanks

  18. amueller commented on May 29, 2020

    @amueller
    Member

    @ShyamPandya feel free to take it up. The issue is still to improve the error message but the existing PR is stalled and doesn't raise an error properly.

  19. erodrago commented on Jun 6, 2020

    @erodrago
    Contributor

    take

  20. amueller commented on Jun 6, 2020

    @amueller
    Member

    related to #17294 I think.

  21. Praveenk8051 commented on Jul 24, 2020

    @Praveenk8051

    @amueller Is it still available ? Seems like its not solved, I'm first timer and would like to pitch in !!

  22. jnothman commented on Jul 25, 2020

    @jnothman
    Member

    I have cleared the assignee after 6 weeks of silence. It's open for taking as far as I'm concerned

  23. bmaisonn commented on Jul 25, 2020

    @bmaisonn
    Contributor

    I've just tried to reproduce this one but it now returns an appropriate error message

    TypeError: Encoders require their input to be uniformly strings or numbers. Got ['int', 'str']

  24. thomasjpfan commented on Jul 25, 2020

    @thomasjpfan
    Member

    @bmaisonn Thank you for looking into this issue!

    With that error message, it looks like this issue has already been resolved.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stalledgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions