Lesson 20: Feature Engineering — Making Data Model-Ready

Encode categorical variables; scale numerical features; create new features.

By • Last updated • 65 min

Feature Engineering: Making Data Model-Ready

Machine learning models only understand numbers. If you have a column with values like “Red”, “Blue”, and “Green”, or a column measuring age in years alongside a column measuring income in thousands of dollars, the model will struggle. Feature engineering is the process of reshaping your raw data into a form that actually helps a model learn — and it is often the single highest-leverage thing you can do to improve model performance, more than swapping in a fancier algorithm.

It helps to think of a dataset as a table where every column is a “feature” the model can use to make its prediction. A raw feature straight from a spreadsheet or database is rarely in the ideal form. Text needs to become numbers. Numbers on wildly different scales need to be brought into a comparable range. And sometimes the most useful piece of information isn’t in any single column at all — it’s hiding in the relationship between two or more columns, waiting to be extracted.

“Better features beat better algorithms.”

Key Techniques

  • One-Hot Encoding: Turns categories into multiple binary (1 or 0) columns. E.g., Is_Red, Is_Blue. This avoids accidentally telling the model that “Green” is somehow numerically “greater than” Red, which would happen if you just assigned Red=1, Blue=2, Green=3.
  • Feature Scaling: Normalizes numerical features so they are on the same scale. For example, standardizing features to have a mean of 0 and a standard deviation of 1. Algorithms like KNN are highly sensitive to unscaled data, because they rely on distance calculations — a feature measured in the thousands (like income) would completely dominate a feature measured in single digits (like age) if left unscaled.
  • Feature Creation: Combining columns to make a more predictive feature, like calculating `FamilySize = Siblings + Parents + 1`. This is often where domain knowledge pays off the most, since you’re encoding a human insight about what actually matters for the problem.
  • Handling Missing Values: Real-world data is messy. Deciding whether to fill in a missing value (with a mean, median, or a sensible placeholder) or drop the row entirely is itself a feature engineering decision that can meaningfully change your results.
  • Binning: Converting a continuous number into categories, like turning exact ages into buckets such as “Child”, “Adult”, and “Senior”. This can help a model pick up on patterns that aren’t perfectly linear with age.

A Real-World Analogy

Imagine handing a friend a recipe written in a mix of grams, cups, and “a pinch of salt.” They’d have a hard time cooking consistently. Feature engineering is like rewriting that recipe so every ingredient uses the same units and every step is unambiguous. The dish (your model) doesn’t get more delicious because you changed the units — but a cook (the algorithm) can now actually follow the instructions reliably.

Feature Engineering vs. Feature Selection

It’s worth distinguishing feature engineering from a closely related idea: feature selection. Feature engineering is about creating and transforming columns to make them more useful. Feature selection is about deciding which of your (possibly many) features are actually worth keeping. A dataset with too many irrelevant or redundant features can slow down training and even hurt accuracy, since the model has to work harder to figure out which signals matter. In practice, the two go hand in hand — you engineer a rich set of candidate features, then prune down to the ones that genuinely help.

Different algorithms also care about feature engineering to different degrees. Tree-based models like Decision Trees and Random Forests are naturally robust to unscaled features, since they split on thresholds rather than measuring distances. Distance-based algorithms like KNN, and gradient-based algorithms like neural networks, are much more sensitive, which is exactly why scaling shows up so often as a required preprocessing step for those model families.

Common Pitfalls

A frequent mistake is fitting a scaler or encoder on your entire dataset, including the test set, before splitting the data. This leaks information from the test set into training and makes your evaluation overly optimistic. The safer approach is to fit transformations only on the training data, then apply that same transformation to the test data. It’s also easy to over-engineer features that are really just memorizing quirks of your specific dataset rather than capturing a pattern that will generalize — always sanity-check that a new feature makes intuitive sense, not just that it happens to improve your validation score.

Another subtle issue is one-hot encoding a category with hundreds of unique values, like a “city” column with a different value for every city in a country. This can explode your feature count into the hundreds or thousands, making training slower and increasing the risk of overfitting. In situations like this, practitioners often group rare categories into an “Other” bucket, or switch to a different encoding strategy entirely, rather than blindly one-hot encoding everything.

Building the Habit

As you work through more projects, you’ll start to develop intuition for which transformations a given dataset needs just by looking at a handful of rows and summary statistics. That intuition is exactly what separates practitioners who can squeeze meaningful performance out of a mediocre algorithm from those who only ever get mediocre results out of an excellent one. Treat feature engineering not as a box to check before “the real modeling starts,” but as one of the most valuable, ongoing parts of the whole process.

The challenge below gives you hands-on practice with one of the most common scaling techniques, StandardScaler, which you’ll use constantly once you start working with algorithms like KNN, SVMs, and neural networks that are sensitive to feature scale.

Python Challenge: Scale it Up

Use scikit-learn’s StandardScaler to scale some numerical data.

from sklearn.preprocessing import StandardScaler
import numpy as np

# Age (years) and Income ($)
X = np.array([[25, 40000], 
              [45, 85000], 
              [30, 50000]])

# TODO: Initialize StandardScaler
# scaler = ???

# TODO: Fit the scaler to X and transform X
# X_scaled = ???

# print(X_scaled)
# Notice how the values are now centered around 0!
main.py
Run your code to see output here.