Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Manage ML Model Stages with Python
📖 Scenario: You work in a team that builds machine learning models. Each model can be in one of three stages: staging (testing), production (live use), or archived (old versions).You want to organize your models by their stages so the team can easily find and update them.
🎯 Goal: Create a Python dictionary to store model names and their stages. Then, filter models by stage and finally print the models in production.
📋 What You'll Learn
Create a dictionary called models with exact model names and stages
Create a variable called target_stage set to the string 'production'
Use a dictionary comprehension to create a new dictionary called filtered_models that includes only models in target_stage
Print the filtered_models dictionary
💡 Why This Matters
🌍 Real World
Teams managing machine learning models often track which models are being tested, which are live, and which are archived. Organizing models by stage helps with deployment and maintenance.
💼 Career
Understanding how to manage model stages is important for MLOps engineers and data scientists to keep production systems stable and organized.
Progress0 / 4 steps
1
Create the initial models dictionary
Create a dictionary called models with these exact entries: 'modelA': 'staging', 'modelB': 'production', 'modelC': 'archived', 'modelD': 'production'
MLOps
Hint
Use curly braces {} to create a dictionary with keys as model names and values as their stages.
2
Set the target stage variable
Create a variable called target_stage and set it to the string 'production'
MLOps
Hint
Assign the string 'production' to the variable target_stage.
3
Filter models by the target stage
Use a dictionary comprehension to create a new dictionary called filtered_models that includes only the models from models where the stage equals target_stage
MLOps
Hint
Use {name: stage for name, stage in models.items() if stage == target_stage} to filter the dictionary.
4
Print the filtered models
Write a print statement to display the filtered_models dictionary
MLOps
Hint
Use print(filtered_models) to show the filtered dictionary.
Practice
(1/5)
1. What is the primary purpose of the staging stage in model lifecycle management?
easy
A. To deploy models for live user traffic
B. To test and validate models before they go live
C. To permanently delete old models
D. To archive models for long-term storage
Solution
Step 1: Understand model stages
The staging stage is used to test and validate models before they are deployed to production.
Step 2: Differentiate from other stages
Production is for live use, archived is for storage, so staging is the testing phase.
Final Answer:
To test and validate models before they go live -> Option B
Quick Check:
Staging = Testing phase [OK]
Hint: Staging means testing before live use [OK]
Common Mistakes:
Confusing staging with production
Thinking archived means active use
Assuming staging is for deleting models
2. Which MLflow command correctly transitions a model version to the production stage?
easy
A. mlflow models transition-version --model-name my_model --version 3 --stage production
B. mlflow model transition-version --model-name my_model --version 3 --to-stage production
C. mlflow models transition-version --model-name my_model --model-version 3 --stage production
D. mlflow models transition --model-name my_model --model-version 3 --stage production
Solution
Step 1: Recall MLflow CLI syntax
The correct MLflow CLI command to transition a model version is mlflow models transition-version with flags --model-name, --version and --stage.
Step 2: Identify correct flags and command
mlflow models transition-version --model-name my_model --version 3 --stage production matches the correct syntax.
Final Answer:
mlflow models transition-version --model-name my_model --version 3 --stage production -> Option A
The method transition_model_version_stage changes the stage of a model version to the specified stage.
Step 2: Check the stage argument
The stage argument is set to "archived", so the model version 2 of "my_model" will be moved to archived stage.
Final Answer:
Archived -> Option A
Quick Check:
transition_model_version_stage with stage='archived' = Archived [OK]
Hint: Stage argument sets the model's new stage directly [OK]
Common Mistakes:
Assuming default stage if not specified
Confusing method name with other MLflow functions
Thinking 'archived' means deletion
4. You run this MLflow CLI command but get an error:
mlflow models transition-version --version 5 --stage production
What is the most likely cause?
medium
A. The command should be mlflow model transition-version (singular 'model')
B. Incorrect stage name; should be 'prod' instead of 'production'
C. Version number must be a string, not a number
D. Missing the --model-name argument
Solution
Step 1: Check required arguments for transition-version
The mlflow models transition-version command requires the --model-name argument to specify which model to update.
Step 2: Identify missing argument
The command lacks --model-name, causing the error.
Final Answer:
Missing the --model-name argument -> Option D
Quick Check:
Missing required flags cause errors [OK]
Hint: Always include --model-name when transitioning versions [OK]
Common Mistakes:
Assuming stage names are abbreviated
Using singular 'model' instead of 'models'
Passing version as string instead of number (both accepted)
5. You have two model versions: v1 in production and v2 in staging. You want to promote v2 to production and archive v1 using MLflow Python API. Which sequence of calls correctly achieves this?
hard
A. client.transition_model_version_stage('model', 2, 'archived') then client.transition_model_version_stage('model', 1, 'production')
B. client.transition_model_version_stage('model', 2, 'production') then client.transition_model_version_stage('model', 2, 'archived')
C. client.transition_model_version_stage('model', 1, 'archived') then client.transition_model_version_stage('model', 2, 'production')
D. client.transition_model_version_stage('model', 1, 'production') then client.transition_model_version_stage('model', 2, 'staging')
Solution
Step 1: Archive current production version first
To free the production stage, first move v1 from production to archived.
Step 2: Promote staging version to production
After archiving v1, promote v2 from staging to production.
Final Answer:
Archive v1 then promote v2 -> Option C
Quick Check:
Archive old before promoting new [OK]
Hint: Archive old production before promoting new [OK]
Common Mistakes:
Promoting new before archiving old causes stage conflict