Reputation: 814
I'm trying to get the mappings of label encoder to figure out which code got each string for a column in my df.
Here is the encoding code:
y[:]=LabelEncoder().fit_transform(y[:])
I would like to get something like this as output:
A:1
B:2
C:3
Thanks for the help!
Upvotes: 5
Views: 3060
Reputation: 403208
You should refrain from in-line initialisation if you want to be able to make use of the mappings or inverse-transformation later.
data = ['A', 'A', 'B', 'C', 'B', 'B'] # `y`
le = LabelEncoder()
mapped = le.fit_transform(data)
mapping = dict(zip(le.classes_, range(1, len(le.classes_)+1)))
print(mapping)
# {'A': 1, 'B': 2, 'C': 3}
Better still, if you want to reverse the encoding, use inverse_transform
:
print(le.inverse_transform(mapped))
# ['A' 'A' 'B' 'C' 'B' 'B']
Upvotes: 4