2. Which of the following is the correct way to create a simple pipeline in scikit-learn?
easy
A. Pipeline('scale', StandardScaler(), 'model', LogisticRegression())
B. Pipeline({'scale': StandardScaler(), 'model': LogisticRegression()})
C. Pipeline([('scale', StandardScaler()), ('model', LogisticRegression())])
D. Pipeline(scale=StandardScaler(), model=LogisticRegression())
Solution
Step 1: Recall scikit-learn pipeline syntax
It requires a list of tuples with step name and transformer/model.
Step 2: Match syntax to options
Only Pipeline([('scale', StandardScaler()), ('model', LogisticRegression())]) uses a list of tuples correctly.
Final Answer:
Pipeline([('scale', StandardScaler()), ('model', LogisticRegression())]) -> Option C
Quick Check:
Pipeline syntax = list of tuples [OK]
Hint: Use list of (name, step) tuples for pipelines [OK]
Common Mistakes:
Using dictionary instead of list of tuples
Passing keyword arguments instead of list
Passing separate arguments without list
3. Given the code below, what will be the output of print(pipe.named_steps['model'].coef_) after fitting?
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = Pipeline([
('scale', StandardScaler()),
('model', LogisticRegression())
])
X = [[1, 2], [2, 3], [3, 4], [4, 5]]
y = [0, 0, 1, 1]
pipe.fit(X, y)
print(pipe.named_steps['model'].coef_)
medium
A. A 2D array with coefficients for each feature
B. An error because 'coef_' is not available
C. A list of predicted labels
D. A scalar value representing accuracy
Solution
Step 1: Understand pipeline fitting
Pipeline fits scaler then logistic regression on data.
Step 2: Access model coefficients
After fitting, LogisticRegression has attribute 'coef_' which is a 2D array of feature weights.
Final Answer:
A 2D array with coefficients for each feature -> Option A
Quick Check:
Model coef_ = 2D array [OK]
Hint: Model coef_ holds feature weights after fit [OK]
Common Mistakes:
Expecting coef_ before fitting
Confusing coef_ with predictions
Trying to access coef_ on pipeline instead of model
4. What is wrong with this pipeline code snippet?
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = Pipeline([
('scale', StandardScaler()),
('model', LogisticRegression())
])
pipe.fit(X, y)
pipe.predict(X_test)
Assuming X, y, and X_test are defined correctly.
medium
A. The pipeline is missing a call to transform before predict
B. The pipeline steps are not in a list
C. The pipeline is missing a final estimator
D. Nothing is wrong; code runs fine
Solution
Step 1: Check pipeline construction
Pipeline steps are correctly given as a list of tuples with scaler and model.
Step 2: Verify usage of fit and predict
Calling fit and then predict on pipeline is correct; pipeline applies scaler then model automatically.
Final Answer:
Nothing is wrong; code runs fine -> Option D
Quick Check:
Pipeline fit/predict usage = correct [OK]
Hint: Pipeline handles transform internally during predict [OK]
Common Mistakes:
Thinking transform must be called separately
Passing steps as dict instead of list
Missing final estimator in pipeline
5. You want to build a pipeline that scales data, selects the top 3 features, and then fits a logistic regression model. Which pipeline setup is best practice?
hard
A. Pipeline([('model', LogisticRegression()), ('scale', StandardScaler()), ('select', SelectKBest(k=3))])
B. Pipeline([('scale', StandardScaler()), ('select', SelectKBest(k=3)), ('model', LogisticRegression())])
C. Pipeline([('select', SelectKBest(k=3)), ('scale', StandardScaler()), ('model', LogisticRegression())])
D. Pipeline([('scale', StandardScaler()), ('model', LogisticRegression()), ('select', SelectKBest(k=3))])
Solution
Step 1: Determine correct order of steps
Scaling should happen before feature selection to normalize data for selection.
Step 2: Place model last in pipeline
The model must be the final step to fit on selected features.
Final Answer:
Pipeline([('scale', StandardScaler()), ('select', SelectKBest(k=3)), ('model', LogisticRegression())]) -> Option B
Quick Check:
Order: scale -> select -> model [OK]
Hint: Scale first, then select features, then model [OK]
Common Mistakes:
Selecting features before scaling
Putting model before preprocessing steps
Mixing order of pipeline steps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.