Repository navigation
Description
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:
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
amueller commentedon Nov 20, 2018
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
LabelEncoderis 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).amueller commentedon Nov 20, 2018
It looks like we tried to fix it in 0.20 but not successfully.
The error is now in
ping @jorisvandenbossche
readyready15728 commentedon Nov 20, 2018
Someone on Stack Overflow said that
LabelEncodercan outperformOneHotEncoderwhen used with XGBoost. I don't know whether or not that is true, but I achieved extreme, nearly 100% accuracy out of sample after usingLabelEncoderso I think it's safe to assume it didn't get in the way.jorisvandenbossche commentedon Nov 20, 2018
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 innp.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?
readyready15728 commentedon Nov 20, 2018
A somewhat ugly solution would be better than none at all.
amueller commentedon Nov 20, 2018
@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.
readyready15728 commentedon Nov 20, 2018
I added what you said about
OrdinalEncoderto a comment thread on Stack Overflow and hopefully some people will see it. It really is surprising how often it's recommended to useLabelEncoder; the material on DataCamp does the very same thing.amueller commentedon Nov 20, 2018
That's because we took too long to fix it, it's only really working in 0.20 iirc.
[-]Better error messages needed for aberrant values[/-][+]Better error message when passing un-sortable data to the Encoders[/+]romankarski commentedon Nov 28, 2018
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 ?
amueller commentedon Nov 28, 2018
@romankarski as you can see above there is already a pull request referencing this issue at #12640
amueller commentedon Nov 28, 2018
@jorisvandenbossche you added the help wanted after the PR went up, is there some issue with the PR?
romankarski commentedon Nov 28, 2018
@amueller Oh, i missed it
9 remaining items
janvanrijn commentedon Jan 14, 2019
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
ShyamPandya commentedon May 24, 2020
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
amueller commentedon May 29, 2020
@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.
erodrago commentedon Jun 6, 2020
take
amueller commentedon Jun 6, 2020
related to #17294 I think.
Praveenk8051 commentedon Jul 24, 2020
@amueller Is it still available ? Seems like its not solved, I'm first timer and would like to pitch in !!
jnothman commentedon Jul 25, 2020
I have cleared the assignee after 6 weeks of silence. It's open for taking as far as I'm concerned
bmaisonn commentedon Jul 25, 2020
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']thomasjpfan commentedon Jul 25, 2020
@bmaisonn Thank you for looking into this issue!
With that error message, it looks like this issue has already been resolved.