Data Science with Python Certification Data Science with Python Feature Engineering Techniques 4 — Questions and Answers
Question 1: What is the correct way to apply sklearn's StandardScaler inside a cross-validation loop to avoid data leakage?
- Fit the scaler on all data before splitting folds
- Include the scaler in a Pipeline and pass the pipeline to cross_validate (Correct answer)
- Fit and transform both train and test folds together
- Fit the scaler on test data and apply it to train data
Correct answer: Include the scaler in a Pipeline and pass the pipeline to cross_validate
Wrapping the scaler in a Pipeline ensures it is fit only on the training fold and then applied to the test fold during each cross-validation iteration.
Question 2: Which technique encodes high-cardinality categorical features into a fixed-length binary vector using a hash function?
- Ordinal encoding
- Feature hashing (hashing trick) (Correct answer)
- Binary encoding
- Frequency encoding
Correct answer: Feature hashing (hashing trick)
Feature hashing maps categories to indices via a hash function, producing a fixed-size sparse vector regardless of the number of unique categories.
Question 3: In feature engineering, what is 'weight of evidence' (WoE) encoding primarily used for?
- Encoding features for clustering algorithms
- Encoding categorical variables for binary classification (Correct answer)
- Converting continuous features into ranked bins
- Scaling numerical features to unit variance
Correct answer: Encoding categorical variables for binary classification
WoE encoding replaces categories with the log ratio of the proportion of events to non-events for that category, making it well-suited for logistic regression in binary classification.
Question 4: What issue arises when you apply PCA for dimensionality reduction before splitting data into train and test sets?
- PCA cannot handle numerical features
- PCA components depend on the test set, causing data leakage (Correct answer)
- PCA increases the number of features
- PCA requires target labels to compute components
Correct answer: PCA components depend on the test set, causing data leakage
Fitting PCA on the full dataset before splitting leaks test-set variance information into the transformation, invalidating the holdout evaluation.
Question 5: Which pandas function is most efficient for creating multiple aggregated features from a GroupBy object in a single pass?
- GroupBy.apply()
- GroupBy.agg() (Correct answer)
- GroupBy.transform()
- GroupBy.filter()
Correct answer: GroupBy.agg()
GroupBy.agg() accepts a dictionary or list of aggregation functions and computes all specified statistics in one pass, returning a summarized DataFrame.
Question 6: What does the 'sparse_output' parameter of OneHotEncoder control?
- Whether to compress rare categories into an 'other' bin
- Whether the output matrix is stored as a sparse or dense array (Correct answer)
- The maximum fraction of zeros allowed per column
- Whether to apply L2 normalization to each row
Correct answer: Whether the output matrix is stored as a sparse or dense array
Setting sparse_output=False (or sparse=False in older versions) returns a dense numpy array instead of a scipy sparse matrix.
Question 7: Which feature engineering approach is most appropriate when a categorical feature's level order matters (e.g., 'low', 'medium', 'high')?
- One-hot encoding
- Target encoding
- Ordinal encoding (Correct answer)
- Binary encoding
Correct answer: Ordinal encoding
Ordinal encoding assigns increasing integers to reflect the natural order of categories, preserving rank information that one-hot encoding discards.
What is the correct way to apply sklearn's StandardScaler inside a cross-validation loop to avoid data leakage?