Create an Experiment¶
An "experiment" in MLflow is like a specific chapter in your research book. It's an organized set of steps and records that detail how you've tested a particular approach in your machine learning project. Each experiment in MLflow includes the code you've used, the configured parameters, the input data, the results obtained, and any additional notes you want to add.
For example, if you are developing a model to predict house prices, an experiment in MLflow might involve training the model with a specific dataset, adjusting parameters like the number of trees in a random forest model, recording performance metrics like mean squared error (MSE), and making notes about any significant observations. Every time you try a different set of parameters or data, or even change the algorithm, you can create a new experiment in MLflow to maintain a clear record of your research and results. This makes it easier to track your progress and make informed decisions in your machine learning project.
Create an Experiment¶
Creating an experiment in MLflow is simple. You just need to provide a name for the experiment, and MLflow will take care of the rest. If you want to create a new experiment, you can use the mlflow.create_experiment
function. This function will create a new experiment with the specified name and return its ID. If an experiment with the same name already exists, it will raise an exception.
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # change this to your experiment name
# Create an experiment
experiment_id = mlflow.create_experiment(EXPERIMENT_NAME)
Get an existing experiment¶
It is possible to get an existing experiment by using the get_experiment_by_name
method. If the experiment does not exist, it will return None
.
experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)
We can see the details of the experiment by printing some of its attributes.
print(f"Name: {experiment.name}")
print(f"Experiment_id: {experiment.experiment_id}")
print(f"Artifact Location: {experiment.artifact_location}")
print(f"Tags: {experiment.tags}")
print(f"Lifecycle_stage: {experiment.lifecycle_stage}")
Name: mlflow-demo Experiment_id: 133178755694431312 Artifact Location: mlflow-artifacts:/133178755694431312 Tags: {} Lifecycle_stage: active