Data Science with Python Feature Engineering Techniques Questions and Answers — Questions and Answers
Question 1: A data scientist is preparing a dataset for a K-Nearest Neighbors (KNN) model. The dataset contains an 'age' feature (range 20-70) and an 'income' feature (range 30,000-250,000). Since KNN is a distance-based algorithm, what is the most appropriate feature scaling technique to apply and why?
- One-Hot Encoding, because it converts features into a binary format that is easier to process.
- Min-Max Scaling, because it preserves the original distribution of the data without distortion.
- Standardization (Z-score scaling), because it is less sensitive to outliers than Min-Max scaling and handles features on vastly different scales effectively. (Correct answer)
- Log Transformation, because it reduces the right-skewness often found in income data.
Correct answer: Standardization (Z-score scaling), because it is less sensitive to outliers than Min-Max scaling and handles features on vastly different scales effectively.
Standardization (Z-score scaling) rescales features to have a mean of 0 and a standard deviation of 1. This is crucial for distance-based algorithms like KNN, where features with larger scales (like 'income') can dominate the distance calculation. Standardization is generally more robust to outliers than Min-Max scaling, which scales data to a fixed range (e.g., 0 to 1) and can be skewed by extreme values. [2, 7, 24]
Question 2: You are working with a categorical feature 'product_category' which contains the values 'Electronics', 'Apparel', 'Home Goods', and 'Books'. There is no inherent order or ranking among these categories. Which encoding technique should be used to prepare this feature for a linear regression model to prevent the model from assuming a false ordinal relationship?
- Label Encoding, as it is computationally efficient and assigns a unique integer to each category.
- One-Hot Encoding, as it creates separate binary columns for each category, avoiding any implied ranking. (Correct answer)
- Binary Encoding, as it is more memory-efficient than One-Hot Encoding for high-cardinality features.
- Frequency Encoding, as it replaces categories with their count in the dataset.
Correct answer: One-Hot Encoding, as it creates separate binary columns for each category, avoiding any implied ranking.
One-Hot Encoding is the correct choice for nominal categorical data (where no order exists) when used with linear models. It creates a new binary (0 or 1) feature for each category, preventing the model from incorrectly interpreting the categories as having a quantitative relationship (e.g., that 'Books' (encoded as 3) is greater than 'Apparel' (encoded as 1)). [19, 25, 26]
Question 3: A dataset contains a 'last_login_date' column with a `datetime64[ns]` dtype. Which of the following feature engineering approaches is most effective for extracting cyclical patterns that could be useful for a predictive model?
- Converting the entire 'last_login_date' column into a single integer representing the Unix timestamp.
- Applying a log transformation to the date column to normalize its distribution.
- Dropping the column, as datetime objects cannot be used directly in most machine learning models.
- Creating new numerical features such as 'day_of_week', 'month_of_year', and a binary 'is_weekend' flag. (Correct answer)
Correct answer: Creating new numerical features such as 'day_of_week', 'month_of_year', and a binary 'is_weekend' flag.
Extracting components like the day of the week, month, or creating a flag for weekends allows a model to capture time-based patterns and seasonality (e.g., user activity might be higher on weekends or at the beginning of the month). This is a standard and highly effective technique for making datetime information useful to a model. [15, 18, 29]
Question 4: A feature representing 'customer_spending' in a dataset is heavily right-skewed, with most values being low but with a long tail of very high-spending customers. Many linear machine learning models perform better with normally distributed features. What is a common and effective transformation to apply to this feature to make its distribution more symmetric?
- Applying a logarithmic transformation (e.g., `np.log1p`) to compress the higher values and expand the lower values. (Correct answer)
- Applying Standardization, which will center the data around a mean of 0.
- Using binning to group the spending values into discrete categories like 'low', 'medium', and 'high'.
- Using Min-Max scaling to scale all values to a fixed range between 0 and 1.
Correct answer: Applying a logarithmic transformation (e.g., `np.log1p`) to compress the higher values and expand the lower values.
A logarithmic transformation is a powerful and common method for handling right-skewed data. It compresses the range of large values more than it compresses the range of small values, which effectively pulls the long tail in towards the center of the distribution, making it more symmetric and closer to a normal distribution. [1, 6, 14, 22]
Question 5: What is a primary advantage of using binning (or discretization) to transform a continuous numerical feature, such as 'Age', into categorical bins (e.g., '18-25', '26-40', '41-60')?
- It guarantees that the resulting feature will have a normal distribution.
- It significantly reduces the memory footprint of the dataset by storing strings instead of integers.
- It can help a linear model capture non-linear relationships between the feature and the target variable. (Correct answer)
- It is the only way to handle missing values within a continuous feature.
Correct answer: It can help a linear model capture non-linear relationships between the feature and the target variable.
By converting a continuous feature into discrete bins, a model (especially a linear one) can learn a separate weight for each bin. This allows it to capture complex, non-linear patterns where the effect of the feature on the target variable changes across different ranges (e.g., the likelihood of purchasing a product might be high for the '18-25' age group, low for '26-40', and high again for '41-60'). [5, 8, 11, 16]
Question 6: In a dataset for predicting house prices, you have features for 'num_bedrooms' and 'num_bathrooms'. Your initial model is underperforming, and you suspect the combined effect of these two features is more important than their individual effects. Which feature engineering technique would best capture this combined effect?
- Applying standardization to both features to put them on the same scale.
- Creating polynomial features for each variable independently (e.g., bedrooms squared).
- Binning each feature into categories like 'low', 'medium', and 'high'.
- Creating an interaction feature, for example, by multiplying 'num_bedrooms' by 'num_bathrooms'. (Correct answer)
Correct answer: Creating an interaction feature, for example, by multiplying 'num_bedrooms' by 'num_bathrooms'.
Creating an interaction feature by multiplying 'num_bedrooms' and 'num_bathrooms' allows the model to learn the combined effect of these variables. This new feature can capture relationships that the individual features alone cannot, such as a house with many bedrooms AND many bathrooms being disproportionately more valuable. [3, 4, 17]
A data scientist is preparing a dataset for a K-Nearest Neighbors (KNN) model.
The dataset contains an 'age' feature (range 20-70) and an 'income' feature (range 30,000-250,000).
Since KNN is a distance-based algorithm, what is the most appropriate feature scaling technique to apply and why?