KatInterview

URL Safety Classifier with Imbalanced Data

Machine LearningModel answer
MediumModel answerMachine Learningclassificationimbalanced-datafeature-engineering

Last updated

Design and evaluate a classifier to identify malicious URLs given limited labeled examples and severe class imbalance.

You need to build a classifier that identifies malicious URLs. You have access to 1,000 labeled URLs (marked as either malicious or safe), plus a larger collection of unlabeled URLs. Malicious links are uncommon in your dataset.

Your task is to:

  1. Design a feature set for URL classification
  2. Train a logistic regression model and explain your approach to handling class imbalance
  3. Define an evaluation strategy appropriate for imbalanced data, including:
    • Cross-validation approach
    • Appropriate metrics and why standard accuracy is insufficient
    • Model calibration assessment
  4. Select an operating threshold when blocking a safe URL costs 1 unit but allowing a malicious URL through costs 50 units
  5. Address potential pitfalls including data leakage, distribution shift between labeled and production data, and planning for deployment validation

Explain your reasoning at each step, including what questions you would ask stakeholders and what tradeoffs you're making.

Hints

Hint 1

With potentially only 10-100 malicious examples, feature dimensionality and validation strategy need careful consideration

Hint 2

Class imbalance affects which metrics are meaningful - consider what happens if you predict every URL as safe

Hint 3

The cost asymmetry should directly inform your decision threshold, not just your training procedure