Bird
0
0

What is wrong with this pipeline code snippet?

medium📝 Debug Q7 of 15
SciPy - Integration with Scientific Ecosystem
What is wrong with this pipeline code snippet?
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
import numpy as np

def add_one(x):
    return x + 1

pipe = Pipeline([
    ('add', FunctionTransformer(add_one, validate=False))
])

X = [1, 2, 3]
pipe.transform(X)
APipeline cannot have only one step
BFunctionTransformer requires validate=true for lists
CInput X should be a numpy array, not a list
Dadd_one function must be vectorized
Step-by-Step Solution
Solution:
  1. Step 1: Check input type

    FunctionTransformer expects numpy arrays by default; list input may cause errors.
  2. Step 2: Validate input

    Passing list without conversion can cause unexpected behavior or errors.
  3. Final Answer:

    Input X should be a numpy array, not a list -> Option C
  4. Quick Check:

    Use numpy arrays for transformations [OK]
Quick Trick: Always use numpy arrays with scikit-learn transformers [OK]
Common Mistakes:
  • Passing lists directly to transformers
  • Misunderstanding validate parameter
  • Thinking single-step pipelines are invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes