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.”
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.
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.
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.
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.
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!