CLASSICAL ML ยท INTERVIEW QUESTION
What is overfitting? How do you detect and prevent it?
๐ข beginner ยท Supervised Learning
Hint: Training performance vs validation performance gap.
Answer
Overfitting occurs when a model memorizes noise in training data, achieving high training accuracy but poor generalization to unseen data.
Detection
- Large gap between training and validation metrics
- High variance in cross-validation scores
- Model performance degrades on new data
Prevention Strategies
| Strategy | How it Helps |
|---|---|
| More data | Reduces chance of memorizing noise |
| L1/L2 regularization | Penalizes large weights |
| Cross-validation | Detects overfitting early |
| Early stopping | Stop training before memorization |
| Dropout | Randomly disables neurons during training |
| Data augmentation | Increases effective dataset size |
| Ensemble methods | Averages out individual model errors |
python
from sklearn.model_selection import cross_val_score
# Detect overfitting with cross-validation
scores = cross_val_score(model, X, y, cv=5)
print(f'CV scores: {scores.mean():.3f} ยฑ {scores.std():.3f}')
# High std suggests overfittingThe bias-variance tradeoff is key: overfitting = low bias, high variance. The goal is finding the sweet spot.
Related questions
- What is cross-validation? Compare k-fold, stratified, and time series CV.
- Explain precision, recall, F1-score, and when to use each.
- What is the ROC curve and AUC? What are their limitations?
- How do you handle class imbalance? Compare different approaches.
- Explain bias-variance tradeoff with a concrete example.