Databricks Machine Learning Associate Question 98
Single answerYou are working on a machine learning project in Databricks and have trained multiple models with different hyperparameters, logging each run to MLflow. You want to programmatically identify the best run based on the highest validation accuracy using the MLflow Client API. Which of the following code snippets correctly identifies the best run?
- A
best_run = client.search_runs(experiment_ids=['1'], order_by=['metrics.validation_accuracy DESC'])[0]
- B
best_run = max(client.search_runs(experiment_ids=['1']), key=lambda run: run.data.metrics['validation_accuracy'])
- C
best_run = client.list_run_infos(experiment_id='1')[0]
- D
best_run = client.get_run(run_id='best_val_accuracy')
Show answer and explanation
Correct answer: B
Explanation
To identify the best run using the MLflow Client API, you need to compare the desired metric ('validation_accuracy') across all runs. The search_runs method fetches all runs for a given experiment, and the max function with a lambda function on the metric ensures you identify the run with the highest 'validation_accuracy'. This is a common approach for programmatically selecting the best-performing model.
- A. Incorrect.
This code snippet sorts runs by 'validation_accuracy' in descending order using the 'order_by' parameter. However, sorting is only valid with 'search_runs', and the first result would indeed have the highest value. Despite this, the code assumes the experiment ID is '1' without variable context, and it doesn't handle cases where the metric might be missing.
- B. Correct.
This is the correct answer. It uses the
maxfunction with a lambda to extract the run with the highest 'validation_accuracy' from the list of runs returned bysearch_runs. This approach is robust and correctly handles the filtering logic. - C. Incorrect.
This code retrieves only metadata about runs using
list_run_infos, which does not include metric values like 'validation_accuracy'. Therefore, it cannot be used to identify the best run. - D. Incorrect.
This code snippet assumes there is a run ID named 'best_val_accuracy', but such an ID would not exist unless explicitly created. Additionally, this does not programmatically identify the best run based on the metric.