CertSafari
    Databricks Certified Machine Learning Professional· Lessons

    Domain 1 · Lesson 1/11

    Tuning, Evaluating and Scoring Spark ML Models

    Model Development Using Spark ML

    8 min read
    9.09% of exam
    10 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Tune a Spark ML model with CrossValidator or TrainValidationSplit, and choose the right Hyperopt trials class for MLlib
    • Pick the evaluator that matches a regression, binary or multiclass problem, and log its metrics
    • Score a model in batch or streaming, and pick Spark-based scoring or Model Serving based on the inference type

    1.Tuning Spark ML models

    Spark ML has its own tuning classes, CrossValidator and TrainValidationSplit. They wrap an estimator, which can be a whole pipeline, and search hyperparameter settings against an evaluation metric. MLlib cross validation can even tune an XGBoost model. For tracking, the old MLlib automated MLflow tracking logged hyperparameters and metrics for these tuners. It is now deprecated and disabled by default from Runtime 10.4 LTS ML onward. Use mlflow.pyspark.ml.autolog() instead, which Databricks Autologging turns on by default.

    Hyperopt can also tune MLlib, but the correct setup is easy to get backwards. SparkTrials sends trials to the workers. It is meant for single-machine models such as scikit-learn. An MLlib model already uses the whole cluster for each fit. So you use the default Trials class: Hyperopt generates each trial on the driver, and the algorithm itself does the distributed training. Trials does not autolog to MLflow, so you log each trial yourself. Also note that Hyperopt is not included in Databricks Runtime ML after 16.4 LTS ML. Databricks now recommends Optuna for single-node optimization or Ray Tune for distributed tuning.

    Hyperopt trials classes and the models they suit
    Trials classUse withWhere each trial runsMLflow logging
    SparkTrialsSingle-machine models such as scikit-learnDistributed to Spark workersAutomatic
    TrialsDistributed algorithms such as MLlib or HorovodDriver node, with full cluster resourcesManual: call MLflow yourself

    Checkpoint 1 of 5· Check yourself

    On Databricks Runtime 15.4 LTS ML, a team runs CrossValidator over a pyspark.ml pipeline and wants the run tracked in MLflow. What is the documented approach?

    Sources1234

    2.Evaluating Spark ML models

    Evaluation is what tuning optimizes, so the evaluator has to match the problem type. pyspark.ml.connect lists three: RegressionEvaluator for continuous targets, BinaryClassificationEvaluator for two-class problems, and MulticlassClassificationEvaluator for more than two classes. CrossValidator and TrainValidationSplit score every candidate with the evaluator you give them. If you tune with Hyperopt, the objective function does the training and loss calculation and returns the loss for fmin to minimize.

    Record the metrics so runs can be compared. MLflow tracking can log metrics computed with any framework. With Hyperopt's Trials class that logging is manual, as covered above. These sources do not list each evaluator's metricName options, such as which ones apply to regression versus multiclass. For those details Databricks points to the Apache Spark API references.

    Checkpoint 2 of 5· Exam question

    A machine learning engineer is assembling a Spark ML `Pipeline` with a `StringIndexer`, a `OneHotEncoder`, a `VectorAssembler`, and a `LogisticRegression` estimator as its stages, in that order. What happens when `pipeline.fit(trainingData)` is called?

    Checkpoint 3 of 5· Check yourself

    A product classifier predicts one of ten categories. Which evaluator should the tuning step use?

    Sources5367

    3.Scoring in batch and streaming

    Scoring a table means applying the same model to every row, which is data parallelism and exactly what Spark is built for. Databricks describes batch and streaming scoring as high-throughput and low-cost, with latencies as low as minutes. A fitted Spark ML model or pipeline scores a DataFrame through transform. For any model logged with MLflow, Databricks recommends Spark pandas UDFs to scale batch and streaming inference across the cluster. MLflow generates the inference code that applies a logged model as a pandas UDF. Databricks' MLlib example notebooks include a Structured Streaming example.

    Checkpoint 4 of 5· Check yourself

    A model logged with MLflow has to score a multi-terabyte table every night across the cluster. Which approach does Databricks recommend?

    Sources89

    4.Spark-based scoring or Model Serving: choosing by inference type

    The inference type decides where the model runs. Batch and streaming workloads take whole datasets and accept latency measured in minutes, so they fit Spark-based scoring. Real-time workloads are different: an application sends one request and waits for the answer. For that, Databricks offers Model Serving. It is a highly available, low-latency service that exposes each model as a REST API and scales automatically on serverless compute. Model Serving also supports batch work through ai_query, which runs efficient batch inference against a served endpoint.

    Matching inference type to the Databricks scoring option
    Inference typeDatabricks optionWhat you get
    BatchApache Spark UDFs or mlflow.pyfunc; ai_query against a Model Serving endpointHigh-throughput, low-cost scoring of whole tables
    StreamingSpark pandas UDFs over a Structured Streaming sourceContinuous scoring at latencies as low as minutes
    Real-timeModel Serving REST endpointLow-latency, high-uptime responses with serverless autoscaling

    Checkpoint 5 of 5· Check yourself

    A loan-approval web app needs a prediction within the user's request. Which deployment fits?

    Sources106

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.SparkTrials is the right way to parallelize Hyperopt for any model on a cluster, including MLlib.Why is that wrong?

      SparkTrials is for single-machine models. MLlib models already train in a distributed way, so use the default Trials class, which runs each trial from the driver.

      Covered in Tuning Spark ML models

    2. 2.CrossValidator runs are logged to MLflow automatically by MLlib's built-in tracking on current runtimes.Why is that wrong?

      MLlib automated MLflow tracking is deprecated and off by default from 10.4 LTS ML. Use mlflow.pyspark.ml.autolog() instead.

      Covered in Tuning Spark ML models

    3. 3.Hyperopt ships with every current Databricks Runtime ML, so new tuning code should be built on it.Why is that wrong?

      Hyperopt is not included after 16.4 LTS ML. Databricks recommends Optuna for single-node optimization or Ray Tune for distributed tuning.

      Covered in Tuning Spark ML models

    Sources

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “when you run tuning code that uses CrossValidator or TrainValidationSplit, hyperparameters and evaluation metrics are automatically logged in MLflow.”
      ↩︎ Tuning Spark ML models
      “MLlib automated MLflow tracking is deprecated and disabled by default on clusters that run Databricks Runtime 10.4 LTS ML and above.”
      ↩︎ Exam trap 2
      “Instead, use MLflow PySpark ML autologging by calling mlflow.pyspark.ml.autolog(), which is enabled by default with Databricks Autologging.”
      ↩︎ Checkpoint
    2. 2.
      “The second example shows how to use MLlib cross validation to tune an XGBoost model.”
      ↩︎ Tuning Spark ML models
    3. 3.
      “SparkTrials is designed to parallelize computations for single-machine ML models such as scikit-learn.”
      ↩︎ Tuning Spark ML models
      “This function typically contains code for model training and loss calculation.”
      ↩︎ Evaluating Spark ML models
      “For models created with distributed ML algorithms such as MLlib or Horovod, do not use SparkTrials.”
      ↩︎ Exam trap 1
      “Use Trials when you call distributed training algorithms such as MLlib methods or Horovod in the objective function.”
      ↩︎ Prediction
    4. 4.
      “Each trial is executed from the driver node, giving it access to the full cluster resources.”
      ↩︎ Tuning Spark ML models
      “Databricks does not support automatic logging to MLflow with the Trials class.”
      ↩︎ Tuning Spark ML models
      “Databricks recommends using either Optuna for single-node optimization or RayTune”
      ↩︎ Tuning Spark ML models
      “Hyperopt is not included in Databricks Runtime for Machine Learning after 16.4 LTS ML.”
      ↩︎ Exam trap 3
    5. 5.
      “Evaluator: pyspark.ml.connect.RegressionEvaluator, pyspark.ml.connect.BinaryClassificationEvaluator and MulticlassClassificationEvaluator”
      ↩︎ Evaluating Spark ML models
    6. 6.
      “MLflow tracking can log metrics computed using your custom framework”
      ↩︎ Evaluating Spark ML models
      “You can also use custom code with Apache Spark UDFs (example) or mlflow.pyfunc for batch inference.”
      ↩︎ Spark-based scoring or Model Serving: choosing by inference type
      “Model Serving provides low-latency, high-uptime managed REST endpoints with serverless autoscaling.”
      ↩︎ Checkpoint
    7. 7.
      “For reference information about MLlib features, Databricks recommends the following Apache Spark API references”
      ↩︎ Evaluating Spark ML models
    8. 8.
      “Batch and streaming scoring supports high-throughput, low-cost scoring at latencies as low as minutes.”
      ↩︎ Scoring in batch and streaming
      “When you log a model from Databricks, MLflow automatically provides inference code to apply the model as a pandas UDF.”
      ↩︎ Scoring in batch and streaming
      “consider creating a preprocessing job to ETL the data into a Delta Lake table before running the inference job”
      ↩︎ Scoring in batch and streaming
      “Use Spark Pandas UDFs to scale batch and streaming inference across a cluster.”
      ↩︎ Checkpoint
    9. 9.
      “Binary classification, decision trees, GBT regression, Structured Streaming, custom transformer”
      ↩︎ Scoring in batch and streaming
    10. 10.
      “Model Serving provides a highly available and low-latency service for deploying models.”
      ↩︎ Spark-based scoring or Model Serving: choosing by inference type
      “Each model you serve is available as a REST API that you can integrate into your web or client application.”
      ↩︎ Spark-based scoring or Model Serving: choosing by inference type

    Ready to test yourself?

    Practise the 32 questions on this subdomain.

    Spotted a mistake, or was something unclear? Tell us.