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
Feature Engineering Pipelines
📖 Scenario: You are working on a machine learning project. You need to prepare your data by creating a feature engineering pipeline. This pipeline will help you clean and transform your data automatically before training your model.
🎯 Goal: Build a simple feature engineering pipeline using scikit-learn that scales numeric features and encodes categorical features.
📋 What You'll Learn
Create a dataset dictionary with numeric and categorical features
Define a configuration variable for numeric feature names
Build a pipeline that scales numeric features and encodes categorical features
Print the transformed feature array
💡 Why This Matters
🌍 Real World
Feature engineering pipelines automate data preparation steps, making machine learning workflows faster and less error-prone.
💼 Career
Understanding how to build and use feature engineering pipelines is essential for MLOps engineers and data scientists to deploy reliable ML models.
Progress0 / 4 steps
1
Create the initial dataset dictionary
Create a dictionary called data with these exact entries: 'age': [25, 32, 47], 'salary': [50000, 60000, 80000], and 'department': ['sales', 'engineering', 'marketing'].
MLOps
Hint
Use a dictionary with keys 'age', 'salary', and 'department' and assign lists of values exactly as shown.
2
Define numeric feature names
Create a list called numeric_features containing the strings 'age' and 'salary'.
MLOps
Hint
Define a list named numeric_features with the exact two strings.
3
Build the feature engineering pipeline
Import ColumnTransformer, StandardScaler, and OneHotEncoder from sklearn. Create a ColumnTransformer called preprocessor that applies StandardScaler() to numeric_features and OneHotEncoder() to the 'department' feature.
MLOps
Hint
Use ColumnTransformer with two transformers: one for numeric features using StandardScaler() and one for categorical feature 'department' using OneHotEncoder().
4
Transform and print the processed features
Convert data to a pandas DataFrame called df. Use preprocessor.fit_transform(df) to transform the data and assign it to features. Print features.
MLOps
Hint
Use pandas.DataFrame to convert data. Then call preprocessor.fit_transform(df) and print the result.
Practice
(1/5)
1. What is the main purpose of a feature engineering pipeline in MLOps?
easy
A. To automate and standardize data preparation steps
B. To deploy machine learning models to production
C. To monitor model performance after deployment
D. To collect raw data from external sources
Solution
Step 1: Understand the role of feature engineering pipelines
Feature engineering pipelines automate the process of transforming raw data into features for model training and testing.
Step 2: Differentiate from other MLOps tasks
Deploying models, monitoring, and data collection are separate tasks from feature engineering pipelines.
Final Answer:
To automate and standardize data preparation steps -> Option A
Quick Check:
Feature engineering pipeline = automate data prep [OK]
Hint: Feature pipelines automate data prep, not deployment or monitoring [OK]
Common Mistakes:
Confusing feature pipelines with model deployment
Thinking pipelines collect raw data
Mixing up monitoring with feature engineering
2. Which of the following is the correct way to define a simple feature engineering pipeline step using scikit-learn's Pipeline?
easy
A. pipeline = Pipeline([('scaler', StandardScaler()), ('pca', PCA(n_components=2))])
B. pipeline = Pipeline('scaler', StandardScaler(), 'pca', PCA(n_components=2))
C. pipeline = Pipeline({'scaler': StandardScaler(), 'pca': PCA(n_components=2)})
D. pipeline = Pipeline(StandardScaler(), PCA(n_components=2))
Solution
Step 1: Recall scikit-learn Pipeline syntax
Pipeline expects a list of tuples, each tuple with a name and a transformer object.
Step 2: Check each option's syntax
pipeline = Pipeline([('scaler', StandardScaler()), ('pca', PCA(n_components=2))]) correctly uses a list of tuples. Options B, C, and D use incorrect argument formats.
Final Answer:
pipeline = Pipeline([('scaler', StandardScaler()), ('pca', PCA(n_components=2))]) -> Option A
Quick Check:
Pipeline needs list of (name, transformer) tuples [OK]
Hint: Pipeline needs list of (name, transformer) tuples [OK]
Common Mistakes:
Passing arguments without list brackets
Using dict instead of list of tuples
Omitting step names in pipeline
3. Given the following pipeline code, what will be the output of pipeline.transform([[0, 0], [1, 1]])?
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
pipeline = Pipeline([
('scaler', StandardScaler()),
('pca', PCA(n_components=1))
])
pipeline.fit([[0, 0], [1, 1]])
result = pipeline.transform([[0, 0], [1, 1]])
print(result)
medium
A. Error: PCA requires more than one sample
B. [[0. 0.]
[1. 1.]]
C. [[0.5]
[0.5]]
D. [[-1.41421356]
[ 1.41421356]]
Solution
Step 1: Understand pipeline steps
First, data is scaled to zero mean and unit variance, then PCA reduces to 1 component.
Step 2: Calculate transformed output
Scaling [[0,0],[1,1]] centers data, PCA finds principal component; output is approximately [[-1.41421356],[1.41421356]].
The error says input is 1D but 2D is expected for fit method.
Step 2: Check input format
Input [1, 2, 3, 4] is a 1D list; fit expects 2D array like [[1], [2], [3], [4]].
Final Answer:
Input to fit should be 2D array, not 1D list -> Option B
Quick Check:
fit input shape must be 2D [OK]
Hint: fit() needs 2D array shape, not flat list [OK]
Common Mistakes:
Passing 1D list instead of 2D array
Misunderstanding PCA parameter limits
Thinking StandardScaler restricts input types
5. You want to create a feature engineering pipeline that handles missing values by filling them with the median, then scales features, and finally selects the top 3 features using a model-based selector. Which pipeline setup is correct?
hard
A. Pipeline([('scaler', StandardScaler()), ('imputer', SimpleImputer(strategy='median')), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3))])
B. Pipeline([('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3)), ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler())])
C. Pipeline([('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3))])
D. Pipeline([('imputer', SimpleImputer(strategy='mean')), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3)), ('scaler', StandardScaler())])
Solution
Step 1: Order pipeline steps logically
Missing values must be handled first, then scaling, then feature selection.
Step 2: Check each option's correctness
Pipeline([('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3))]) follows correct order and uses median for imputation. Pipeline([('scaler', StandardScaler()), ('imputer', SimpleImputer(strategy='median')), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3))]) swaps imputer and scaler incorrectly. Pipeline([('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3)), ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler())]) starts with selector which needs complete data. Pipeline([('imputer', SimpleImputer(strategy='mean')), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3)), ('scaler', StandardScaler())]) uses mean instead of median and wrong order.
Final Answer:
Pipeline([('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()), ('selector', SelectFromModel(estimator=RandomForestClassifier(), max_features=3))]) -> Option C
Quick Check:
Impute -> scale -> select features [OK]
Hint: Impute missing -> scale -> select features in pipeline order [OK]
Common Mistakes:
Placing scaler before imputer
Selecting features before imputing missing values
Using mean instead of median when median is required