Categorical Encoding
Label Encode
Replace category strings with integer codes. Assign each unique category an
integer starting from 0. By hand, build a sorted-unique list, create a
mapping dict, then look up each label. With pandas,
astype('category').cat.codes produces the same codes because pandas assigns
category codes in sorted alphabetical order.
By hand
Build unique = sorted(set(labels)) — the same alphabetical order pandas
uses — then enumerate it to create mapping. The first loop shows the dict
filling in with bird→0, cat→1, dog→2. The second loop applies the
mapping to produce the code list.
labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']
unique = sorted(set(labels))
mapping = {}
for i, cat in enumerate(unique):
mapping[cat] = i
result = []
for s in labels:
result.append(mapping[s])
print('RESULT:', result)
labels ← ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']
1labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']2unique = sorted(set(labels))values this step['cat', 'dog', 'bird', 'dog', 'cat', 'bird']labelsunique ← ['bird', 'cat', 'dog']
1labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']2unique = sorted(set(labels))3mapping = {}values this step['bird', 'cat', 'dog']uniquemapping ← {}
2unique = sorted(set(labels))3mapping = {}4for i, cat in enumerate(unique):values this step{}mappingcat ← 'bird', i ← 0
3mapping = {}4for i, cat in enumerate(unique):5 mapping[cat] = ivalues this step'bird'cat0imapping ← {'bird': 0}
4for i, cat in enumerate(unique):5 mapping[cat] = i6result = []values this step{} → {'bird': 0}mappingcat ← 'cat', i ← 1
3mapping = {}4for i, cat in enumerate(unique):5 mapping[cat] = ivalues this step'bird' → 'cat'cat0 → 1imapping ← {'bird': 0, 'cat': 1}
4for i, cat in enumerate(unique):5 mapping[cat] = i6result = []values this step{'bird': 0} → {'bird': 0, 'cat': 1}mappingcat ← 'dog', i ← 2
3mapping = {}4for i, cat in enumerate(unique):5 mapping[cat] = ivalues this step'cat' → 'dog'cat1 → 2imapping ← {'bird': 0, 'cat': 1, 'dog': 2}
4for i, cat in enumerate(unique):5 mapping[cat] = i6result = []values this step{'bird': 0, 'cat': 1} → {'bird': 0, 'cat': 1, 'dog': 2}mappingfor i, cat in enumerate(unique):
3mapping = {}4for i, cat in enumerate(unique):5 mapping[cat] = iresult ← []
5 mapping[cat] = i6result = []7for s in labels:values this step[]results ← 'cat'
6result = []7for s in labels:8 result.append(mapping[s])values this step'cat'sresult ← [1]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[] → [1]results ← 'dog'
6result = []7for s in labels:8 result.append(mapping[s])values this step'cat' → 'dog'sresult ← [1, 2]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[1] → [1, 2]results ← 'bird'
6result = []7for s in labels:8 result.append(mapping[s])values this step'dog' → 'bird'sresult ← [1, 2, 0]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[1, 2] → [1, 2, 0]results ← 'dog'
6result = []7for s in labels:8 result.append(mapping[s])values this step'bird' → 'dog'sresult ← [1, 2, 0, 2]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[1, 2, 0] → [1, 2, 0, 2]results ← 'cat'
6result = []7for s in labels:8 result.append(mapping[s])values this step'dog' → 'cat'sresult ← [1, 2, 0, 2, 1]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[1, 2, 0, 2] → [1, 2, 0, 2, 1]results ← 'bird'
6result = []7for s in labels:8 result.append(mapping[s])values this step'cat' → 'bird'sresult ← [1, 2, 0, 2, 1, 0]
7for s in labels:8 result.append(mapping[s])9print('RESULT:', result)values this step[1, 2, 0, 2, 1] → [1, 2, 0, 2, 1, 0]resultfor s in labels:
6result = []7for s in labels:8 result.append(mapping[s])stdout ← RESULT: [1, 2, 0, 2, 1, 0]
8 result.append(mapping[s])9print('RESULT:', result)values this stepRESULT: [1, 2, 0, 2, 1, 0]stdout
With pandas
astype('category') converts the column to the pandas Categorical dtype and
sets the category list in sorted order. .cat.codes returns the integer code
for each element. The snapshot shows categories: ['bird', 'cat', 'dog'] so
the code→label correspondence is immediately visible.
import pandas as pd
from dalib.display import set_display
set_display()
labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']
df = pd.DataFrame({'c': labels})
s = df['c'].astype('category')
codes = s.cat.codes
result = codes.tolist()
print('categories:', s.cat.categories.tolist())
print('index:', codes.index.tolist())
print('dtype:', codes.dtype)
print('RESULT:', result)
categories: ['bird', 'cat', 'dog']
index: [0, 1, 2, 3, 4, 5]
dtype: int8
RESULT: [1, 2, 0, 2, 1, 0]
Implementation notes
- pandas assigns category codes in sorted order, not first-seen order.
The naive half uses
sorted(set(labels))to match — using insertion order (a plain dict walk) would produce different codes and break parity. cat.codesdtype isint8; codes start at 0 for the first sorted category.- For explicit control over the code assignment (e.g. keeping a specific
category as code 0), build a manual dict and use
.map(dict)instead. - Cross-reference:
encode-labels(ml-basics chapter) for one-hot and ordinal encoding alternatives.