KatInterview

End-to-End ML Pipeline with NumPy and Pandas Only

Machine LearningModel answer
MediumModel answerMachine Learningsupervised-learninggradient-descentfeature-engineering

Last updated

Build a complete, leak-free machine learning pipeline — preprocessing, training, and evaluation — using only NumPy and pandas, no ML libraries.

The Challenge

You receive a tabular dataset as a pandas DataFrame df. It contains a mix of numeric and categorical feature columns plus a target column y that is either a continuous value (regression) or a binary label (classification).

Your tools are limited to pandas, numpy, and the Python standard library. You may look up documentation freely, but ML frameworks like scikit-learn, statsmodels, or XGBoost are off the table.

Build a working, end-to-end pipeline that covers the following four areas.


1. Data Preparation

  • Deal with missing values in both numeric and categorical columns.
  • Convert categorical variables into a numeric representation the model can consume.
  • Partition the data into training and validation sets (or implement cross-validation).
  • Decide when feature scaling is necessary and apply it correctly, making sure nothing leaks from validation into the training fit.

2. Model Implementation

  • Pick a sensible baseline: linear regression for a continuous target, logistic regression for a binary one.
  • Write the training loop using gradient descent in NumPy — no fit() calls from any library.
  • Implement a predict function that works on new data.

3. Evaluation

  • Choose metrics that match the task (e.g., RMSE / R² for regression; accuracy, precision, recall, F1, AUC for classification).
  • Show how to detect overfitting from the train vs. validation metric gap, and describe what you would do about it.

4. Concept Questions

Be ready to explain:

  • Bias–variance tradeoff — what it is and how your choices affect it.
  • Regularization — how L1 and L2 differ mathematically and in the solutions they produce.
  • Class imbalance — when it causes problems and how to handle it.
  • Feature scaling — which algorithms need it and why.
  • Data leakage — common sources and how to design a pipeline that avoids them.

What the answer should demonstrate: a correct, well-structured pipeline; awareness of edge cases; the ability to reason about trade-offs rather than just reciting formulas.

Hints

Hint 1

Split the data before computing any summary statistics (medians, means, standard deviations, category vocabularies). If you fit the preprocessor on the whole dataset first, you've already leaked information from the validation set into your features.

Hint 2

The gradient update rule for both linear and logistic regression has the same form: Xᵀ(ŷ − y) / n. The only difference is whether ŷ is a raw dot product or passed through a sigmoid. One training loop with a task flag handles both cases cleanly.

Hint 3

AUC can be computed without sorting predictions into a ROC curve by hand — use the Wilcoxon-Mann-Whitney statistic: rank the scores, sum the ranks of the positive examples, then apply the formula. Watch out for the edge case where a split contains only one class.