Bird
Raised Fist0
SciPydata~20 mins

SciPy with scikit-learn pipeline - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
SciPy Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple pipeline with StandardScaler and LogisticRegression
What is the output of the following code snippet that creates a pipeline with a scaler and logistic regression, then fits and predicts on a test sample?
SciPy
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import numpy as np

X_train = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
y_train = np.array([0, 0, 1, 1])

pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('logreg', LogisticRegression(random_state=0))
])

pipeline.fit(X_train, y_train)

X_test = np.array([[1.5, 2.5]])
prediction = pipeline.predict(X_test)
print(prediction)
A[1]
B[1 0]
C[0 1]
D[0]
Attempts:
2 left
💡 Hint
Think about how StandardScaler transforms the input and how logistic regression predicts based on training labels.
data_output
intermediate
2:00remaining
Shape of transformed data after applying PCA in a pipeline
Given the following pipeline that applies PCA to reduce dimensionality, what is the shape of the transformed data after calling transform on X_test?
SciPy
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
import numpy as np

X_train = np.random.rand(10, 5)
X_test = np.random.rand(3, 5)

pipeline = Pipeline([
    ('pca', PCA(n_components=2))
])

pipeline.fit(X_train)
X_transformed = pipeline.transform(X_test)
print(X_transformed.shape)
A(3, 5)
B(3, 2)
C(10, 2)
D(10, 5)
Attempts:
2 left
💡 Hint
PCA reduces the number of features to n_components but keeps the number of samples the same.
🔧 Debug
advanced
2:00remaining
Identify the error in pipeline usage with SciPy function
What error will this code raise when trying to use a SciPy function inside a scikit-learn pipeline step?
SciPy
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
from scipy.special import expit
import numpy as np

X = np.array([[0, 1], [2, 3]])

pipeline = Pipeline([
    ('sigmoid', FunctionTransformer(expit)),
])

pipeline.fit(X)
output = pipeline.transform(X)
print(output)
ANo error, outputs transformed array
BAttributeError: 'FunctionTransformer' object has no attribute 'fit'
CTypeError: 'FunctionTransformer' object is not callable
DValueError: Input contains NaN
Attempts:
2 left
💡 Hint
FunctionTransformer wraps a function to apply it during transform. Check if expit works element-wise.
🚀 Application
advanced
2:00remaining
Using a custom SciPy statistical function in a pipeline step
You want to add a pipeline step that replaces each feature with its z-score using SciPy's zscore function. Which pipeline step code correctly applies this transformation?
A('zscore', StandardScaler())
B('zscore', FunctionTransformer(scipy.stats.zscore))
C('zscore', FunctionTransformer(lambda x: scipy.stats.zscore(x, axis=0)))
D('zscore', FunctionTransformer(lambda x: scipy.stats.zscore(x, axis=1)))
Attempts:
2 left
💡 Hint
zscore needs axis=0 to standardize features column-wise.
🧠 Conceptual
expert
2:00remaining
Why integrate SciPy functions in scikit-learn pipelines?
What is the main advantage of integrating SciPy functions inside scikit-learn pipelines using FunctionTransformer?
AIt allows seamless combination of SciPy transformations with scikit-learn estimators for consistent preprocessing and model fitting.
BIt automatically converts SciPy functions into scikit-learn estimators with fit and predict methods.
CIt speeds up SciPy functions by compiling them into C code within the pipeline.
DIt enables SciPy functions to handle missing data automatically during pipeline execution.
Attempts:
2 left
💡 Hint
Think about how pipelines help organize multiple steps in machine learning workflows.

Practice

(1/5)
1. What is the main benefit of using a Pipeline in scikit-learn when combined with SciPy functions?
easy
A. It organizes data processing and modeling steps into one repeatable workflow.
B. It automatically improves model accuracy without tuning.
C. It replaces the need for any data cleaning.
D. It allows running code without importing any libraries.

Solution

  1. Step 1: Understand the purpose of Pipeline

    A Pipeline in scikit-learn is designed to chain multiple steps like data transformation and modeling into a single object.
  2. Step 2: Recognize the benefit of combining SciPy functions

    Using SciPy functions inside a Pipeline via FunctionTransformer keeps the workflow organized and repeatable.
  3. Final Answer:

    It organizes data processing and modeling steps into one repeatable workflow. -> Option A
  4. Quick Check:

    Pipeline = Organized workflow [OK]
Hint: Pipelines bundle steps for easy reuse and clarity [OK]
Common Mistakes:
  • Thinking Pipeline improves accuracy automatically
  • Assuming Pipeline removes need for data cleaning
  • Believing Pipeline runs without imports
