Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

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

Tuning and evaluating Spark ML models

Databricks Machine Learning Professional · Lesson 2 of 22 · 9:46

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 Professional exam. In this lesson: parameter grids, CrossValidator and TrainValidationSplit, tuning the whole pipeline, the regression, binary, and multiclass evaluators and their default metrics, and a fair final score on held-out data. Includes three original practice questions.

Study notes

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

Default settings

Pebblecove's cancellation pipeline works, but its logistic regression still runs with default settings, and nobody knows whether a stronger penalty or another mix of penalties would do better. The team also needs a fair score, one that it can trust before the model goes anywhere near production. This lesson covers tuning a Spark ML model with MLlib, and evaluating it with the right metric.

Grids and cross-validation

ParamGridBuilder builds the grid: each addGrid call lists the values to try for one parameter, like the penalty strength, and build returns every combination. CrossValidator takes an estimator, the grid, an evaluator, and a number of folds, and for each combination, it trains on all folds but one and scores the one left out. It then refits the best combination on all the training data, and the result is a CrossValidatorModel, with that refitted model as bestModel and the average metric of each combination in avgMetrics. TrainValidationSplit evaluates each combination only once, on a single split set by trainRatio, which is cheaper, but less reliable on small data. And the parallelism parameter trains several models at the same time, where a value up to about ten suits most clusters.

Tuning the whole pipeline

The safer pattern passes the whole Pipeline as the estimator of CrossValidator, so that every stage that learns, like StringIndexer, Imputer, or StandardScaler, is fitted again on the training folds of each round. The grid can then reach any stage, like the number of trees of a random forest, or the strategy of the Imputer. The cost grows with the grid size times the number of folds, so start with a coarse grid, and keep parallelism within what the cluster can run. And with MLflow autologging for pyspark dot ml, the search is recorded as a parent run, with child runs for the combinations, and the best parameters on the parent.

Evaluators

Each evaluator reads the prediction columns, and returns one number. RegressionEvaluator uses the root mean squared error by default, and also offers the mean squared error, R squared, and the mean absolute error. BinaryClassificationEvaluator uses the area under the ROC curve by default, and the area under the precision recall curve when the positive class is rare, while MulticlassClassificationEvaluator uses F1 by default, and also offers accuracy. Set metricName to change the metric, and CrossValidator reads isLargerBetter from the evaluator, so it maximizes an area or R squared, and minimizes an error.

A fair final score

Cross-validation picks the parameters, so its scores are slightly optimistic, which is why a test set stays aside, untouched by the search. Split the bookings before tuning, fit the CrossValidator on the training part, and call evaluate on the test predictions of bestModel. With dates in the data, split by time, training on older bookings and testing on newer ones, as production will see them. Compare the score with a simple baseline, like predicting the cancellation rate of each country, to show what the model adds. And log the test metric, the parameters, and the model to MLflow, so that the choice can be checked later.

Recap

Let's recap. ParamGridBuilder lists the combinations, and CrossValidator scores each one over k folds, then refits the best on all the training data. TrainValidationSplit uses one split, which is cheaper and less reliable, and parallelism trains several models at once. Pass the whole Pipeline as the estimator, so that each fold refits the stages that learn. RegressionEvaluator defaults to the root mean squared error, BinaryClassificationEvaluator to the area under ROC, and MulticlassClassificationEvaluator to F1, with metricName to change it. And keep a test set aside, split by time when dates matter, for a fair final score.

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. To save time, the team fits an Imputer on all the bookings first, then passes only the logistic regression to CrossValidator. What is the risk?

    • CrossValidator refuses a single model
    • The number of folds drops to one
    • The imputed column is dropped from the vector
    • Validation folds leak into the median it learned
    Show the answer

    Validation folds leak into the median it learned

    The Imputer learned its median from every row, including the rows that later serve as validation folds, so the scores look better than they are. “CrossValidator refuses a single model” is wrong, since CrossValidator accepts any estimator, a single model included. “The number of folds drops to one” is wrong, since the number of folds does not depend on the Imputer. And “The imputed column is dropped from the vector” is wrong, since nothing forces the imputed column to be dropped.

  2. About one booking in twenty is cancelled, and the team wants the tuning metric to focus on the rare cancellations, whatever the threshold. Which evaluator setting fits?

    • Binary evaluator, areaUnderPR
    • Binary evaluator, areaUnderROC
    • Regression evaluator, rmse
    • Multiclass evaluator, accuracy
    Show the answer

    Binary evaluator, areaUnderPR

    The area under the precision recall curve focuses on the rare positive class, and it does not depend on a threshold. “Binary evaluator, areaUnderROC” is the default, which stays high on imbalanced data even when the rare class is poorly found. “Regression evaluator, rmse” would treat a yes or no label as a number to predict. And “Multiclass evaluator, accuracy” would depend on the threshold, and accuracy hides a rare class.

  3. A CrossValidator uses a RegressionEvaluator with metricName set to R². Which combination does it keep?

    • The lowest average R²
    • The highest average R²
    • The best R² on any single fold
    • None: R² is not supported
    Show the answer

    The highest average R²

    R squared is larger when the model is better, so the evaluator reports isLargerBetter as true, and the search keeps the highest average. “The lowest average R²” would describe an error metric, like the root mean squared error, which the search minimizes. “The best R² on any single fold” is wrong, since the search uses the average over the folds, not one fold. And “None: R² is not supported” is wrong, since R squared is a valid metric for this evaluator.