Label Encoding
"red" හෝ "blue" කියන වචනයකට model එකකට ගණිතයක් කරන්න බෑ. Label encoding කරන්නෙ categories, algorithm එකකට process කරන්න පුළුවන් numbers බවට හරවන එක.
A model can't do maths on the word "red" or "blue". Label encoding turns categories into numbers so an algorithm can actually process them.
Lesson 16 of 64 · Part 1 · Python & Data Basics · Data Pre-processing · bilingual notes (English + සිංහල)
Why this matters. A model can't do maths on the word "red" or "blue". Label encoding turns categories into numbers so an algorithm can actually process them.
සිංහලෙන්: "red" හෝ "blue" කියන වචනයකට model එකකට ගණිතයක් කරන්න බෑ. Label encoding කරන්නෙ categories, algorithm එකකට process කරන්න පුළුවන් numbers බවට හරවන එක.
What you'll use this for. Any dataset with a text column (gender, city, product type) needs this step before it can be fed to almost any scikit-learn model.
සිංහලෙන්: Text column එකක් තියෙන ඕනම dataset එකකට (gender, city, product type) scikit-learn model එකකට දෙන්නත් කලින් මේ step එක ඕන.
Key principles
- Encoding order can accidentally imply rank that doesn't exist. If "red"=0, "blue"=1, "green"=2, some models may wrongly assume green > blue > red in some meaningful way — this is exactly why one-hot encoding exists for unordered categories.
Encode කරන පිළිවෙළ නිසා නැති rank එකක් accidentally implied වෙන්න පුළුවන්
Walkthrough
Label Encoding:
- converting the labels into numeric form
# importing the Dependencies
import pandas as pd
from sklearn.preprocessing import LabelEncoderLabel Encoding of Breast Cancer Dataset
# loading the data from csv file to pandas dataFrame
cancer_data = pd.read_csv('/content/data.csv')# first 5 rows of the dataframe
cancer_data.head()| id | diagnosis | radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | … | |
|---|---|---|---|---|---|---|---|---|
| 0 | 842302 | M | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | … |
| 1 | 842517 | M | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | … |
| 2 | 84300903 | M | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | … |
| 3 | 84348301 | M | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | … |
| 4 | 84358402 | M | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | … |
# finding the count of different labels
cancer_data['diagnosis'].value_counts()Output
B 357
M 212
Name: diagnosis, dtype: int64# load the Label Encoder function
label_encode = LabelEncoder()labels = label_encode.fit_transform(cancer_data.diagnosis)# appending the labels to the DataFrame
cancer_data['target'] = labelscancer_data.head()| id | diagnosis | radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | … | |
|---|---|---|---|---|---|---|---|---|
| 0 | 842302 | M | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | … |
| 1 | 842517 | M | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | … |
| 2 | 84300903 | M | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | … |
| 3 | 84348301 | M | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | … |
| 4 | 84358402 | M | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | … |
0 --> Benign
1 --> Malignant
cancer_data['target'].value_counts()Output
0 357
1 212
Name: target, dtype: int64Label Encoding of iris data
# loading the data from csv file to pandas dataFrame
iris_data = pd.read_csv('/content/iris_data.csv')iris_data.head()| Id | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | Species | |
|---|---|---|---|---|---|---|
| 0 | 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
| 1 | 2 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
| 2 | 3 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
| 3 | 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
| 4 | 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
iris_data['Species'].value_counts()Output
Iris-versicolor 50
Iris-virginica 50
Iris-setosa 50
Name: Species, dtype: int64# loding the label encoder
label_encoder_1 = LabelEncoder()iris_labels = label_encoder_1.fit_transform(iris_data.Species)iris_data['target'] = iris_labelsiris_data.head()| Id | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | Species | target | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | 0 |
| 1 | 2 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | 0 |
| 2 | 3 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | 0 |
| 3 | 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | 0 |
| 4 | 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | 0 |
iris_data['target'].value_counts()Output
2 50
1 50
0 50
Name: target, dtype: int64Iris-setosa --> 0
Iris-versicolor --> 1
Iris-virginica --> 2
Keep going: ← Lesson 15 · Lesson 17 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



