Bird
Raised Fist0

Handling Missing Data - Imputation Strategies and When to Drop

THEORYintermediateInterview freq: high
Choose your preparation mode6 modes available
⚖️
Imputation Strategies vs Dropping Missing Data

In a dataset with 20% missing values, would you use imputation or drop the missing data? Justify your choice.

📊
Comparison
DimensionImputation StrategiesDropping Missing Data
Dataset Size PreservationHighLow
Bias Introduction RiskMediumLow
Computational CostHighLow
SimplicityLowHigh
Handling High MissingnessMediumLow
✅
Verdict
Default to imputation strategies when missing data is under 30% and dataset size is manageable; switch to dropping data only when missingness is minimal and dataset size is large.
💡
Key Insight
The choice between imputation and dropping data hinges on the balance between preserving information and managing computational complexity.
⚠️
Interview Trap
❌ Wrong: Always use imputation because it preserves data.
Ignoring the computational cost and potential bias introduced by imputation.
✅ Consider the percentage of missing data, dataset size, and computational resources before deciding.
Imputation Strategies
"Preserves dataset size and potential information by filling in missing values."
  • Can introduce bias if the imputation model is not well-chosen.
  • Computationally expensive for large datasets with complex imputation models.
  • May lead to overfitting if the imputed values are not representative of true data.
Ideal When
  • When missing data is less than 30% of the dataset.
  • When the dataset size is less than 100K rows.
  • When computational resources are available for complex imputation models.
Netflix uses imputation strategies to handle missing user ratings to improve recommendation systems.
Dropping Missing Data
"Simplifies the dataset by removing incomplete entries, reducing noise."
  • Loss of potentially valuable data, especially if missingness is not random.
  • Can significantly reduce dataset size, impacting model training.
  • Not suitable for datasets with high missing value percentages.
Ideal When
  • When missing data is less than 5% of the dataset.
  • When the dataset size is greater than 1M rows.
  • When missingness is completely at random (MCAR).
Airbnb drops listings with incomplete data to ensure quality and reliability in search results.
🐍
Imputation Strategies - Example
▼ Show
Python
import numpy as np
from sklearn.impute import SimpleImputer

# Sample data with missing values
X = np.array([[1, 2, np.nan], [4, np.nan, 6], [7, 8, 9]])

# Impute missing values with mean
imputer = SimpleImputer(strategy='mean')
X_imputed = imputer.fit_transform(X)
print(X_imputed)
🐍
Dropping Missing Data - Example
▼ Show
Python
import numpy as np

# Sample data with missing values
X = np.array([[1, 2, np.nan], [4, np.nan, 6], [7, 8, 9]])

# Drop rows with any missing values
X_dropped = X[~np.isnan(X).any(axis=1)]
print(X_dropped)
✅
Use Imputation Strategies When
  • •When missing data is less than 30% and dataset size is under 100K rows.
  • •When computational resources allow for complex imputation models.
  • •When preserving dataset size is crucial for model performance.
✅
Use Dropping Missing Data When
  • •When missing data is less than 5% and dataset size is over 1M rows.
  • •When missingness is completely at random (MCAR).
  • •When simplicity and speed are prioritized over potential data loss.
↳
Nuances
  • Imputation can introduce bias if the missing data is not missing at random.
  • Dropping data is often inappropriate for small datasets as it can lead to significant information loss.
  • For datasets with structured missingness, imputation can leverage patterns to improve accuracy.

Practice

(1/5)
1. In the context of handling class imbalance, which technique is specifically designed to adjust the loss function to focus more on hard-to-classify examples?
easy
A. Oversampling
B. Undersampling
C. Cost-sensitive Learning
D. Focal Loss

Solution

  1. Step 1: Identify the technique that modifies the loss function.

    Focal Loss is designed to focus more on hard-to-classify examples by adjusting the loss function.
  2. Step 2: Eliminate wrong options.

    Oversampling and undersampling adjust the dataset, not the loss function. Cost-sensitive learning modifies the cost, not the focus on hard examples.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Focal Loss focuses on hard examples. [OK]
