The Fruit Stall's Recipe

Data ProcessingMedium+20 XP+20

The Fruit Stall's Recipe

A fruit stall sorts its stock with a small recipe: a few preparation steps, then a model. In scikit-learn you would wrap the whole thing in a Pipeline, call fit once on the training rows and predict on new ones. Here you build that behaviour yourself, in plain Python, so every number the recipe learns passes through your hands.

Task: write pipeline_predict(steps, X_train, y_train, X_new), which fits the recipe on the training rows and then uses it on the new rows.

The rows. X_train and X_new are lists of rows, and each row is a list of numbers. A missing value is written None. y_train holds one label per training row.

The preparation steps. steps lists step names in the order they run. Each step is a transformer: while fitting it learns a few numbers per column, and from then on it uses those numbers to transform rows.

namelearns, for each columnturns each value xxx into
"fill_mean"the average of the values that are presentxxx itself, or that average if xxx is None
"fill_median"the median of the values that are presentxxx itself, or that median if xxx is None
"minmax"the smallest value mmm and the largest value MMM(x−m)/(M−m)(x - m) / (M - m)(x−m)/(M−m)
"standard"the average μ\muμ and the spread σ\sigmaσ (divide by nnn, not n−1n - 1n−1)(x−μ)/σ(x - \mu) / \sigma(x−μ)/σ

The model. After the last step comes a 1-nearest-neighbour classifier. For each prepared new row it finds the closest prepared training row by straight-line (Euclidean) distance and predicts that row's label. If two training rows are exactly as close, the earlier one wins.

Return a tuple (prepared, predictions):

  • prepared: the rows of X_new exactly as the model receives them, every value a float rounded to 4 decimal places;
  • predictions: one label per row of X_new.

Round only for the output: the model measures its distances with the unrounded values.

In every test, a column with gaps meets a fill step before it meets a scaler, and no column reaches a scaler with zero range or zero spread.

This is the whole job of Pipeline.fit followed by Pipeline.predict. The real work is deciding, step by step, which rows each transformer is allowed to learn from.

weight
110170
sweetness
28
0.00.51.0one shared scale

In raw numbers weight spans 60 while sweetness spans 6. The highlighted pair sits far apart on one track and almost together on the other.

Examples

steps = ["minmax"]
X_train = [[110, 2], [130, 3], [150, 6], [170, 8]]
y_train = ["lemon", "lemon", "apple", "apple"]
X_new = [[100, 7], [165, 4]]
returns ([[-0.1667, 0.8333], [0.9167, 0.3333]], ['lemon', 'apple'])
steps = ["fill_mean", "standard"]
X_train = [[150, 7], [None, 8], [170, 3], [130, 2], [None, 2], [160, 6]]
y_train = ["apple", "apple", "lemon", "lemon", "lemon", "apple"]
X_new = [[None, 5], [140, 7]]
returns ([[0.0, 0.1374], [-1.0351, 0.9615]], ['apple', 'apple'])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run