MLCrack

    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

    StrategyHow it Helps
    More dataReduces chance of memorizing noise
    L1/L2 regularizationPenalizes large weights
    Cross-validationDetects overfitting early
    Early stoppingStop training before memorization
    DropoutRandomly disables neurons during training
    Data augmentationIncreases effective dataset size
    Ensemble methodsAverages 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 overfitting

    The bias-variance tradeoff is key: overfitting = low bias, high variance. The goal is finding the sweet spot.