Apply a Fit/Transform Pipeline

Easy
~20 min
code completion

Apply a Fit/Transform Pipeline

A pipeline is an ordered list of transformation steps. Each step exposes two operations:

  • fit(X) → learns parameters from X and stores them internally
  • transform(X) → applies the learned transformation to X
  • The correct pattern:

    1. On training data: call fit_transform(X_train) for each step, passing the result to the next step

    2. On test data: call only transform(X_test) for each step — never refit

    You are given a pipeline as a list of step objects, each with .fit(X) and .transform(X) methods.

    Your task:

    Implement:

  • fit_pipeline(steps, X_train) — fits all steps sequentially, returning the final transformed training data
  • transform_pipeline(steps, X_test) — transforms test data through already-fitted steps
  • Example Tests

    Single mean-centering step: training output has zero mean

    Input: {"X_test":[[3]],"X_train":[[1],[3],[5]],"pipeline_spec":"mean_center_1step"}

    Expected: [[0]]

    Two steps applied in order: test output uses train statistics only

    Input: {"X_test":[[2]],"X_train":[[0],[4]],"pipeline_spec":"mean_center_then_scale_2step"}

    Expected: [[0]]

    Empty pipeline returns input unchanged

    Input: {"X_test":[[5]],"X_train":[[1],[2]],"pipeline_spec":"empty"}

    Expected: [[5]]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.