Track Your ML models as a Pro, Track them with MLflow.
By: Fernando López
Tags: Classification, Decision Tree, Scikit Learn, Tracking, Mlflow

Track Your ML models as a Pro, Track them with MLflow.

Photo by Victor Garcia on Unsplash

As a machine learning engineer or data scientist, most of your time is spent experimenting with machine learning models, for example adjusting parameters, comparing metrics, creating and saving visualizations, generating reports, etc. However, on many occasions we do not usually carry out this tracking in a healthy way. A healthy, simple and efficient way to carry out this tracking is by making use of tools that facilitate this type of activity, such is the case of MLflow.

In this blog, you will learn how to install, how track metrics, how to track parameters and how to save and reuse a scikit-learn ML model. So this blog is divided into the following sections:

What is MLflow?

Building a ML pipeline

Tracking a ML pipeline

Visualization

So, let’s get started!

What is MLflow?

MLflow is an open source platform to manage the ML lifecycle, including experimentation, reproducibility, deployment, and a central model registry [1]. MLflow is python-based, you can find the source code here. So, essentially MLflow offers:

Figure 1. MLflow capabilities | Image by Author | Idea taken from source

Basically MLflow offers a set of tools to make our life of machine learning engineer or data scientist more efficient and effective.

In this blog we will focus on the part of tracking the parameters and metrics as well as generating a model, saving it and loading it to be used again. So let’s move on to the installation!

Since MLflow is python-based the installation is quite easy, you can do it with pip, such as:

pip install mlflow

Great, assuming the installation was successful, we are going to build a simple pipeline in which we are going to train a decision tree to generate predictions on the mushroom dataset. So let’s go for it!

You can find the complete implementation here: https://github.com/FernandoLpz/Tracking-ML-model-MLflow

Building a ML pipeline

As we mentioned, we are going to build a machine learning model using the mushroom dataset (you can find the dataset here: source). So the pipeline will have the following form:

Figure 2. Pipeline | Image by author

In each part of the pipeline we are going to perform specific tasks such as:

Load data : Read the csv file and load it into a pandas dataframe

: Read the csv file and load it into a pandas dataframe Preprocessing : Remove unnecessary columns and apply one-hot encoding to transform categorical data into a numeric format.

: Remove unnecessary columns and apply one-hot encoding to transform categorical data into a numeric format. Split data: Given the entire dataset, it will be generated the train and test sets.

Given the entire dataset, it will be generated the train and test sets. Parameters tuning : By means of a grid search, the optimal parameters for the decision tree will be obtained, subsequently k-fold cross validation will be applied to observe the behavior of the model with the optimal parameters.

: By means of a grid search, the optimal parameters for the decision tree will be obtained, subsequently k-fold cross validation will be applied to observe the behavior of the model with the optimal parameters. Model evaluation: Given the optimal parameters, the model will be tested by using the test data to calculate in order to calculate the accuracy.

Then, the specific tasks of each component of the pipeline are described as follows: