Python processing tags often need to display an array of tags with a set of numbers, and numbers are required constantly.
for example, [‘a’, ‘b’, ‘c’, ‘a’, ‘a’, ‘b’, ‘c’] == (a->0, b->1, c->2) => [0, 1, 2, 0, 0, 0, 1, 2]. To make this article easier to find, add the keyword: re-encoding
you can usesklearn.preprocessing.LabelEncoder()
this feature.
Taking the label as an example:
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit([1,2,2,6,3])
In [2]: le.classes_
Out[2]: array([1, 2, 3, 6])
In [3]: le.transform([1,1,3,6,2])
Out[3]: array([0, 0, 2, 3, 1], dtype=int64)
This is the “reverse coding”:
In [4]: le.inverse_transform([0, 0, 2, 3, 1])
Out[4]: array([1, 1, 3, 6, 2])
Unify the meaning of the non-digital poster:
In [5]: from sklearn import preprocessing
...: le =preprocessing.LabelEncoder()
...: le.fit(["paris", "paris", "tokyo", "amsterdam"])
...: print ('номер тега:% s'% le.classes_)
...: print ('Стандартизация значения тега: % s' % le.transform (["tokyo", "tokyo", "paris"]))))
...: print ('Стандартизированное изменение значения метки: % s' % le.inverse_transform ([2, 2, 1]))
...:
Количество тегов: [Амстердам '' 'Paris' 'Tokyo']
Стандартизация значения метки: [2 2 1]
Стандартизированное изменение стоимости этикетки: [«Токио» Токио «Париж»]