Imbalanced text data means some categories have many examples, and others have very few. Handling this helps models learn better and make fair predictions.
When classifying emails as spam or not spam, but spam emails are much fewer.
When detecting rare events in customer reviews, like complaints versus normal feedback.
When sorting news articles into topics where some topics appear less often.
When building sentiment analysis models with many neutral texts but few positive or negative ones.
Syntax
NLP
from sklearn.utils import resample
# Upsample minority class
minority_upsampled = resample(minority_class_data, replace=True, n_samples=desired_count, random_state=42)
# Downsample majority class
majority_downsampled = resample(majority_class_data, replace=False, n_samples=desired_count, random_state=42)
Use resample to increase or decrease samples in classes.
Set random_state for reproducible results.
Examples
This makes the smaller class as big as the larger one by repeating samples.
NLP
from sklearn.utils import resample
# Upsample minority class to match majority
minority_upsampled = resample(minority_data, replace=True, n_samples=len(majority_data), random_state=1)
This reduces the larger class size by randomly picking fewer samples.
NLP
from sklearn.utils import resample
# Downsample majority class to match minority
majority_downsampled = resample(majority_data, replace=False, n_samples=len(minority_data), random_state=1)
SMOTE creates new synthetic samples for the minority class to balance data.
D. n_samples must be less than original minority size
Solution
Step 1: Check resample parameters
replace=True allows sampling with replacement, so n_samples can be larger than original minority size.
Step 2: Verify code behavior
random_state is optional; code runs fine and prints length 5 as expected.
Final Answer:
No error; code runs correctly and prints 5 -> Option A
Quick Check:
Upsampling with replacement works = A [OK]
Hint: replace=True allows larger sample size [OK]
Common Mistakes:
Thinking random_state is mandatory
Believing n_samples must be smaller
Confusing replace parameter usage
5. You have a text classification dataset with 90% class A and 10% class B. After upsampling class B to balance the data, which metric should you check to ensure your model performs well on both classes?
hard
A. Accuracy only
B. Precision and recall for each class
C. Training time
D. Number of epochs
Solution
Step 1: Understand metric importance
Accuracy can be misleading with imbalanced data; precision and recall show performance per class.
Step 2: Choose metrics for balanced evaluation
Precision and recall help check if model correctly identifies minority class without many false positives or negatives.
Final Answer:
Precision and recall for each class -> Option B
Quick Check:
Balanced data needs precision & recall check = C [OK]
Hint: Check precision and recall, not just accuracy [OK]
Common Mistakes:
Relying only on accuracy
Ignoring class-wise metrics
Focusing on training time or epochs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.