ish*_*ido 3 python label dictionary categorical-data
当用于LabelEncoder将分类变量编码为数字时,
如何保存跟踪转换的字典?
即一个字典,在其中我可以看到哪些值变成了什么:
{'A':1,'B':2,'C':3}
Run Code Online (Sandbox Code Playgroud)
我从创建了字典 classes_
le = preprocessing.LabelEncoder()
ids = le.fit_transform(labels)
mapping = dict(zip(le.classes_, range(len(le.classes_))))
Run Code Online (Sandbox Code Playgroud)
去测试:
all([mapping[x] for x in le.inverse_transform(ids)] == ids)
Run Code Online (Sandbox Code Playgroud)
应该回来True。
之所以有效,是因为fit_transform用于numpy.unique同时计算标签编码和classes_属性:
def fit_transform(self, y):
self.classes_, y = np.unique(y, return_inverse=True)
return y
Run Code Online (Sandbox Code Playgroud)