Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 6

    Data Cleaning

    Data Cleaning — Fix missing, duplicate, and invalid values before training.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Data Cleaning — Fix missing, duplicate, and invalid values before training. This lesson pairs the idea with a minimal Python/sklearn workflow you can extend in a notebook.

    Understanding the topic

    Concept Fix missing, duplicate, and invalid values before training.

    Workflow Load data → preprocess → fit or apply technique → measure on hold-out data.

    In practice Start with a small public dataset before jumping to proprietary production data.

    • Concept — Fix missing, duplicate, and invalid values before training.
    • Workflow — Load data → preprocess → fit or apply technique → measure on hold-out data.
    • In practice — Start with a small public dataset before jumping to proprietary production data.

    Step-by-step explanation

    1. Concept — Fix missing, duplicate, and invalid values before training.
    2. Workflow — Load data → preprocess → fit or apply technique → measure on hold-out data.
    3. In practice — Start with a small public dataset before jumping to proprietary production data.

    Informative example

    Python starter:

    python
    import numpy as np
    from sklearn.datasets import make_regression
    from sklearn.impute import SimpleImputer
    from sklearn.linear_model import Ridge
    from sklearn.metrics import root_mean_squared_error
    from sklearn.model_selection import train_test_split
    rng = np.random.default_rng(42)
    X, y = make_regression(n_samples=200, n_features=4, noise=8.0, random_state=42)
    mask = rng.random(X.shape) < 0.08
    X_dirty = X.copy()
    X_dirty[mask] = np.nan
    X_tr, X_te, y_tr, y_te = train_test_split(X_dirty, y, test_size=0.2, random_state=42)
    imp = SimpleImputer(strategy="median").fit(X_tr)
    model = Ridge(alpha=1.0).fit(imp.transform(X_tr), y_tr)
    rmse = root_mean_squared_error(y_te, model.predict(imp.transform(X_te)))
    print(f"Missing cells imputed: {int(mask.sum())} | Hold-out RMSE: {rmse:.2f}")

    Output

    Missing cells imputed: 64 | Hold-out RMSE: 19.42

    Execution workflow

    1Data Cleaning — workflow
    1 / 3

    Concept

    Fix missing, duplicate, and invalid values before training.

    Best practices

    • Hold out a test set before hyperparameter tuning.
    • Scale numeric columns for distance-based models.
    • Track multiple metrics — not accuracy alone on skewed labels.

    Common mistakes

    • Leaking test statistics into preprocessing fit on full data.
    • Training on the same rows you report as test performance.
    • Chasing complex models before a simple baseline.

    Hands-on exercise

    Practice:

    • Apply Data Cleaning on a sample dataset
    • Write down one metric that proves the technique helped

    Summary

    Data Cleaning: Fix missing, duplicate, and invalid values before training.

    Ready to mark this lesson complete?Track your journey across the entire course.