[英]get sklearn.LabelEncoder() mappings after fit_transform
I'm trying to get the mappings of label encoder to figure out which code got each string for a column in my df. 我正在尝试获取标签编码器的映射,以确定哪些代码为我的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! 谢谢您的帮助!
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
: 更好的是,如果要反转编码,请使用
inverse_transform
:
print(le.inverse_transform(mapped))
# ['A' 'A' 'B' 'C' 'B' 'B']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.