Data Science with Python Certification Data Science with Python Feature Engineering Techniques 2 — Questions and Answers
Question 1: Which technique creates new features by computing the interaction between two existing numerical features?
- Feature hashing
- Polynomial feature expansion (Correct answer)
- Target encoding
- Variance thresholding
Correct answer: Polynomial feature expansion
Polynomial feature expansion generates interaction terms and higher-degree powers of existing numerical features using sklearn's PolynomialFeatures.
Question 2: What is the primary risk of applying target encoding before performing cross-validation?
- Increased training time
- Data leakage from the target variable (Correct answer)
- Loss of categorical information
- Multicollinearity in features
Correct answer: Data leakage from the target variable
Applying target encoding before cross-validation leaks target information into the training folds, causing overly optimistic model evaluation.
Question 3: When using pandas to extract the day-of-week from a datetime column, which method returns an integer (0=Monday, 6=Sunday)?
- dt.weekday (Correct answer)
- dt.day
- dt.isoweekday
- dt.dayofyear
Correct answer: dt.weekday
The dt.weekday accessor returns an integer from 0 (Monday) to 6 (Sunday) for each datetime value in a pandas Series.
Question 4: Which binning strategy creates intervals where each bin contains approximately the same number of observations?
- Equal-width binning
- Quantile-based binning (Correct answer)
- Logarithmic binning
- K-means binning
Correct answer: Quantile-based binning
Quantile-based binning (e.g., pd.qcut) divides data so each bin holds roughly equal numbers of samples, handling skewed distributions well.
Question 5: What does the 'handle_unknown' parameter control in sklearn's OneHotEncoder?
- How NaN values are imputed before encoding
- Behavior when an unseen category appears at transform time (Correct answer)
- Whether to drop the first category to avoid multicollinearity
- The maximum number of unique categories to encode
Correct answer: Behavior when an unseen category appears at transform time
handle_unknown='ignore' causes OneHotEncoder to output all-zero rows for unseen categories at transform time, preventing errors on new data.
Question 6: Which feature selection method ranks features by computing a statistical test between each feature and the target, independently of any model?
- Recursive Feature Elimination
- Univariate feature selection (Correct answer)
- L1 regularization
- Permutation importance
Correct answer: Univariate feature selection
Univariate feature selection (e.g., sklearn's SelectKBest) scores each feature individually using a statistical test such as f_classif or chi2.
Question 7: What is the purpose of applying a Box-Cox transformation to a numerical feature?
- Convert a continuous feature into discrete bins
- Reduce the number of unique values in the feature
- Stabilize variance and make the distribution more Gaussian (Correct answer)
- Encode ordinal relationships between categories
Correct answer: Stabilize variance and make the distribution more Gaussian
The Box-Cox transformation applies a power transformation to make a feature's distribution closer to normal and stabilize its variance across the range.
Which technique creates new features by computing the interaction between two existing numerical features?