Data Preparation
Encode Labels
Map class-name strings to integer codes using sorted unique order: build
sorted(set(labels)) → dict mapping each class to its index → encode each
label via lookup loop. Library: sklearn.preprocessing.LabelEncoder() .fit_transform(labels) assigns codes in sorted alphabetical order, matching
the naive approach exactly. RESULT: integer code list.
By hand
labels=['cat','dog','bird','cat','bird','dog','cat']. Sorted unique: ['bird','cat','dog'] → {bird:0, cat:1, dog:2}. Encoded: [1,2,0,1,0,2,1].
naive.py
Replay: real traced execution (multi-file project)
labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']
unique = sorted(set(labels))
code_map = {}
for i in range(len(unique)):
code_map[unique[i]] = i
encoded = []
for lbl in labels:
encoded.append(code_map[lbl])
print('RESULT:', encoded)
labels ← ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']
1labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']2unique = sorted(set(labels))values this step['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']labelsunique ← ['bird', 'cat', 'dog']
1labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']2unique = sorted(set(labels))3code_map = {}values this step['bird', 'cat', 'dog']uniquecode_map ← {}
2unique = sorted(set(labels))3code_map = {}4for i in range(len(unique)):values this step{}code_mapi ← 0
3code_map = {}4for i in range(len(unique)):5 code_map[unique[i]] = ivalues this step0icode_map ← {'bird': 0}
4for i in range(len(unique)):5 code_map[unique[i]] = i6encoded = []values this step{} → {'bird': 0}code_mapi ← 1
3code_map = {}4for i in range(len(unique)):5 code_map[unique[i]] = ivalues this step0 → 1icode_map ← {'bird': 0, 'cat': 1}
4for i in range(len(unique)):5 code_map[unique[i]] = i6encoded = []values this step{'bird': 0} → {'bird': 0, 'cat': 1}code_mapi ← 2
3code_map = {}4for i in range(len(unique)):5 code_map[unique[i]] = ivalues this step1 → 2icode_map ← {'bird': 0, 'cat': 1, 'dog': 2}
4for i in range(len(unique)):5 code_map[unique[i]] = i6encoded = []values this step{'bird': 0, 'cat': 1} → {'bird': 0, 'cat': 1, 'dog': 2}code_mapfor i in range(len(unique)):
3code_map = {}4for i in range(len(unique)):5 code_map[unique[i]] = iencoded ← []
5 code_map[unique[i]] = i6encoded = []7for lbl in labels:values this step[]encodedlbl ← 'cat'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'cat'lblencoded ← [1]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[] → [1]encodedlbl ← 'dog'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'cat' → 'dog'lblencoded ← [1, 2]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1] → [1, 2]encodedlbl ← 'bird'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'dog' → 'bird'lblencoded ← [1, 2, 0]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1, 2] → [1, 2, 0]encodedlbl ← 'cat'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'bird' → 'cat'lblencoded ← [1, 2, 0, 1]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1, 2, 0] → [1, 2, 0, 1]encodedlbl ← 'bird'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'cat' → 'bird'lblencoded ← [1, 2, 0, 1, 0]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1, 2, 0, 1] → [1, 2, 0, 1, 0]encodedlbl ← 'dog'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'bird' → 'dog'lblencoded ← [1, 2, 0, 1, 0, 2]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1, 2, 0, 1, 0] → [1, 2, 0, 1, 0, 2]encodedlbl ← 'cat'
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])values this step'dog' → 'cat'lblencoded ← [1, 2, 0, 1, 0, 2, 1]
7for lbl in labels:8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this step[1, 2, 0, 1, 0, 2] → [1, 2, 0, 1, 0, 2, 1]encodedfor lbl in labels:
6encoded = []7for lbl in labels:8 encoded.append(code_map[lbl])stdout ← RESULT: [1, 2, 0, 1, 0, 2, 1]
8 encoded.append(code_map[lbl])9print('RESULT:', encoded)values this stepRESULT: [1, 2, 0, 1, 0, 2, 1]stdout
With scikit-learn
LabelEncoder().fit_transform(labels) returns an integer array; .tolist()
converts it. classes_ shows the sorted alphabet used for the mapping.
library.py
from sklearn.preprocessing import LabelEncoder
from dalib.display import set_display
set_display()
labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']
le = LabelEncoder()
encoded = le.fit_transform(labels).tolist()
print('classes:', le.classes_.tolist())
print('RESULT:', encoded)
classes: ['bird', 'cat', 'dog']
RESULT: [1, 2, 0, 1, 0, 2, 1]
Implementation notes
- LabelEncoder assigns codes in sorted (alphabetical) order — not insertion
order.
sorted(set(labels))in the naive code reproduces this exactly. setordering is hash-based and NOT deterministic; thesorted()call is required for parity.- LabelEncoder is for target labels (y). For input features (X) containing
categorical strings, use
OrdinalEncoder(single column) orOneHotEncoder(multiple columns) instead. - Cross-reference:
label-encode(data-cleaning) uses the same sorted-order convention via pandascat.codes; both sklearn and pandas sort before assigning codes.