2. Which of the following is the correct way to include a SciPy function scipy_func inside a scikit-learn pipeline using FunctionTransformer?
easy
A. Pipeline([('transform', scipy_func), ('model', LogisticRegression())])
B. Pipeline([('transform', FunctionTransformer(scipy_func)), ('model', LogisticRegression())])
C. Pipeline([('transform', FunctionTransformer()), ('model', LogisticRegression())])
D. Pipeline([('transform', FunctionTransformer(scipy_func())), ('model', LogisticRegression())])

Solution

  1. Step 1: Understand FunctionTransformer usage

    FunctionTransformer takes a function as an argument without calling it (no parentheses).
  2. Step 2: Identify correct pipeline syntax

    The pipeline step should be ('transform', FunctionTransformer(scipy_func)) to wrap the function properly.
  3. Final Answer:

    Pipeline([('transform', FunctionTransformer(scipy_func)), ('model', LogisticRegression())]) -> Option B
  4. Quick Check:

    FunctionTransformer(function) no parentheses [OK]
Hint: Pass function name, not call, to FunctionTransformer [OK]
Common Mistakes:
  • Calling the function inside FunctionTransformer
  • Passing function directly without FunctionTransformer
  • Using FunctionTransformer without function argument
3. What will be the output of the following 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)),
])

X = np.array([1, 2, 3])
result = pipe.transform(X)
print(result)
medium
A. Error: Pipeline has no transform method
B. [1 2 3]
C. [0 1 2]
D. [2 3 4]

Solution

  1. Step 1: Understand FunctionTransformer behavior

    FunctionTransformer applies the function add_one to input data during transform.
  2. Step 2: Apply the function to input array

    Input array [1, 2, 3] plus 1 becomes [2, 3, 4].
  3. Final Answer:

    [2 3 4] -> Option D
  4. Quick Check:

    Input + 1 = Output [OK]
Hint: FunctionTransformer applies function on transform call [OK]
Common Mistakes:
  • Assuming pipeline has no transform method
  • Forgetting function adds 1
  • Confusing fit and transform methods
4. Identify the error in this pipeline code using a SciPy function inside FunctionTransformer:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
import numpy as np

def multiply_by_two(X):
    return X * 2

pipe = Pipeline([
    ('mult', FunctionTransformer(multiply_by_two())),
])

X = np.array([1, 2, 3])
result = pipe.transform(X)
print(result)
medium
A. Using transform instead of fit_transform
B. Missing import for numpy
C. Calling multiply_by_two() instead of passing the function
D. Pipeline missing a model step

Solution

  1. Step 1: Check FunctionTransformer argument

    FunctionTransformer expects a function, not the result of a function call.
  2. Step 2: Identify the error in code

    Code calls multiply_by_two() immediately, which causes an error because it returns an array, not a function.
  3. Final Answer:

    Calling multiply_by_two() instead of passing the function -> Option C
  4. Quick Check:

    Pass function, don't call it [OK]
Hint: Pass function name, avoid parentheses in FunctionTransformer [OK]
Common Mistakes:
  • Calling function instead of passing it
  • Assuming pipeline needs a model step
  • Confusing transform with fit_transform
5. You want to build a pipeline that first applies a SciPy function to normalize data, then fits a logistic regression model. Which of the following code snippets correctly implements this?
hard
A. from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.linear_model import LogisticRegression import scipy.stats as stats pipe = Pipeline([ ('normalize', FunctionTransformer(stats.zscore)), ('model', LogisticRegression()) ])
B. from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.linear_model import LogisticRegression import scipy.stats as stats pipe = Pipeline([ ('normalize', stats.zscore()), ('model', LogisticRegression()) ])
C. from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.linear_model import LogisticRegression import scipy.stats as stats pipe = Pipeline([ ('normalize', FunctionTransformer(stats.zscore())), ('model', LogisticRegression()) ])
D. from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.linear_model import LogisticRegression import scipy.stats as stats pipe = Pipeline([ ('normalize', FunctionTransformer(stats.zscore)), ('model', LogisticRegression) ])

Solution

  1. Step 1: Use FunctionTransformer correctly with SciPy function

    Pass the function stats.zscore without calling it, wrapped by FunctionTransformer.
  2. Step 2: Ensure LogisticRegression is instantiated

    Use LogisticRegression() with parentheses to create the model instance.
  3. Final Answer:

    Code snippet with FunctionTransformer(stats.zscore) and LogisticRegression() -> Option A
  4. Quick Check:

    FunctionTransformer(function) + model instance [OK]
Hint: Wrap function, instantiate model with parentheses [OK]
Common Mistakes:
  • Calling SciPy function instead of passing it
  • Not instantiating LogisticRegression
  • Passing function call to FunctionTransformer