Two models that predict delivery times both fit last month’s deliveries almost perfectly. Which one deserves trust for tomorrow’s deliveries? Their training errors cannot settle it, because each model was built to make exactly those errors small. This lesson gives the data three separate jobs, fitting, choosing and a final evaluation, and uses a constant baseline to check whether a learned model adds anything for the cases it will actually face.
Key terms
Training set
Definition The training set is the part of the labeled data that the learning algorithm reads to set the model parameters. For a model and a loss , the training error is the average loss over the training examples,
also called the empirical risk on the training set. Every quantity a procedure learns from data, including preprocessing constants such as a mean used to fill missing values or a scale used to standardize a feature, is computed from the training examples only and then applied unchanged to all other data. Because the parameters were chosen to make the training error small, the training error of a fitted model tends to understate its error on new cases, and on its own it cannot rank models of different flexibility.
In plain words The training set is the material the model studies. A low score on it shows that the model fits what it studied, not that it will do well on cases it has never seen.
Example A constructed delivery data set has four training deliveries with distances of 4, 6, 8 and 10 km and durations of 18, 22, 26 and 30 minutes. The rule , which predicts a duration in minutes from a distance of km, reproduces all four durations exactly, so its training MSE is 0. On the two validation deliveries in the next card it misses each one by 1 minute, so a perfect training score did not carry over.
Validation set
Definition The validation set is labeled data held out from fitting and used to compare candidate procedures, such as different model families, feature sets or hyperparameter values, and to make other development decisions. Each candidate is fitted on the training set and scored on the validation set with a metric chosen in advance, and the candidate with the best validation score is selected. Because the validation data take part in that choice, the winning validation score tends to understate the selected model’s error on new cases, more so when many candidates are compared. It is therefore not the final performance estimate. When data are scarce, cross-validation rotates the validation role through several splits of the development data, which is everything except the test set.
In plain words The validation set is a practice exam used to decide how to study. Since it shaped the decision, a good practice score is not the final grade.
Example Take validation deliveries of 6 and 8 km that took 23 and 27 minutes, and write each error as prediction minus actual outcome. The constant rule errs by and minutes, so its validation MSE is square minutes and its MAE is minutes. The rule predicts 22 and 26 minutes, errs by twice, and has validation MSE 1 square minute. With MSE fixed beforehand as the selection metric, the distance rule is selected.
Test set
Definition The test set is labeled data set aside before any modeling decision and used once, after the whole procedure is fixed, to estimate how the final model performs. Suppose the final model was fixed without using the test cases and the test cases are independent draws from the deployment population. Then the test score
is an unbiased estimate of the population risk of , its expected loss on a new case from that population, and with few test cases it can still be far from it. If test results influence any choice, such as revising features after a disappointing score or reporting the best of several models by test score, the test set has served as validation data and its score is no longer unbiased. The smallest of several test errors is on average no larger than the population risk of the model it picks, and usually smaller. A planned refit of the chosen procedure on training and validation data together, done before the test set is opened, keeps the test set’s role. A later revision of the model needs evidence that has not been used yet.
In plain words The test set is the sealed final exam. It is opened once, after every decision is made, and its score describes the frozen model on cases like the test cases. Changing the model after seeing it turns it into one more practice exam.
Example The test deliveries are 5 and 9 km long and took 21 and 29 minutes. The selected rule predicts 20 and 28 minutes, errs by twice, and has test MSE 1 square minute. The constant rule, still predicting the training mean of 24 minutes, errs by and , so its test MSE is square minutes. Two constructed test cases show what these rules did on these deliveries and say little about their population risk. A real report would also consider the number of test cases, how well they represent the deployment population and the practical cost of errors.
Model parameters and hyperparameters
Definition A model parameter is a quantity inside a model whose value the training algorithm sets from the training data, such as a coefficient or intercept of a linear model, or the single value a constant baseline predicts. The value training produces is an estimate, written with a hat as in , and a different training sample would usually give a different estimate. A hyperparameter is a setting fixed before the training algorithm runs that selects the model family or controls how training works, such as the number of neighbors in k-nearest neighbors, a penalty weight , a polynomial degree or a step size. Parameters are fitted on the training set. Hyperparameters are chosen by comparing the validation scores of candidates that were each fitted on the training set. Choosing them by training error tends to favor the most flexible setting. For example, with every training point is its own nearest neighbor, so when no two training points share a feature vector the training error is zero.
In plain words Parameters are what the model learns. Hyperparameters are the choices about how it learns, and the validation set is where those choices are judged.
Example In the delivery data, fix the slope at a value in minutes per km and learn only the intercept. Least squares with a fixed slope gives
the training mean of , which here equals because the training distances average 7 km. The intercept is a parameter, and the slope, chosen from a list of candidates before fitting, plays the role of a hyperparameter. The candidate gives , which is exactly the constant baseline. For the validation MSE is 5, 2, 1 and 2 square minutes, so validation picks with the fitted intercept minutes.
Constant baseline
Definition A constant baseline predicts the same value for every case, with computed from the training targets alone. The best constant depends on the loss. Under squared error the training mean is the unique best constant, because for every
where the cross term has dropped out since , and the last term is positive unless . Under absolute error the best constants are the medians of the training targets, the middle value when is odd and every value between the two middle values, inclusive, when is even. For classification scored by accuracy, the constant with the highest training accuracy is the most frequent training class, and a tie between classes needs a rule stated in advance. A learned model adds something only if it beats an appropriate baseline on held-out data under the same metric, and the baseline’s constant is never recomputed from validation or test outcomes.
In plain words The constant baseline ignores the features and always gives the same answer. A model that cannot beat it has not shown that the features help. For some tasks another simple rule makes the better baseline, such as the last observed value for a series that changes slowly or the value from the same season last year.
Example The training durations 18, 22, 26 and 30 minutes have mean 24, and their squared deviations from 24 add to 80. So any constant has training squared-error total , smallest at . Under absolute error every constant from 22 to 26 minutes ties, with total absolute error 16 minutes. For classification, suppose three of four training deliveries were on time and one was late. The majority baseline always predicts on time. On four held-out deliveries with one late, it is 75 percent accurate and never detects a late delivery, so when a missed late delivery is costly, accuracy alone hides what matters.
Generalization and the deployment population
Definition The deployment population is the set of cases on which a model’s predictions will actually be used, described by which units they concern, where and when they arise, and how their features are measured at prediction time. Write for a new case drawn from it. The population risk of a fixed model is its expected loss on such a case,
Generalization is the performance of a fitted model on new cases from the deployment population that played no part in fitting or selection, measured by with held fixed. The training error is an average over the training data and is a different quantity from this population risk. A test score estimates only for the population the test cases represent, so the split has to copy the intended use, as data leakage and honest splitting works out. When the deployment population changes, an old score need not carry over.
In plain words Generalization means doing well on the cases that matter later, not on the cases already seen. Deciding which cases matter later is a decision about the deployment population, and the test set has to resemble those cases.
Example Suppose a winter disruption adds 10 minutes to every delivery, so the test deliveries now take 31 and 39 minutes. The rule still predicts 20 and 28 minutes, errs by twice and has MSE 121 square minutes, against 1 before. The constant baseline also gets worse, with MSE . A test set drawn from summer deliveries says little about winter performance, and a random split that mixes seasons answers yet another question. For future deliveries the split would hold out a later period, and for new customers it would hold out whole customers.
Check yourself
- A company predicts the monthly maintenance cost of a machine in currency units. The training costs are 600, 1000 and 1400, and the validation costs are 900 and 1300. Find the constant baseline under squared error and its validation MSE, RMSE and MAE, with units.
Show the answer
The baseline is the training mean, currency units. With error written as prediction minus actual, the validation errors are and . The MSE is
squared currency units, the RMSE is currency units and the MAE is currency units.
Units deserve care. If the costs were recorded in hundreds, as 6, 10 and 14, the same steps give an MSE of 5 in squared hundreds, which is squared currency units, not 500.
- A team tunes the number of neighbors of a classifier. It tries every from 1 to 25, checks the test accuracy after each run, keeps the best value and reports that accuracy as the accuracy to expect in use. What is wrong, and how should the work be organized?
Show the answer
The test set chose , so it served as validation data. The largest of 25 test accuracies is on average at least the population accuracy of the model it picks, and usually higher, so the reported number is optimistic. The more candidates are compared, the larger this selection effect tends to be.
The repair is to give each job its own data. Fit each candidate on the training set, choose by validation accuracy or by cross-validation within the development data, optionally refit the chosen setting on training and validation data together, and only then evaluate once on the untouched test set. Since this test set has already been used for choosing, a credible final number now needs fresh test data that played no part in development. Accuracy should also be compared with the majority-class baseline, especially when one class is much more common.
- A company wants to predict next month’s maintenance costs for machines it has just bought. It has three years of monthly cost records for 200 older machines. Propose roles for training, validation and test data, and explain why a random split of all machine-months answers a different question.
Show the answer
The deployment cases are machines the model has never seen, in a month after all the data used for development, so the evaluation should hold out both machines and time. One plan fits candidates on earlier months of one group of machines, compares them on a later period of a second group of machines, and keeps the latest months of a third group, never used in development, as the test set. The exact cutoffs depend on the data, but each role is fixed before any model is compared, and every feature uses only information available before the month being predicted.
A random split of machine-months would put months of the same machine on both sides and mix earlier with later months. Its score would describe predictions for familiar machines with information from around the same time, which is usually an easier task than the intended one. Even the grouped and time-ordered plan assumes that the older machines resemble the new ones, so checking that assumption is part of describing the deployment population.