Bird
Raised Fist0

Handling Class Imbalance - Oversampling, Undersampling, Focal Loss

THEORYintermediateInterview freq: very_high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/4
Data Distribution

Class A has 90% of the samples, while Class B has only 10%.

🎯 Recognizing class imbalance is crucial for applying the right technique.
from collections import Counter
print(Counter(y_train))
Oversampling

Minority class samples are duplicated to match the majority class.

🎯 Oversampling can lead to overfitting if not handled properly.
from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(random_state=42)
X_res, y_res = ros.fit_resample(X_train, y_train)
Undersampling

Majority class samples are reduced to match the minority class.

🎯 Undersampling can lead to loss of information.
from imblearn.under_sampling import RandomUnderSampler
rus = RandomUnderSampler(random_state=42)
X_res, y_res = rus.fit_resample(X_train, y_train)
Focal Loss

Loss is adjusted to focus more on harder-to-classify examples.

🎯 Focal loss helps in training models on imbalanced datasets by focusing on difficult samples.
import tensorflow as tf
def focal_loss(gamma=2., alpha=0.25):
    def focal_loss_fixed(y_true, y_pred):
        y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())
        loss = -y_true * alpha * tf.pow(1 - y_pred, gamma) * tf.math.log(y_pred)
        return tf.reduce_mean(loss)
    return focal_loss_fixed
📊
Diagram
Diagram
┌───────────────────────────────────────────────────────────────┐
│                       Class Imbalance Handling                │
├───────────────────────────────────────────────────────────────┤
│                                                               │
│  Data Distribution:                                           │
│  ┌───────────────┐   ┌───────────────┐   ┌───────────────┐    │
│  │ Class A: 90%  │   │ Class B: 10%  │   │ Balanced Data │    │
│  └───────────────┘   └───────────────┘   └───────────────┘    │
│       │                    │                    │             │
│       ▼                    ▼                    ▼             │
│  Oversampling         Undersampling         Focal Loss       │
│  ┌───────────────┐   ┌───────────────┐   ┌───────────────┐    │
│  │ Duplicate     │   │ Remove        │   │ Adjust Loss   │    │
│  │ Minority      │   │ Majority      │   │ for Harder    │    │
│  │ Samples       │   │ Samples       │   │ Examples      │    │
│  └───────────────┘   └───────────────┘   └───────────────┘    │
│                                                               │
└───────────────────────────────────────────────────────────────┘
  • Data Distribution: Initial class imbalance
  • Oversampling: Duplicating minority class samples
  • Undersampling: Removing majority class samples
  • Focal Loss: Adjusting loss for harder examples
🔑
Key Moments
▲
Q: Why might oversampling lead to overfitting?
A: Oversampling duplicates minority class samples, which can cause the model to learn noise as if it were a significant pattern.
Q: What is a potential downside of undersampling?
A: Undersampling reduces the number of majority class samples, which can lead to loss of valuable information and potentially underfitting.
Q: How does focal loss help in handling class imbalance?
A: Focal loss adjusts the loss function to focus more on harder-to-classify examples, reducing the impact of easy-to-classify examples and improving model performance on imbalanced datasets.

This diagram and trace explain how to handle class imbalance using oversampling, undersampling, and focal loss. Oversampling duplicates minority class samples, which can lead to overfitting. Undersampling reduces majority class samples, risking information loss. Focal loss adjusts the loss function to focus on harder examples, improving performance on imbalanced datasets. Understanding these techniques is crucial for developing robust models in the presence of class imbalance.

Primary Focus

✓ Handling class imbalance in datasets using various techniques

Practice

(1/5)
1. Given the following Python code, what will be the output when calculating the ROC-AUC for a highly imbalanced dataset?
from sklearn.metrics import roc_auc_score
y_true = [0, 0, 0, 0, 1]
y_scores = [0.1, 0.2, 0.3, 0.4, 0.9]
roc_auc = roc_auc_score(y_true, y_scores)
print(roc_auc)
easy
A. 1.0
B. 0.75
C. 0.9
D. 0.5

