Feature Engineering
Preprocessing transforms raw data into numerical matrices optimized for training.
Handling Missing Data and Outliers
Real-world datasets are rarely perfect. Data may be missing due to sensor failures, non-responses, or unrecorded events.
- Imputation: Replaces missing values using training statistics (e.g., mean, median, mode) or algorithms (e.g., kNN imputation).
- Removal: Dropping rows or irrelevant columns with too many missing values.
Handling Outliers
Outliers are extreme values that can significantly skew statistical measures and models like Linear Regression. Depending on the context, we can:
- Keep: If the outliers represent genuine, important anomalies (e.g., fraud detection).
- Delete: If the outliers are clearly errors or irrelevant.
- Impute: Treat outliers as missing data and impute a central value.
- Transform: Apply mathematical transformations to reduce their impact.
Boxplots are visual tools commonly used to identify outliers. They display the interquartile range (IQR) of data, with points falling outside typically flagged as outliers.
Skewed Data and Transformations
When data is highly skewed (e.g., income distribution with a long right tail), it can negatively impact model performance. We apply transformations to make the distribution more Gaussian-like:
- Logarithmic Transformation: (useful for right-skewed data).
- Square Root Transformation: .
- Box-Cox Transformation: A parameterized family of power transformations.
Handling String and Text Data
Models require numerical inputs. Categorical and textual data must be encoded:
- Ordinal Encoding: Maps categories to ordered integers (e.g., “Low”, “Medium”, “High” to 1, 2, 3).
- One-Hot Encoding: Creates separate binary columns for each category, preventing the model from assuming a false ordinal relationship between nominal categories (e.g., colors).
- Bag of Words (BoW): For free-text data, BoW creates a vocabulary of all unique words and represents each document as a vector indicating the frequency of each word.
Feature Scaling
Disparate scales bias distance-based models (KNN, SVM). We scale features via:
- Min-Max Scaling: maps to : .
- Standardization: centers by mean and variance: .
Feature Selection and Correlation
Linear relationships are analyzed using a Correlation Matrix of Pearson coefficients in the range . Below is an example mapping features from a real estate dataset:
| House Size | Num Bedrooms | Distance to Center | |
|---|---|---|---|
| House Size | 1.00 | 0.85 | -0.60 |
| Num Bedrooms | 0.85 | 1.00 | -0.45 |
| Distance to Center | -0.60 | -0.45 | 1.00 |
The diagonal represents self-correlation. The coefficient indicates a strong positive relationship between size and bedrooms, while shows a negative relationship with center distance (further houses tend to be smaller).
Example: Scaling and Encoding
The following example demonstrates categorical one-hot encoding and standardization:
Interactive Lab
Scale continuous features using standardization and encode categorical features using one-hot encoding.
Exercise
Evaluate your understanding of data preprocessing constraints:
Why is standardization preferred over Min-Max scaling when features contain significant outliers?
Data Leakage
Preprocessing parameters must fit only on training data. Fitting on all data leaks test set statistics, inflating validation scores.