Elffuss BraidHub

kikocis/auto-code-rover public

main · 2.1 KiBRawPermalink
1OneHotEncoder ignore unknown error when categories are strings
2#### Description
3
4This bug is very specific, but it happens when you set OneHotEncoder to ignore unknown entries.
5and your labels are strings. The memory of the arrays is not handled safely and it can lead to a ValueError
6
7Basically, when you call the transform method it will sets all the unknown strings on your array to OneHotEncoder.categories_[i][0] which is the first category alphabetically sorted given for fit
8If this OneHotEncoder.categories_[i][0] is a long string, and the array that you want to transform has small strings, then it is impossible to fit the whole OneHotEncoder.categories_[i][0] into the entries of the array we want to transform. So OneHotEncoder.categories_[i][0] is truncated and this raise the ValueError.
9
10
11
12#### Steps/Code to Reproduce
13```
14
15import numpy as np
16from sklearn.preprocessing import OneHotEncoder
17
18
19# It needs to be numpy arrays, the error does not appear
20# is you have lists of lists because it gets treated like an array of objects.
21train = np.array([ '22','333','4444','11111111' ]).reshape((-1,1))
22test = np.array([ '55555', '22' ]).reshape((-1,1))
23
24ohe = OneHotEncoder(dtype=bool,handle_unknown='ignore')
25
26ohe.fit( train )
27enc_test = ohe.transform( test )
28
29```
30
31
32#### Expected Results
33Here we should get an sparse matrix 2x4 false everywhere except at (1,1) the '22' that is known
34
35#### Actual Results
36
37> ValueError: y contains previously unseen labels: ['111111']
38
39
40#### Versions
41System:
42 python: 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
43 machine: Linux-4.4.0-138-generic-x86_64-with-Ubuntu-16.04-xenial
44executable: /usr/bin/python
45
46BLAS:
47 macros: HAVE_CBLAS=None
48cblas_libs: openblas, openblas
49 lib_dirs: /usr/lib
50
51Python deps:
52 Cython: 0.25.2
53 scipy: 0.18.1
54setuptools: 36.7.0
55 pip: 9.0.1
56 numpy: 1.15.2
57 pandas: 0.19.1
58 sklearn: 0.21.dev0
59
60
61
62#### Comments
63
64I already implemented a fix for this issue, where I check the size of the elements in the array before, and I cast them into objects if necessary.