When we talk about data preparation time, the key metric is time efficiency. This means how long it takes to clean, organize, and transform data before training a model. Good data preparation saves time later and improves model quality. So, measuring the time spent on data preparation helps us understand why it takes most of the project time.
Why data preparation consumes most ML time in ML Python - Why Metrics Matter
Data Preparation Time Breakdown (Example):
+-------------------------+------------+
| Task | Time (hrs) |
+-------------------------+------------+
| Data Collection | 5 |
| Data Cleaning | 15 |
| Data Transformation | 10 |
| Feature Engineering | 8 |
| Data Validation | 2 |
+-------------------------+------------+
| Total | 40 |
+-------------------------+------------+
Data preparation tasks take 40 hours, which is 80% of a 50-hour ML project.
In data preparation, the tradeoff is between speed and quality. If you rush data cleaning, you save time but risk poor model results (low precision and recall). If you spend more time cleaning and transforming data carefully, the model learns better and predicts more accurately.
Example: For a spam filter, if data is messy, the model might wrongly mark good emails as spam (low precision). Spending more time fixing data helps avoid this mistake.
Good: Data preparation takes about 60-80% of total project time, ensuring clean, well-structured data that leads to high model accuracy and reliability.
Bad: Data preparation is rushed or skipped, taking less than 30% of time, resulting in noisy data, poor model performance, and more time fixing errors later.
- Ignoring data quality: High accuracy can be misleading if data is biased or incomplete.
- Data leakage: Poor preparation can cause information from test data to leak into training, inflating metrics falsely.
- Overfitting: Without proper feature engineering and cleaning, models may memorize noise, hurting real-world performance.
- Underestimating time: Not tracking data prep time leads to unrealistic project plans and rushed work.
No, it is not good. The model misses most fraud cases (low recall), which is dangerous. This often happens if data preparation missed important fraud examples or features. Spending more time preparing data can improve recall and make the model safer.
Practice
Solution
Step 1: Understand the nature of raw data
Raw data collected from real sources often contains errors, missing values, and inconsistencies.Step 2: Recognize the need for cleaning and organizing
Before training, data must be cleaned and formatted properly to ensure the model learns correctly.Final Answer:
Because raw data is often messy and needs cleaning before use -> Option CQuick Check:
Messy data = More preparation time [OK]
- Confusing model training time with data cleaning time
- Thinking deployment takes the most time
- Assuming testing is the longest step
df?Solution
Step 1: Recall pandas method for missing values
The methodisnull()returns a DataFrame of booleans indicating missing values.Step 2: Summing missing values per column
Usingsum()on the boolean DataFrame counts missing values per column.Final Answer:
df.isnull().sum() -> Option AQuick Check:
pandas isnull + sum = count missing [OK]
- 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
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?Solution
Step 1: Understand df.mean()
Calculates mean of each numeric column ignoring null (NaN) values.Step 2: fillna with means
fillna replaces missing values with the corresponding column mean.Final Answer:
DataFrame with missing values replaced by column means -> Option DQuick Check:
fillna(df.mean()) fills missing with mean [OK]
- Thinking fillna(df.mean()) causes error
- Assuming missing values remain unchanged
- Believing missing values become zero
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?Solution
Step 1: Understand dropna axis parameter
axis=0 drops rows; axis=1 drops columns with missing values.Step 2: Identify the mistake
Using axis=1 removes columns, but the goal is to remove rows.Final Answer:
dropna(axis=1) removes columns, not rows -> Option BQuick Check:
axis=1 means columns, not rows [OK]
- Confusing axis=1 with rows
- Thinking dropna always removes rows by default
- Believing dropna needs threshold parameter
Solution
Step 1: Consider filling missing values
Replacing missing data with mean keeps data size and reduces bias from dropping rows.Step 2: Standardize text format
Converting text to lowercase fixes inconsistencies and helps model learn better.Final Answer:
Filling missing values with mean and converting all text to lowercase -> Option AQuick Check:
Fill missing + text clean = better data [OK]
- Dropping all rows loses too much data
- Ignoring missing values causes errors
- Shuffling or dropping duplicates alone won't fix missing data
Ready to prove it?
Pass the test and this pattern moves unknown
