Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 12: Tuning Models and Feature Engineering

By now you can build a model, measure it honestly, and tell whether it is overfitting or underfitting. The next step is making it better, and there are two levers to pull. The first is tuning: adjusting the model's settings, called hyperparameters, that you choose before training begins. The second is feature engineering: creating better features from the raw data you already have. Of the two, feature engineering usually matters more — a clever new feature can do what no amount of tuning can — but both are part of the job.

Hyperparameters are the settings you choose rather than the model learning: how deep a decision tree may grow, how many trees a random forest contains, or how big a step gradient descent takes. Tuning them by hand, one at a time, is slow and easy to get wrong, because settings interact: the best tree depth depends on the other settings too. The standard approach is a search. In a grid search, you list a few values for each setting and try every combination, using cross-validation to score each one. In a random search, you try random combinations instead, which often finds a good answer faster when some settings matter much more than others. Either way, the rule from Lesson 3 still applies: tune on the validation folds, and keep the final test set untouched until the end.

Making better features

Feature engineering is where knowledge of the problem beats knowledge of algorithms. Raw data rarely arrives in the form a model wants. Dates can be split into day of the week or month, which may matter more than the date itself. A house's size and number of bedrooms can be combined into average room size. Text can be turned into counts of important words. A shop's raw sales records can become features such as how many days since a customer's last visit. Each new feature is a guess about what might matter, and cross-validation tells you whether the guess paid off.

There is also a darker side to feature engineering: a feature can be too good. If you are predicting whether a customer will leave, and one of your features is whether they have already asked to cancel, the model will look perfect — because the feature contains the answer. That is leakage again, in a new costume: a feature that would not be available at the moment the prediction has to be made. Always ask of every feature: would I know this value at prediction time? If the answer is no, the feature must go, however tempting its score.

The danger of tuning too much

Tuning has its own version of overfitting, and it is easy to fall into. Every time you try a new combination of settings and keep the best one, you are learning something about the validation data — and if you try hundreds of combinations, you will eventually find one that happens to score well on those particular folds by luck alone. That setting will then disappoint you on fresh data, because its good score was partly chance. The defence is the same discipline you learned in Lesson 3: keep the final test set untouched until the very end, and treat the best validation score with mild suspicion when it comes from a very long search. A useful rule is to prefer simpler settings when their validation scores are close to the best — a tree of depth five that scores 81% is a safer choice than a tree of depth twelve that scores 82%, because the simpler model's score is less likely to be a lucky accident, and it will be easier to explain to anyone who asks.

One last habit separates careful practitioners from careless ones: keep a record of what you tried. Tuning and feature engineering involve dozens of small experiments, and without notes you will forget which combination gave the best score, or why you dropped a feature. A simple log — the setting, the feature, the cross-validation score — is enough. Machine learning is an experimental science, and an experiment without a record is an experiment you cannot repeat.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • Machine Learning Fundamentals: Cross Validation — StatQuest with Josh Starmer
    Josh Starmer explains cross-validation with worked examples, showing how repeated train-and-test splits give a steadier estimate of how a model will do on new data.

Key Terms

Hyperparameter
A setting chosen before training begins, such as how deep a tree may grow, rather than something the model learns.
Grid search
Trying every combination of a list of hyperparameter values and keeping the combination with the best cross-validation score.
Random search
Trying random combinations of hyperparameter values, which often finds a good answer faster than a grid search.
Feature engineering
Creating new, more useful features from raw data, such as turning a date into a day of the week.
Leakage
When information that would not be available at prediction time, or that comes from the test data, sneaks into training.
Validation score
A model's score on held-out data during development, used to compare settings and features before the final test.

Discussion Questions

  1. Why is it better to tune hyperparameters with cross-validation than with a single train-test split?
  2. Think of a prediction problem you care about. What new feature could you build from the raw data, and what would it capture?
  3. A feature gives a perfect score but would not be known at prediction time. Should you keep it? What would happen if you did?

Practice Questions

  1. What is a hyperparameter? Give an example.
  2. What is the difference between a grid search and a random search?
  3. Give one example of feature engineering.
  4. Why is a feature that contains the answer dangerous?
  5. Why should you keep a written log of tuning experiments?
Show answers
  1. A setting chosen before training, such as the maximum depth of a decision tree. It is set by the person building the model, not learned from the data.
  2. A grid search tries every listed combination; a random search tries random combinations. Random search often finds a good answer faster when only a few settings matter much.
  3. Turning a date into the day of the week, or combining size and bedrooms into average room size. New features are built from raw data to capture something the model could not see directly.
  4. Because it leaks information that would not be available when the prediction must be made. The model looks perfect in testing but is useless in the real world, where that feature does not exist yet.
  5. So you can repeat the best result and remember why each choice was made. Without notes, dozens of small experiments blur together and the best combination is lost.
Keep reading to complete this lesson