Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Machine Learning Associate

Cross-validation

Databricks Machine Learning Associate · Lesson 14 of 19 · 1:50:19

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Machine Learning Associate exam. In this lesson: k-fold cross-validation, cross-validation or a train-validation split, the Spark ML CrossValidator and its parameter grid, and counting the models that a tuning run trains. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Two winners

Two data scientists at Juniper Rides compare the same hyperparameters on the damage data, and get different winners, because each one used a single random validation split of a few thousand trips. Meanwhile, a grid search with cross-validation has been running for six hours. This lesson covers how cross-validation works, how it compares with a train-validation split, the CrossValidator of Spark ML, and how to count the models that a search trains.

K-fold cross-validation

A single validation split gives a score that depends on which rows landed in it, especially with little data. K-fold cross-validation splits the training data into k folds, trains a model on k minus one folds, and validates it on the remaining fold, k times, so each fold serves once for validation. The score is the average over the k folds, which is more stable, and every row serves for both training and validation. A test set is still held out, untouched, for the final evaluation. And with time series, like daily rides, the folds must respect time, training on the past and validating on later dates, never the reverse.

Cross-validation or a train-validation split

A train-validation split uses one split, with a ratio like eighty percent for training and twenty for validation, so each combination is evaluated once. It is cheaper, by a factor of about k, but less reliable when the training data is not large. Cross-validation costs k times more, since each combination trains k models, but it gives a more stable estimate, and uses all the data. So choose cross-validation for small or medium datasets, where the estimate matters most, and a train-validation split for very large data, or a tight compute budget. In Spark ML, both are estimators, CrossValidator and TrainValidationSplit, and trainRatio sets the share of training data in the second one.

The CrossValidator

In Spark ML, a CrossValidator takes an estimator, often a whole Pipeline, the estimatorParamMaps built with ParamGridBuilder, and an evaluator. numFolds sets k, and parallelism evaluates several combinations at once. The evaluator measures each model on its validation fold, like a BinaryClassificationEvaluator for the area under the ROC curve, or a RegressionEvaluator for RMSE. After the search, the CrossValidator refits the estimator on the entire dataset that it received, with the best parameters, and returns a CrossValidatorModel. Its bestModel holds that model, and avgMetrics holds the average score of each combination.

Counting the models

The cost of a search follows from a simple count. The number of combinations is the product of the number of values of each hyperparameter: three depths and four learning rates give twelve combinations. Cross-validation trains each combination once per fold, so twelve combinations with three folds train thirty six models, then one final model on all the data. A train-validation split trains each combination once, so twelve models, plus the final one, which is why it costs about k times less.

Recap

Let's recap. K-fold cross-validation trains k models per combination, each validated on a different fold, and averages the scores. A train-validation split evaluates each combination once, which is cheaper, but less reliable on smaller data. The CrossValidator takes an estimator, a parameter grid, an evaluator, and numFolds, and refits the best model on all its data. A search trains the number of combinations times the number of folds, plus one final model. And a test set stays apart, for the final evaluation.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. With five-fold cross-validation and one combination of hyperparameters, how many times does each row of the training data serve for validation?

    • Five times
    • Four times
    • Once
    • Never: the test set validates
    Show the answer

    Once

    Each fold serves once for validation, so each row is validated once, and it serves for training in the four other models. “Five times” counts the models, not the validations of a row. “Four times” is the number of times that each row serves for training. And “Never: the test set validates” is wrong, since the test set stays apart, for the final evaluation.

  2. A team has 500 million trips and a tight compute budget, to compare twelve combinations of hyperparameters for a Spark ML model. What fits best?

    • CrossValidator with ten folds
    • TrainValidationSplit, one fit per combination
    • CrossValidator with two folds, no test set
    • Comparing the combinations on the test set
    Show the answer

    TrainValidationSplit, one fit per combination

    With this much data, one split already gives a reliable score, and each combination trains once instead of k times. “CrossValidator with ten folds” trains ten models per combination, ten times the cost. “CrossValidator with two folds, no test set” drops the test set, which must stay for the final evaluation. And “Comparing the combinations on the test set” would tune on the test set, so its score would no longer be fair.

  3. A CrossValidator uses a grid with four values of maxDepth and three values of maxIter, and four folds. How many models does it train in total, including the final one?

    • 12
    • 48
    • 16
    • 49
    Show the answer

    49

    Four times three gives twelve combinations, each trained once per fold, so forty eight models, plus the final model, refit on the whole dataset. “12” counts the combinations only. “48” forgets the final refit. And “16” adds the number of folds, instead of multiplying by it.