Guide

Models eat numbers. Real data arrives as timestamps, categories, nested JSON, missing fields and duplicate users. Feature engineering turns that mess into a matrix that a linear model or a gradient-boosted tree can learn from. It has to do this without leaking the future, and it has to apply the same transforms in training and in serving.

On tabular problems, features often matter more than the choice between boosting libraries. A better definition of days_since_last_active can beat another round of hyperparameter search.

Concepts in this topic

Read in this order. Each page builds on the one before it.

StepConceptWhat it answers
1Scaling and transformsHow do you put numbers on a comparable scale?
2Encoding categories and textHow do you turn labels and words into numbers?
3Missing values and pipelinesWhat does a gap mean, and where should the fill be fitted?
4Point-in-time featuresCould this value have existed at prediction time?
5Target encoding without leakageHow do you encode a category with thousands of values?
6Class imbalanceWhat do you do when positives are rare?

Interactions and a minimal feature set

Linear models cannot learn "if A and B" unless you give them the product or a crossed one-hot. Add a few hand-crossed features that domain experts trust, such as mobile × new_user. Trees find interactions on their own, so spend your time on clean, informative columns. Neural nets often turn wide categorical columns into embeddings.

For many B2B churn or conversion problems, this short list beats a naive rule:

  • Activity counts over 1, 7 and 28 days.
  • Intensity ratios, such as 7-day over 28-day activity, which show acceleration or decay.
  • Time since the last activity, and tenure since signup.
  • Plan, seat count or revenue bucket.
  • Support tickets over 30 days, with a missing indicator.

Train a logistic model and a boosted tree on only these. If neither beats the heuristic on a time split, more exotic encodings will not save you. Revisit the labels and the decision.

Common mistakes across the topic

  • Fitting transforms on all the data. Scalers, imputers and encoders are fitted on the training split only, inside the pipeline.
  • Using different code in training and serving. Notebook pandas and a rewritten production SQL query drift apart. Share one implementation, or serialize the whole pipeline.
  • Searching hyperparameters before features. Fix the features first, then tune.

Key takeaways

  • Scale and transform numbers for linear models. Trees care less about scale and more about missing values and categories.
  • Use one-hot or hashing for categories by default. Target-encode high-cardinality fields only with out-of-fold statistics.
  • Treat missingness as a feature, and impute inside a pipeline fitted on training data.
  • Every window aggregate must be correct as of the prediction time.
  • Ship the transforms as one pipeline shared by training and serving.

Further learning

A practical, example-driven book on preparing predictors:

Feature Engineering and Selection (Kuhn and Johnson)Feature Engineering and Selection: A Practical Approach for Predictive Modelsfeat.engineering

For features, training-serving skew and data flows in production:

Designing Machine Learning Systems (Chip Huyen)oreilly.com

The scikit-learn preprocessing guide documents the APIs used on these pages:

scikit-learn User Guide: PreprocessingThe sklearn.preprocessing package provides several common utility functions and transformer classes to change raw feature vectors into a representation that is more suitable for the downstream esti...scikit-learn