docs/docs/classic-ml/tracking/quickstart/index.mdx
import ImageBox from "@site/src/components/ImageBox";
:::tip[MLflow Assistant] Need help setting up tracking? Try <ins>MLflow Assistant</ins> - a powerful AI assistant that can help you set up MLflow tracking for your project. :::
The purpose of this quickstart is to provide a quick guide to the most essential core APIs of MLflow Tracking. In just a few minutes of following along with this quickstart, you will learn:
MLflow is available on PyPI. If you don't already have it installed on your system, you can install it with:
pip install mlflow
Then, follow the instructions in the Set Up MLflow guide to set up MLflow.
If you just want to start super quick, run the following code in a notebook cell:
import mlflow
mlflow.set_experiment("MLflow Quickstart")
Before training our first model, let's prepare the training data and model hyperparameters.
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"random_state": 8888,
}
In this step, we train the model on the training data loaded in the previous step, and log the model and its metadata to MLflow. The easiest way to do this is to using MLflow's Autologging feature.
import mlflow
# Enable autologging for scikit-learn
mlflow.sklearn.autolog()
# Just train the model normally
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
With just one line of additional code mlflow.sklearn.autolog(), now you get the best of both worlds: you can focus on training the model, and MLflow will take care of the rest:
To learn more about autologging and supported libraries, see the Autologging documentation.
To see the results of training, you can access the MLflow UI by navigating to the URL of the Tracking Server. If you have not started one, open a new terminal and run the following command at the root of the MLflow project and access the UI at http://localhost:5000 (or the port number you specified).
mlflow server --port 5000
When opening the site, you will see a screen similar to the following:
<ImageBox src="/images/tutorials/introductory/quickstart-tracking/quickstart-ui-home.png" alt="MLflow UI Home page" />The "Experiments" section shows a list of (recently created) experiments. Click on the "MLflow Quickstart" experiment.
<ImageBox src="/images/tutorials/introductory/quickstart-tracking/quickstart-ui-run-list.png" alt="MLflow UI Run list page" />The training Run created by MLflow is listed in the table. Click the run to view the details.
<ImageBox src="/images/tutorials/introductory/quickstart-tracking/quickstart-our-run.png" alt="MLflow UI Run detail page" />The Run detail page shows an overview of the run, its recorded metrics, hyper-parameters, tags, and more. Play around with the UI to see the different views and features.
Scroll down to the "Model" section and you will see the model that was logged during training. Click on the model to view the details.
<ImageBox src="/images/tutorials/introductory/quickstart-tracking/quickstart-ui-logged-models.png" alt="MLflow UI Model detail page" />The model page displays similar metadata such as performance metrics and hyper-parameters. It also includes an "Artifacts" section that lists the files that were logged during training. You can also see environment information such as the Python version and dependencies, which are stored for reproducibility.
<ImageBox src="/images/tutorials/introductory/quickstart-tracking/quickstart-our-model.png" alt="MLflow UI Model detail page" />Now that we've learned how to log a model training run with MLflow autologging, let's step further and learn how to log a model and metadata manually. This is useful when you want to have more control over the logging process.
The steps that we will take are:
# Start an MLflow run
with mlflow.start_run():
# Log the hyperparameters
mlflow.log_params(params)
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Log the model
model_info = mlflow.sklearn.log_model(sk_model=lr, name="iris_model")
# Predict on the test set, compute and log the loss metric
y_pred = lr.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
mlflow.log_metric("accuracy", accuracy)
# Optional: Set a tag that we can use to remind ourselves what this run was for
mlflow.set_tag("Training Info", "Basic LR model for iris data")
After logging the model, we can perform inference by:
pyfunc flavor.:::info
To load the model as native scikit-learn model, use mlflow.sklearn.load_model(model_info.model_uri) instead of the pyfunc flavor.
:::
# Load the model back for predictions as a generic Python Function model
loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
predictions = loaded_model.predict(X_test)
iris_feature_names = datasets.load_iris().feature_names
result = pd.DataFrame(X_test, columns=iris_feature_names)
result["actual_class"] = y_test
result["predicted_class"] = predictions
result[:4]
The output of this code will look something like this:
<table> <thead> <tr> <th>sepal length (cm)</th> <th>sepal width (cm)</th> <th>petal length (cm)</th> <th>petal width (cm)</th> <th>actual_class</th> <th>predicted_class</th> </tr> </thead> <tbody> <tr> <td>6.1</td> <td>2.8</td> <td>4.7</td> <td>1.2</td> <td>1</td> <td>1</td> </tr> <tr> <td>5.7</td> <td>3.8</td> <td>1.7</td> <td>0.3</td> <td>0</td> <td>0</td> </tr> <tr> <td>7.7</td> <td>2.6</td> <td>6.9</td> <td>2.3</td> <td>2</td> <td>2</td> </tr> <tr> <td>6.0</td> <td>2.9</td> <td>4.5</td> <td>1.5</td> <td>1</td> <td>1</td> </tr> </tbody> </table>Congratulations on working through the MLflow Tracking Quickstart! You should now have a basic understanding of how to use the MLflow Tracking API to log models.