Solution

  1. Step 1: Calculate ROC-AUC

    ROC-AUC measures the ability to distinguish between classes. Here, the positive class is perfectly ranked higher than the negative class.
  2. Step 2: Eliminate wrong options

    0.5 would indicate random guessing. 0.75 and 0.9 are incorrect as the positive class is perfectly ranked.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Perfect ranking gives ROC-AUC of 1.0. [OK]
Hint: Perfect ranking gives ROC-AUC of 1.0. [OK]
Common Mistakes:
2. You need to label a large dataset for a new domain with minimal existing labeled data. Which strategy should you apply and why?
hard
A. Human Annotation, because it ensures accuracy.
B. Weak Supervision, because it leverages existing data sources.
C. Programmatic Labeling, because it can be adapted to new domains quickly.
D. Active Learning, because it minimizes the amount of data needing manual labeling.

Solution

  1. Step 1: Understand the domain challenge.

    New domains often lack labeled data, making programmatic labeling a flexible choice.
  2. Step 2: Evaluate other strategies.

    Human annotation is accurate but slow. Weak supervision requires existing data sources, which may not be available. Active learning still requires some initial labeled data.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    New domain = Programmatic Labeling [OK]
Hint: New domain = Programmatic Labeling [OK]
Common Mistakes:
3. You are tasked with designing a fraud detection system. How would you ensure that your model does not suffer from data leakage?
hard
A. Ensure that features derived from future transactions are not used in the training set.
B. Include all available transaction data up to the current date in the training set.
C. Use cross-validation with shuffling to ensure robust performance.
D. Use a single train-test split to simplify the evaluation process.

Solution

  1. Step 1: Identify potential leakage sources in fraud detection

    Using future-derived features can cause leakage.
  2. Step 2: Evaluate each option

    Use cross-validation with shuffling to ensure robust performance. might introduce leakage if shuffling is inappropriate, Include all available transaction data up to the current date in the training set. risks future data leakage, Use a single train-test split to simplify the evaluation process. is simplistic and risky.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Avoid future-derived features in training [OK]
Hint: Avoid future-derived features in training [OK]
Common Mistakes:
4. You are tasked with designing a system to handle label drift in a real-time recommendation engine. Which approach would be most effective?
hard
A. Implement a feedback loop to update the model with recent user interactions.
B. Use a static label mapping based on historical data.
C. Increase the size of the training dataset.
D. Apply feature selection to reduce dimensionality.

Solution

  1. Step 1: Identify the best approach for handling label drift.

    A feedback loop allows the model to adapt to changes in label distribution by learning from recent interactions.
  2. Step 2: Eliminate wrong options.

    Static mappings (Use a static label mapping based on historical data.) and increasing dataset size (Increase the size of the training dataset.) don't adapt to real-time changes. Feature selection (Apply feature selection to reduce dimensionality.) doesn't address label drift.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Feedback loop adapts to label drift [OK]
Hint: Feedback loop adapts to label drift [OK]
Common Mistakes:
5. In a multilingual context, how can you adapt Word2Vec embeddings to handle multiple languages effectively?
hard
A. Use a multilingual BERT model instead of Word2Vec.
B. Train separate Word2Vec models for each language and concatenate the embeddings.
C. Translate all text to a single language before training Word2Vec.
D. Use a shared vocabulary across languages and train a single Word2Vec model.

Solution

  1. Step 1: Evaluate multilingual strategies

    Multilingual BERT is designed to handle multiple languages effectively, unlike Word2Vec.
  2. Step 2: Eliminate wrong options

    Train separate Word2Vec models for each language and concatenate the embeddings. doesn't capture cross-lingual semantics. Use a shared vocabulary across languages and train a single Word2Vec model. is impractical due to vocabulary differences. Translate all text to a single language before training Word2Vec. loses language-specific nuances.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Multilingual BERT for cross-language tasks [OK]
Hint: Multilingual BERT for cross-language tasks [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