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📝 Application Q15 of 15
SciPy - Integration with Scientific Ecosystem
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?
Afrom 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())
])
Bfrom 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())
])
Cfrom 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())
])
Dfrom 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)
])
Step-by-Step Solution
Solution:
Step 1: Use FunctionTransformer correctly with SciPy function
Pass the function stats.zscore without calling it, wrapped by FunctionTransformer.
Step 2: Ensure LogisticRegression is instantiated
Use LogisticRegression() with parentheses to create the model instance.
Final Answer:
Code snippet with FunctionTransformer(stats.zscore) and LogisticRegression() -> Option A
Quick Check:
FunctionTransformer(function) + model instance [OK]
Quick Trick:Wrap function, instantiate model with parentheses [OK]
Common Mistakes:
Calling SciPy function instead of passing it
Not instantiating LogisticRegression
Passing function call to FunctionTransformer
Master "Integration with Scientific Ecosystem" in SciPy
9 interactive learning modes - each teaches the same concept differently