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

Handling Missing Values

සැබෑ data වල නම් හැම වෙලාවෙම gaps තියෙනවා — වැඩ නොකළ sensor එකක්, හිස්ව තිබූ form එකක්. ඒක කෙලින්ම model එකකට දුන්නොත් crash වෙනවා, නැත්නම් ඊටත් වඩා නරකට වැරදි දෙයක් ඉගෙන ගන්නවා.

Real-world data almost always has gaps — a sensor that failed, a form left blank. Feed that straight into a model and it crashes, or worse, learns the wrong lesson.

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

Lesson 14 of 64 · Part 1 · Python & Data Basics · Data Pre-processing · bilingual notes (English + සිංහල)

Why this matters. Real-world data almost always has gaps — a sensor that failed, a form left blank. Feed that straight into a model and it crashes, or worse, learns the wrong lesson.

සිංහලෙන්: සැබෑ data වල නම් හැම වෙලාවෙම gaps තියෙනවා — වැඩ නොකළ sensor එකක්, හිස්ව තිබූ form එකක්. ඒක කෙලින්ම model එකකට දුන්නොත් crash වෙනවා, නැත්නම් ඊටත් වඩා නරකට වැරදි දෙයක් ඉගෙන ගන්නවා.

What you'll use this for. This is the very first thing you check on any new dataset, before any visualization or modeling.

සිංහලෙන්: Visualization එකකට හෝ modeling එකකට කලින්, ඕනම අලුත් dataset එකක බලන පළමුවෙනිම දේ මේකයි.

Key principles

  • Deleting rows and filling values are different decisions with different costs. Drop a row and you lose that record entirely. Fill it with the average and you keep the row but add a guess — knowing which trade-off fits your data is the actual skill here.
    Rows delete කරන එකයි values පුරවන එකයි වෙනස් decisions දෙකක්, වෙනස් costs එකක්

Walkthrough

Methods to Handle Missing Values:

  1. Imputation
  2. Dropping

Importing the libraries

ExamplePython
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
ExamplePython
# loading the dataset to a Pandas DataFrame
dataset = pd.read_csv('/content/Placement_Dataset.csv')
ExamplePython
dataset.head()
sl_nogenderssc_pssc_bhsc_phsc_bhsc_s…
01M67.00Others91.00OthersCommerce…
12M79.33Central78.33OthersScience…
23M65.00Central68.00CentralArts…
34M56.00Central52.00CentralScience…
45M85.80Central73.60CentralCommerce…
ExamplePython
dataset.shape

Output

Output
(215, 15)
ExamplePython
dataset.isnull().sum()

Output

Output
sl_no              0
gender             0
ssc_p              0
ssc_b              0
hsc_p              0
hsc_b              0
hsc_s              0
degree_p           0
degree_t           0
workex             0
etest_p            0
specialisation     0
mba_p              0
status             0
salary            67
dtype: int64

Central Tendencies:

  1. Mean
  2. Median
  3. Mode
ExamplePython
# analyse the distribution of data in the salary
fig, ax = plt.subplots(figsize=(8,8))
sns.distplot(dataset.salary)

Output

Output
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version
  warnings.warn(msg, FutureWarning)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f503fe268d0>

Handling Missing Values — figure 1

Replace the missing values with Median value

ExamplePython
dataset['salary'].fillna(dataset['salary'].median(),inplace=True)
ExamplePython
dataset.isnull().sum()

Output

Output
sl_no             0
gender            0
ssc_p             0
ssc_b             0
hsc_p             0
hsc_b             0
hsc_s             0
degree_p          0
degree_t          0
workex            0
etest_p           0
specialisation    0
mba_p             0
status            0
salary            0
dtype: int64
ExamplePython
# filling missing values with Mean value:
# dataset['salary'].fillna(dataset['salary'].mean(),inplace=True)
ExamplePython
# filling missing values with Mean value:
# dataset['salary'].fillna(dataset['salary'].mode(),inplace=True)

Dropping Method

ExamplePython
salary_dataset = pd.read_csv('/content/Placement_Dataset.csv')
ExamplePython
salary_dataset.shape

Output

Output
(215, 15)
ExamplePython
salary_dataset.isnull().sum()

Output

Output
sl_no              0
gender             0
ssc_p              0
ssc_b              0
hsc_p              0
hsc_b              0
hsc_s              0
degree_p           0
degree_t           0
workex             0
etest_p            0
specialisation     0
mba_p              0
status             0
salary            67
dtype: int64
ExamplePython
# drop the missing values
salary_dataset = salary_dataset.dropna(how='any')
ExamplePython
salary_dataset.isnull().sum()

Output

Output
sl_no             0
gender            0
ssc_p             0
ssc_b             0
hsc_p             0
hsc_b             0
hsc_s             0
degree_p          0
degree_t          0
workex            0
etest_p           0
specialisation    0
mba_p             0
status            0
salary            0
dtype: int64
ExamplePython
salary_dataset.shape

Output

Output
(148, 15)

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