Galle, LK--:--5/6 sites live

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.

6 May 20264 min readSelf Guide · Stage 13#Python & Data Basics#Data Pre-processing#Machine learning

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
ExamplePython
# importing the Dependencies
import pandas as pd
from sklearn.preprocessing import LabelEncoder

Label Encoding of Breast Cancer Dataset

ExamplePython
# loading the data from csv file to pandas dataFrame
cancer_data = pd.read_csv('/content/data.csv')
ExamplePython
# first 5 rows of the dataframe
cancer_data.head()
iddiagnosisradius_meantexture_meanperimeter_meanarea_meansmoothness_mean…
0842302M17.9910.38122.801001.00.11840…
1842517M20.5717.77132.901326.00.08474…
284300903M19.6921.25130.001203.00.10960…
384348301M11.4220.3877.58386.10.14250…
484358402M20.2914.34135.101297.00.10030…
ExamplePython
# finding the count of different labels
cancer_data['diagnosis'].value_counts()

Output

Output
B    357
M    212
Name: diagnosis, dtype: int64
ExamplePython
# load the Label Encoder function
label_encode = LabelEncoder()
ExamplePython
labels = label_encode.fit_transform(cancer_data.diagnosis)
ExamplePython
# appending the labels to the DataFrame
cancer_data['target'] = labels
ExamplePython
cancer_data.head()
iddiagnosisradius_meantexture_meanperimeter_meanarea_meansmoothness_mean…
0842302M17.9910.38122.801001.00.11840…
1842517M20.5717.77132.901326.00.08474…
284300903M19.6921.25130.001203.00.10960…
384348301M11.4220.3877.58386.10.14250…
484358402M20.2914.34135.101297.00.10030…

0 --> Benign

1 --> Malignant

ExamplePython
cancer_data['target'].value_counts()

Output

Output
0    357
1    212
Name: target, dtype: int64

Label Encoding of iris data

ExamplePython
# loading the data from csv file to pandas dataFrame
iris_data = pd.read_csv('/content/iris_data.csv')
ExamplePython
iris_data.head()
IdSepalLengthCmSepalWidthCmPetalLengthCmPetalWidthCmSpecies
015.13.51.40.2Iris-setosa
124.93.01.40.2Iris-setosa
234.73.21.30.2Iris-setosa
344.63.11.50.2Iris-setosa
455.03.61.40.2Iris-setosa
ExamplePython
iris_data['Species'].value_counts()

Output

Output
Iris-versicolor    50
Iris-virginica     50
Iris-setosa        50
Name: Species, dtype: int64
ExamplePython
# loding the label encoder
label_encoder_1 = LabelEncoder()
ExamplePython
iris_labels = label_encoder_1.fit_transform(iris_data.Species)
ExamplePython
iris_data['target'] = iris_labels
ExamplePython
iris_data.head()
IdSepalLengthCmSepalWidthCmPetalLengthCmPetalWidthCmSpeciestarget
015.13.51.40.2Iris-setosa0
124.93.01.40.2Iris-setosa0
234.73.21.30.2Iris-setosa0
344.63.11.50.2Iris-setosa0
455.03.61.40.2Iris-setosa0
ExamplePython
iris_data['target'].value_counts()

Output

Output
2    50
1    50
0    50
Name: target, dtype: int64

Iris-setosa --> 0

Iris-versicolor --> 1

Iris-virginica --> 2


Keep going: ← Lesson 15 · Lesson 17 → · Track it in the Self Guide