Advanced Machine Learning Pipelines
Advanced Feature Engineering
Beyond One-Hot Encoding
You already know that one-hot encoding works well for categorical variables with a few unique values, like 'color' (red, green, blue). But what happens when you have a feature with thousands of unique values, like zip codes or user IDs? One-hot encoding would create thousands of new columns, making your dataset massive and difficult for a model to learn from. This is a classic case of the —too many features can actually worsen model performance.
A smarter approach for high-cardinality features is Target Encoding. Instead of creating new columns, you replace each category with the average value of the target variable for that category. For example, to predict customer churn, you could replace each 'city' with the average churn rate for that specific city. This single new feature captures a powerful signal from the target variable itself.
The major risk with Target Encoding is data leakage. If you calculate the average churn rate for a city using the entire dataset and then use that feature to train your model, the model has already seen information about the target values it's supposed to predict. This leads to an overly optimistic performance estimate that won't hold up on new, unseen data.
To prevent leakage, you must calculate the target encodings on your training data only, and then apply those learned averages to your validation and test sets. A more robust method involves using a k-fold cross-validation scheme within the training set itself. For each fold, you calculate the encodings on the other folds and apply them to the current one. This ensures that the encoding for any given row is derived from data that row has never seen.
Engineering for Time
When working with time-series data, the order of your data points is everything. To make a model aware of this temporal context, you need to create features that explicitly represent time-dependent patterns.
The most fundamental temporal feature is a lag feature. This is simply a value from a previous time step. For example, to predict today's sales, you might include features for yesterday's sales (lag 1), the sales from seven days ago (lag 7), and the sales from a year ago (lag 365). These features directly help the model understand recency, weekly seasonality, and yearly seasonality.
Beyond lags, you can create rolling window features. A 7-day rolling average of sales smooths out daily noise and captures the recent trend. Other useful features include extracting parts of the timestamp, like the day of the week, month, or quarter. For cyclical features like 'day of week', it's better to use than simple integer encoding (1-7), as this helps the model understand that Sunday is close to Monday in a cyclical sense.
Taming Skewed Data
Many real-world datasets have skewed distributions. Think of housing prices or user income; most values are clustered at the low end, with a long tail of high values. Linear models and others that assume normally distributed data can perform poorly on such features. To fix this, we can use Power Transforms to stabilize variance and make the distribution more symmetric, or Gaussian-like.
The Box-Cox transformation is a popular choice, but it has one key limitation: it only works on strictly positive data.
A more flexible alternative is the Yeo-Johnson transformation, which works with both positive and negative values. Both methods automatically find the best exponent () to apply to your data to normalize it. Another non-parametric option is Quantile Mapping, which forces the feature's distribution to match a target distribution (like the normal distribution) by mapping its quantiles.
Feature engineering is where the magic happens.
A dataset for predicting house prices contains a 'zip_code' feature with over 10,000 unique values. Why would applying one-hot encoding to this feature likely be a poor choice?
When implementing target encoding to prevent data leakage, you should calculate the encodings using your training data only and then use those learned values to transform your validation and test sets.
Mastering these techniques helps bridge the gap between raw data and a high-performing model, especially when dealing with the complexities of real-world data.