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.
| name | learns, for each column | turns each value into |
|---|---|---|
"fill_mean" | the average of the values that are present | itself, or that average if is None |
"fill_median" | the median of the values that are present | itself, or that median if is None |
"minmax" | the smallest value and the largest value | |
"standard" | the average and the spread (divide by , not ) |
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.fitfollowed byPipeline.predict. The real work is deciding, step by step, which rows each transformer is allowed to learn from.