KatInterview

Design a Tabular Classification Pipeline from Raw CSV to Deployment

Machine LearningModel answer
MediumModel answerMachine Learningclassificationscikit-learnfeature-engineering

Last updated

Build a complete ML classification system: data splits, preprocessing, model training and tuning, evaluation metrics, serialization, and a batch inference entry point.

Tabular Classification Pipeline

You are given a CSV file containing labeled tabular data. Your task is to build a complete, production-minded classification pipeline from raw data to batch inference.

Your implementation must cover each of the following stages:

  1. Data loading — read the CSV and drop rows where the target label is missing.
  2. Stratified splits — divide the data into train, validation, and test sets while preserving class proportions in each split. Cross-validation runs only on the training portion; the test set is used exactly once.
  3. Preprocessing — handle missing values in numeric and categorical columns separately. Scale numeric features. Encode categorical features in a way that gracefully handles categories not seen during training.
  4. Model training — train at least two models: a simple linear baseline and a stronger tree-based or neural model.
  5. Hyperparameter tuning — use cross-validation to search over key hyperparameters for each model.
  6. Evaluation — report accuracy, precision, recall, and ROC-AUC on both the validation and test sets. Justify your choice of primary metric.
  7. Persistence — serialize the fully fitted pipeline (preprocessing + model) into a single artifact.
  8. Batch inference — implement a predict(input_csv_path, output_csv_path) function that loads the saved artifact, validates the input schema, runs prediction, and writes predictions and class probabilities to a CSV.

If you include a neural network, show a correct training loop: optimizer initialization, forward pass, loss computation, backward pass, and an explicit optimizer step.

After presenting your code, briefly address:

  • How you would detect and prevent data leakage.
  • How you would productionize this pipeline (monitoring, retraining triggers, schema validation).
  • How your approach changes if the classes are severely imbalanced.

Assume the dataset fits in memory. The target column name and any ID column (pass-through, not a feature) are supplied as arguments.

Hints

Hint 1

Fit all preprocessing statistics — imputer means, scaler parameters, encoder vocabulary — on training data only. Wrapping them in a single scikit-learn Pipeline with ColumnTransformer enforces this automatically inside every cross-validation fold and makes the artifact a single serializable object.

Hint 2

When choosing a primary metric, ask whether class imbalance is present. Accuracy can be misleading on skewed datasets; ROC-AUC or PR-AUC are more informative and threshold-independent, making them safer choices for hyperparameter selection.

Hint 3

For batch inference, enforce column order and check for missing features before calling predict. Silently dropping or reordering columns is a common source of silent prediction errors in production.