Hint: Focal Loss focuses on hard examples. [OK]
Common Mistakes:
2. Given the following Python code, what will be the output if the model is overfitting?
from sklearn.metrics import accuracy_score
train_preds = model.predict(X_train)
val_preds = model.predict(X_val)
train_acc = accuracy_score(y_train, train_preds)
val_acc = accuracy_score(y_val, val_preds)
print(f'Train Accuracy: {train_acc}, Validation Accuracy: {val_acc}')
easy
A. Train Accuracy: 0.95, Validation Accuracy: 0.60
B. Train Accuracy: 0.95, Validation Accuracy: 0.95
C. Train Accuracy: 0.60, Validation Accuracy: 0.60
D. Train Accuracy: 0.60, Validation Accuracy: 0.95

Solution

  1. Step 1: Understand overfitting output

    Overfitting results in high training accuracy but low validation accuracy.
  2. Step 2: Eliminate wrong options

    Train Accuracy: 0.95, Validation Accuracy: 0.95 suggests good generalization, Train Accuracy: 0.60, Validation Accuracy: 0.60 suggests underfitting, and Train Accuracy: 0.60, Validation Accuracy: 0.95 is unrealistic for overfitting.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    High train, low validation = overfitting [OK]
Hint: High train, low validation = overfitting [OK]
Common Mistakes:
3. A model using Batch Normalization is showing unstable training behavior with fluctuating loss. What is the most likely cause?
medium
A. The batch size is too small, leading to unreliable batch statistics.
B. The learning rate is too high, causing instability.
C. The model architecture is too deep, causing vanishing gradients.
D. The dataset is imbalanced, affecting the normalization process.

Solution

  1. Step 1: Identify the role of batch size in Batch Normalization.

    Batch Normalization relies on accurate batch statistics, which can be unreliable with small batch sizes.
  2. Step 2: Eliminate other causes.

    The learning rate is too high, causing instability. is a common issue but not specific to Batch Norm. The model architecture is too deep, causing vanishing gradients. is unrelated to normalization. The dataset is imbalanced, affecting the normalization process. affects model performance but not directly related to Batch Norm instability.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Small batch size causes unstable Batch Norm. [OK]
Hint: Small batch size causes unstable Batch Norm. [OK]
Common Mistakes:
4. A model performs well offline but poorly online. What is the most likely cause of this discrepancy?
medium
A. The online environment has a different data distribution.
B. The model's hyperparameters are not optimized.
C. The model is overfitting to the training data.
D. The offline evaluation used an incorrect metric.

Solution

  1. Step 1: Analyze the difference between offline and online environments.

    Offline evaluation uses historical data, while online evaluation deals with live data, which may have a different distribution.
  2. Step 2: Eliminate other potential causes.

    The model is overfitting to the training data. is incorrect because overfitting would likely affect both offline and online performance. The model's hyperparameters are not optimized. is incorrect because hyperparameter optimization affects both environments. The offline evaluation used an incorrect metric. is incorrect because using an incorrect metric would not cause a discrepancy between environments.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Different data distribution online. [OK]
Hint: Different data distribution online. [OK]
Common Mistakes:
5. You are tasked with designing a monitoring system for an ML model in production. Which approach ensures robust detection of both data drift and performance decay?
hard
A. Implement a feedback loop using user satisfaction surveys.
B. Use a combination of statistical tests and performance metrics.
C. Rely solely on periodic model retraining.
D. Monitor only the model's prediction confidence scores.

Solution

  1. Step 1: Identify comprehensive monitoring strategies

    Combining statistical tests with performance metrics covers both drift and decay.
  2. Step 2: Eliminate wrong options

    Implement a feedback loop using user satisfaction surveys. is subjective and indirect. Rely solely on periodic model retraining. is reactive, not proactive. Monitor only the model's prediction confidence scores. is too narrow in scope.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Combine stats and metrics for full coverage [OK]
Hint: Combine stats and metrics for full coverage [OK]
Common Mistakes:
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