KatInterview

Reviewing a Venv Setup Script and Data-Prep Transformers

Machine LearningModel answer
MediumModel answerMachine Learningshell-scriptingsklearn-transformersdata-leakage

Last updated

Walk through a virtual-environment shell script, then review an outlier-clipping transformer and three imputers for design, style and testability, and write a key unit test.

You are reviewing a teammate's data-science repository. Answer the questions below as you would in a code review conversation.

Part 1: Environment script

#!/bin/bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
python3 -m venv "$ROOT/.venv"
source "$ROOT/.venv/bin/activate"
pip install --upgrade pip
pip install -r "$ROOT/requirements.txt"
python -m ipykernel install --user --name analysis-env
echo "done"
  1. Explain what each line does and why it is there.
  2. What does shell scripting give a team doing data-science engineering?

Part 2: Outlier clipping

class OutlierHandler:
    def __init__(self, cols, k=1.5):
        self.cols = cols
        self.k = k

    def fit(self, df):
        self.bounds = {}
        for c in self.cols:
            q1, q3 = df[c].quantile([0.25, 0.75])
            iqr = q3 - q1
            self.bounds[c] = (q1 - self.k * iqr, q3 + self.k * iqr)
        return self

    def transform(self, df):
        for c, (lo, hi) in self.bounds.items():
            df[c] = df[c].clip(lo, hi)
        print("clipping finished")
        return df
  1. Describe what the class is for.
  2. Why is it good practice to keep fit() and transform() as separate methods?
  3. List the style and design problems you see.
  4. Write the single unit test you consider most valuable.

Part 3: Imputation

from numpy import *

class MeanFiller:       # numeric columns: learns per-column means in fit, fills NaN in transform
class ModeFiller:       # categorical columns: learns most frequent value, fills missing entries
class ConstantFiller:   # fills every missing entry with a fixed user-supplied value
  1. Summarize at a high level what these three classes do.
  2. What style problems do you see in the script, in particular the first line (from numpy import *)? Justify each one.

Hints

Hint 1

For the shell script, think about what happens if one command fails midway, and what makes the environment identical on another machine.

Hint 2

Ask where the quartiles are computed. Which dataset should they come from, and what happens at serving time when only one row arrives?

Hint 3

For the unit test, fit on one dataset and transform a different one. Check that the bounds match the training data and that the input frame is unchanged.

Hint 4

For the wildcard import, list which built-in names NumPy would overwrite and what that does to a reader trying to trace where a name comes from.