Data Science with Python Certification Data Science with Python Feature Engineering Techniques 5 — Questions and Answers
Question 1: What is the effect of applying np.log1p() instead of np.log() when log-transforming a feature that may contain zeros?
- It clips negative values to zero before transforming
- It computes log(1 + x), avoiding undefined results for zero values (Correct answer)
- It applies base-10 logarithm instead of natural log
- It squares values before applying the logarithm
Correct answer: It computes log(1 + x), avoiding undefined results for zero values
np.log1p(x) computes the natural logarithm of (1 + x), which is defined at x=0 and numerically stable for small positive values.
Question 2: In a machine learning pipeline, what is the role of a 'custom transformer' built by subclassing BaseEstimator and TransformerMixin?
- To define a new loss function for gradient boosting
- To create a reusable, pipeline-compatible feature transformation step (Correct answer)
- To implement a new cross-validation splitter
- To define custom evaluation metrics for model selection
Correct answer: To create a reusable, pipeline-compatible feature transformation step
Subclassing BaseEstimator and TransformerMixin lets you implement fit() and transform() methods that integrate seamlessly into sklearn Pipelines and GridSearchCV.
Question 3: What is 'entity embedding' in the context of feature engineering for categorical variables?
- Replacing categories with their frequency of occurrence
- Learning dense vector representations for categories via a neural network (Correct answer)
- Hashing categories to fixed-length bit strings
- Mapping categories to their mean target value
Correct answer: Learning dense vector representations for categories via a neural network
Entity embeddings use a neural network's embedding layer to learn low-dimensional dense representations for categorical variables, capturing similarity structure.
Question 4: Which strategy helps mitigate the 'curse of dimensionality' after generating many polynomial interaction features?
- Increasing the polynomial degree further
- Adding more training samples only
- Applying regularization or feature selection to prune irrelevant features (Correct answer)
- Converting polynomial features back to categorical
Correct answer: Applying regularization or feature selection to prune irrelevant features
After polynomial expansion, regularization (L1/L2) or feature selection methods discard low-signal features, counteracting the sparsity and overfitting risk from high dimensionality.
Question 5: When computing distance-based features (e.g., Euclidean distance between a point and a centroid), what preprocessing step is essential before computing distances?
- One-hot encoding all features
- Applying SMOTE oversampling
- Scaling numerical features to a common range (Correct answer)
- Binarizing all continuous features
Correct answer: Scaling numerical features to a common range
Distance metrics are sensitive to feature scale; a feature with range 0–10000 dominates one with range 0–1, so standardization or min-max scaling must be applied first.
Question 6: What does sklearn's VarianceThreshold feature selector remove from a dataset?
- Features correlated above a specified threshold
- Features with variance below a specified threshold (Correct answer)
- Features with more than a specified fraction of missing values
- Features with absolute mean above a specified threshold
Correct answer: Features with variance below a specified threshold
VarianceThreshold removes all features whose variance across samples does not meet the minimum threshold, effectively eliminating near-constant features.
Question 7: Which technique decomposes a datetime feature into multiple cyclical features using sine and cosine transformations?
- Ordinal datetime encoding
- Cyclical encoding (Correct answer)
- Fourier feature extraction
- Datetime binning
Correct answer: Cyclical encoding
Cyclical encoding maps periodic datetime components (hour, month, day-of-week) to sin/cos pairs so that the distance between end and beginning of a cycle is small.
What is the effect of applying np.log1p() instead of np.log() when log-transforming a feature that may contain zeros?