lte__
lte__

Reputation: 7576

LabelEncoder() won't store parameters?

LabelEncoder won't "remember" parameters. When I fit and transform data with it then ask for the parameters, all I get is {}. This makes it impossible to re-use the encoder on new data.

Example:

from sklearn.preprocessing import LabelEncoder

encode = LabelEncoder()
encode.fit_transform(['one', 'two', 'three'])
print(encode.get_params())

Not sure about the intended format, but I expect something like {['one', 0], ['two', 1], ['three', 2]}

Actual Result: {}

I'm on:

Darwin-16.7.0-x86_64-i386-64bit
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
NumPy 1.12.1
SciPy 0.19.0
Scikit-Learn 0.18.1

Upvotes: 5

Views: 3798

Answers (1)

Milton Arango G
Milton Arango G

Reputation: 805

Label encoder stores the parameters in the classes_ attribute. You can obtain the encoded values transforming those classes and creating a dictionary. This encoder will work with new data that has the same labels otherwise it will raise a ValueError. Use the transform method on the labels you want to encode and that's it.

from sklearn import preprocessing

encode = preprocessing.LabelEncoder()
encode.fit_transform(['one', 'two', 'three'])
keys = encode.classes_
values = encode.transform(encode.classes_)
dictionary = dict(zip(keys, values))
print(dictionary)

Output: {'three': 1, 'two': 2, 'one': 0}

Upvotes: 10

Related Questions