Machine Learning (ML) · Beginner level

Beginner level · Module 2: Preparing Data for Machine Learning · Lesson 2 of 3

Cleaning and Encoding Data

Handle missing values, fix inconsistent categories and turn categories into numbers that models can use.

In this lesson you will

  • Choose strategies for missing values
  • Encode categorical features with one-hot encoding

Key concepts

imputationdropping rows or columnsone-hot encodingconsistent categories

The lesson

Missing values can be removed or filled (imputed). Dropping a few rows is fine; dropping half the data is not. Filling with the median for numbers or the most common value for categories is a simple, common start — and the fact that a value was missing can itself be useful information.

Fix inconsistent categories before encoding: 'Juba', 'juba' and 'Juba ' should be one value.

Models need numbers. One-hot encoding turns a category column into one 0/1 column per category. Do not simply number categories 1, 2, 3, because that suggests an order that does not exist.

Step by step

  1. Standardise a messy location column.
  2. Fill missing ages with the median.
  3. One-hot encode the school type column.
  4. Record each decision in a markdown cell.

Practice activity

Clean and encode the dropout data set, documenting every decision.

Expected result: A clean, fully numeric feature table with a documented list of changes.

Check your understanding

  1. Why not encode regions as 1, 2 and 3?

    Show answer

    It implies an order and distances between regions that do not exist.

  2. What is imputation?

    Show answer

    Filling in missing values, for example with the median.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.