CertLumen

Databricks Machine Learning Associate · Section 3 · 31% of the exam

Perform cross-validation as a part of model fitting.

One of the objectives of the Databricks Certified Machine Learning Associate exam, in the section “Model Development”. Here is what it covers, the points to remember and a question to try.

What it covers

A train-validation split scores each candidate once; k-fold cross-validation scores it k times on rotating folds, which is more reliable but k times more expensive. A grid search with cross-validation trains (number of combinations) x k models, plus one refit of the best combination. Comparing training and validation error reveals underfitting (high bias) or overfitting (high variance).

Points to remember

  • Training set fits parameters, validation data selects hyperparameters, the test set is used once for the final estimate.
  • Train-validation split: one model per combination, cheap, less reliable on small data. k-fold CV: k models per combination, about k times the cost, more robust, and every row is validated once.
  • Spark defaults: CrossValidator(numFolds=3, parallelism=1), TrainValidationSplit(trainRatio=0.75). scikit-learn uses 5 folds when cv=None (stratified for classifiers).
  • CrossValidator is an estimator; fit() returns a CrossValidatorModel with bestModel and avgMetrics, and refits the best combination on the whole input dataset.
  • Models during grid search with CV = (product of the values per hyperparameter) x k; the sample answer is 18 x 5 = 90. Add 1 only when the refit is counted.

Try a question

A CrossValidator uses a ParamGridBuilder grid with 3 values of regParam and 2 values of elasticNetParam, with numFolds=5. After cv_model = cv.fit(train_df), what does cv_model.avgMetrics contain?

  1. A30 values, one for every fold of every parameter map, in the order they were trained
  2. B5 values, one per fold, each averaged over the 6 parameter maps
  3. C6 values: the metric averaged over the 5 folds for each parameter map, in grid order
  4. D1 value: the average metric of the best parameter map after the final refit
Show the answer

C. CrossValidator.fit() evaluates every parameter map on every fold and averages the evaluator's metric over the folds. CrossValidatorModel.avgMetrics lists those averages in the order of estimatorParamMaps, which lets you see how each combination did, and bestModel is the best combination refit on the whole input DataFrame.

Written for practice in the style of the exam: never a question from the real exam.

In the course

The lesson Cross-Validation, Counting Models and the Bias-Variance Trade-off teaches this objective (about 45 minutes), and 19 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.

More in this section

← Previous objective · All 48 objectives · Next objective →