Feature Engineering for Machine Learning
The feature is the model's raw material
A machine learning model can only find patterns that are representable in the features you give it. A linear model can't discover that "risk is high when age is under 25 AND claims filed is over 2" unless you hand it a feature that encodes that interaction directly — it can only draw straight-line relationships in the feature space it's given. Feature engineering is the practice of transforming, combining, and constructing input variables so that the relationships a model needs to learn are actually present and accessible in the data it sees. It's frequently the highest-leverage work in a machine learning project — often improving performance more than switching from a simple model to a complex one.
Note
Binning: turning continuous values into categories
Binning (or discretization) converts a continuous numeric variable into discrete buckets — for example, converting raw age into bins like "18-25", "26-40", "41-60", "60+". This helps when the relationship between the variable and the target isn't smoothly linear or monotonic, but rather jumps between ranges (e.g., insurance risk that spikes for both very young and very old drivers, dipping in between). Binning also makes a model more robust to noisy outliers within a bin, and can make results more interpretable to non-technical stakeholders ("customers aged 26-40 convert at 2x the rate" is more actionable than a raw coefficient).
The tradeoff: binning throws away precision within each bucket, and choosing bin boundaries (equal-width, equal-frequency/quantile-based, or domain-informed) is itself a design decision that affects results.
Polynomial features and interaction terms
A plain linear model can only fit straight lines and flat planes. Polynomial features — adding $x^2$, $x^3$, and so on as new columns — let a linear model fit curves, by giving it the nonlinear terms as additional inputs it can weight linearly.
Interaction terms go further: they multiply two (or more) features together, like $\text{price} \times \text{quantity}$ or $\text{age} \times \text{income}$. This matters because many real effects are genuinely *conditional*: the effect of a marketing discount on purchase likelihood might depend heavily on customer income — a relationship a model can only see if you explicitly give it the interaction term (or use a model, like a tree ensemble, that can discover interactions automatically).
Note
One-hot vs. ordinal encoding, revisited for engineering
Encoding choice is itself a feature engineering decision, not just a preprocessing formality. One-hot encoding creates one binary column per category and is correct for unordered categories, but loses any notion of similarity between categories (in one-hot space, "Monday" and "Tuesday" are exactly as different as "Monday" and "Sunday"). Ordinal encoding assigns an order and preserves a notion of distance, which is powerful when the order is real (small/medium/large) but actively misleading when it's not. For cyclical features like day-of-week or month, neither is ideal — a better engineered feature uses sine/cosine transforms of the cyclical position, so that December and January end up numerically close instead of far apart.
Feature encoding and transformation choices
| Technique | Best for | What it adds |
|---|---|---|
| One-hot encoding | Unordered categories with low-to-moderate cardinality | No false ordering, but no similarity structure either |
| Ordinal encoding | Genuinely ordered categories | Preserves meaningful distance between categories |
| Binning | Continuous variables with non-monotonic effects | Captures threshold/range effects, improves interpretability |
| Polynomial features | Known nonlinear relationships in a linear model | Lets linear models fit curves |
| Interaction terms | Effects that depend jointly on two+ variables | Captures conditional relationships explicitly |
| Cyclical (sin/cos) encoding | Time-based features (day, month, hour) | Preserves wraparound continuity (Dec near Jan) |
Domain-driven feature creation
The most valuable features rarely come from automated transformations — they come from domain knowledge about what actually drives the outcome. In MarTech, raw "days since signup" is less useful than engineered features like "average session frequency over the last 2 weeks," "ratio of email opens to emails sent," or "days since last purchase relative to the customer's historical purchase cadence." In fraud detection, raw transaction amount matters less than "transaction amount relative to this account's typical spending" or "number of distinct merchant categories used in the last hour."
These ratio features, rolling aggregates, and relative/normalized features encode context that a model can't reconstruct on its own from raw values — they require understanding what actually makes a case anomalous or predictive in your specific domain. This is why feature engineering is often described as the place where subject-matter expertise and machine learning meet most directly.
Note
What's next
Feature engineering assumes your raw data is already clean — see Data Preprocessing & Cleaning for Machine Learning if you haven't handled missing values and leakage yet. To see how tree-based models can automatically discover some interaction effects without manual engineering, revisit decision-trees and random-forest.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.