Bird
Raised Fist0
ML Pythonml~12 mins

Why data preparation consumes most ML time in ML Python - Model Pipeline Impact

Choose your learning style10 modes available
Model Pipeline - Why data preparation consumes most ML time

This pipeline shows why preparing data takes most of the time in machine learning. It starts with raw data, cleans and changes it, then trains a model and checks how well it learns. Finally, it makes predictions.

Data Flow - 5 Stages
1Raw Data Collection
10000 rows x 10 columns→Collect data from different sources, may contain errors and missing values→10000 rows x 10 columns
Rows with missing age or wrong date format
↓
2Data Cleaning
10000 rows x 10 columns→Fix missing values, remove duplicates, correct errors→9800 rows x 10 columns
Removed 200 rows with missing target labels
↓
3Feature Engineering
9800 rows x 10 columns→Create new features, convert text to numbers, scale values→9800 rows x 15 columns
Added 'age group' and normalized 'income' columns
↓
4Train/Test Split
9800 rows x 15 columns→Split data into training and testing sets→7840 rows x 15 columns (train), 1960 rows x 15 columns (test)
Training set has 80% of data, test set 20%
↓
5Model Training
7840 rows x 15 columns→Train model on training data→Trained model
Model learns patterns from training data
Training Trace - Epoch by Epoch

Loss
1.0 |***************
0.8 |**********
0.6 |*******
0.4 |****
0.2 |**
0.0 +----------------
      1 3 5 7 10 Epochs
EpochLoss ↓Accuracy ↑Observation
10.850.55Model starts learning, loss high, accuracy low
30.600.70Loss decreases, accuracy improves
50.450.80Model learning well, metrics improving
70.350.85Loss lower, accuracy higher
100.300.88Training converging, good performance
Prediction Trace - 3 Layers
Layer 1: Input Feature Vector
Layer 2: Model Prediction Layer
Layer 3: Threshold Decision
Model Quiz - 3 Questions
Test your understanding
Why does data cleaning reduce the number of rows?
ABecause cleaning adds new rows
BBecause some rows have missing or wrong data
CBecause the model needs fewer rows
DBecause rows are converted to columns
Key Insight
Most machine learning time is spent preparing data because raw data is often messy and incomplete. Cleaning and changing data into a good form helps the model learn better and faster.

Practice

(1/5)
1. Why does data preparation usually take the most time in a machine learning project?
easy
A. Because testing the model takes a long time
B. Because training the model requires many calculations
C. Because raw data is often messy and needs cleaning before use
D. Because deploying the model to production is complex

Solution

  1. Step 1: Understand the nature of raw data

    Raw data collected from real sources often contains errors, missing values, and inconsistencies.
  2. Step 2: Recognize the need for cleaning and organizing

    Before training, data must be cleaned and formatted properly to ensure the model learns correctly.
  3. Final Answer:

    Because raw data is often messy and needs cleaning before use -> Option C
  4. Quick Check:

    Messy data = More preparation time [OK]
Hint: Messy raw data means more cleaning time [OK]
Common Mistakes:
  • Confusing model training time with data cleaning time
  • Thinking deployment takes the most time
  • Assuming testing is the longest step
2. Which of the following is the correct Python code snippet to check for missing values in a pandas DataFrame named df?
easy
A. df.isnull().sum()
B. df.missing_values()
C. df.check_na()
D. df.nulls_count()

Solution

  1. Step 1: Recall pandas method for missing values

    The method isnull() returns a DataFrame of booleans indicating missing values.
  2. Step 2: Summing missing values per column

    Using sum() on the boolean DataFrame counts missing values per column.
  3. Final Answer:

    df.isnull().sum() -> Option A
  4. Quick Check:

    pandas isnull + sum = count missing [OK]
Hint: Use df.isnull().sum() to count missing data [OK]
Common Mistakes:
  • Using non-existent pandas methods
  • Confusing isnull() with isna() (both work but only isnull() shown)
  • Trying to call methods that don't exist on DataFrame
3. Consider this Python code for data preparation:
import pandas as pd

data = {'age': [25, None, 30], 'salary': [50000, 60000, None]}
df = pd.DataFrame(data)
df_filled = df.fillna(df.mean())
print(df_filled)
What will be the output?
medium
A. DataFrame with missing values replaced by zeros
B. DataFrame unchanged with null values still present
C. Error because df.mean() cannot be used with fillna
D. DataFrame with missing values replaced by column means

Solution

  1. Step 1: Understand df.mean()

    Calculates mean of each numeric column ignoring null (NaN) values.
  2. Step 2: fillna with means

    fillna replaces missing values with the corresponding column mean.
  3. Final Answer:

    DataFrame with missing values replaced by column means -> Option D
  4. Quick Check:

    fillna(df.mean()) fills missing with mean [OK]
Hint: fillna(df.mean()) replaces missing with average [OK]
Common Mistakes:
  • Thinking fillna(df.mean()) causes error
  • Assuming missing values remain unchanged
  • Believing missing values become zero
4. You have this code snippet to drop rows with missing values:
import pandas as pd
data = {'height': [170, None, 160], 'weight': [70, 65, None]}
df = pd.DataFrame(data)
df_clean = df.dropna(axis=1)
print(df_clean)
What is wrong with this code if the goal is to remove rows with missing data?
medium
A. dropna() requires a threshold parameter to work
B. dropna(axis=1) removes columns, not rows
C. dropna() cannot be used on DataFrames
D. dropna(axis=1) removes rows, not columns

Solution

  1. Step 1: Understand dropna axis parameter

    axis=0 drops rows; axis=1 drops columns with missing values.
  2. Step 2: Identify the mistake

    Using axis=1 removes columns, but the goal is to remove rows.
  3. Final Answer:

    dropna(axis=1) removes columns, not rows -> Option B
  4. Quick Check:

    axis=1 means columns, not rows [OK]
Hint: Use dropna(axis=0) to remove rows with missing data [OK]
Common Mistakes:
  • Confusing axis=1 with rows
  • Thinking dropna always removes rows by default
  • Believing dropna needs threshold parameter
5. You have a dataset with many missing values and inconsistent formats. Which two data preparation steps combined will most improve model quality and reduce errors?
hard
A. Filling missing values with mean and converting all text to lowercase
B. Randomly shuffling data and dropping duplicate rows
C. Ignoring missing values and training the model directly
D. Removing all rows with any missing data and normalizing numeric columns

Solution

  1. Step 1: Consider filling missing values

    Replacing missing data with mean keeps data size and reduces bias from dropping rows.
  2. Step 2: Standardize text format

    Converting text to lowercase fixes inconsistencies and helps model learn better.
  3. Final Answer:

    Filling missing values with mean and converting all text to lowercase -> Option A
  4. Quick Check:

    Fill missing + text clean = better data [OK]
Hint: Fill missing + clean text improves data quality [OK]
Common Mistakes:
  • Dropping all rows loses too much data
  • Ignoring missing values causes errors
  • Shuffling or dropping duplicates alone won't fix missing data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard