MLflow Part 1: Getting Started with MLflow! | by David Hundley | Towards Data Science

Hello again friends! We’re back here with another quick tip, and because I do attempt to keep these posts quick, this is actually going to be part one in a series of tips related to MLFlow. In the spirit of full transparency, MLFlow is pretty new to me, so I’m going to be learning things alongside you all over the next few weeks. If you’d like to follow along with my code, check out this link to my correlated GitHub repository.
I’m sure the first question on your mind is, what is MLflow? Simply put, MLflow is an open source platform designed to help streamline the machine learning lifecycle process. Again, I’m still learning all it does, but it seems to offer a lot of promising features that I’m excited to explore within future posts. These things range from creating a model registry, easy deployment of models as APIs, and more. I honestly don’t know how long this sub-series will go, but I imagine we’re going to get a lot out of this neat tool!
But of course, we all gotta start somewhere. We’re going to take baby steps into this world to get ourselves prepped properly for future posts. That said, we’re going to take a pretty barebones approach to MLflow with creating and logging a very simple model. Before getting started, we’ll need to install MLflow, which is as simple as running the following pip command:
pip install mlflow
In the last few posts, we’ve been working with the Titanic dataset, but this time, we’re going to deviate over to using the classic Red Wine Quality dataset 🍷 . Now, I’m personally not a wine drinker (nor alcohol drinker in general), but for you wine connoisseurs out there, I think you’ll enjoy this dataset equally as much as Titanic, if not more since it’s not based on a huge tragedy!
The reason we’re changing to this dataset is because all the features in this dataset are numerical in nature, so we don’t really have to do any feature engineering in order to get it fit into a model. Granted, I will not at all guarantee that we’ll get quality results, but at least we can get a simple model up and running without the fuss of data cleansing. The only thing we’ll do is import the data and appropriately split it between training and validation sets like so:
# Loading data and prepping for trainingdf_wine = pd.read_csv('../data/wine/train.csv')X = df_wine.drop(columns = 'quality')y = df_wine[['quality']]X_train, X_val, y_train, y_val = train_test_split(X, y, random_state = 42)
(Oh, by the way, I feel the need to say that today’s post is more or less a copy of MLflow’s first tutorial, but I intentionally simplified it a lot and am explaining my simplifaction more thoroughly here.)
Now is when we start stepping into the world of MLflow. I’ll go ahead and paste the remainder of the script below and explain what each piece is doing throughout.
# Defining model parametersalpha = 1l1_ratio = 1# Running MLFlow scriptwith mlflow.start_run():# Instantiating model with model parameters    model = ElasticNet(alpha = alpha,                       l1_ratio = l1_ratio)# Fitting training data to the model    model.fit(X_train, y_train)# Running prediction on validation dataset    preds = model.predict(X_val)# Getting metrics on the validation dataset    rmse = mean_squared_error(preds, y_val)    abs_error = mean_absolute_error(preds, y_val)    r2 = r2_score(preds, y_val)# Logging params and metrics to MLFlow    mlflow.log_param('alpha', alpha)    mlflow.log_param('l1_ratio', l1_ratio)    mlflow.log_metric('rmse', rmse)    mlflow.log_metric('abs_error', abs_error)    mlflow.log_metric('r2', r2)# Logging model to MLFlow    mlflow.sklearn.log_model(model, 'model')
Under “mlflow.start_run()”, the first few lines of code should look very familiar to you already. Here we’re simply instantiating a model with the provided parameters, fitting the training data on the parameters, and then getting the predicted results with our newly trained model with the validation dataset. With our predicted results and actual results noted from the validation dataset, we then get appropriate metrics surrounding the root mean squared error, mean absolute error, and R2 score. So far, nothing really new here.
Now is when we start stepping into some new stuff. Fortunately, I think simply reading the code itself is pretty self explanatory. It’s logging time, friends! In our code, we’re going to be logging three different things: the parameters of the model, the metrics from the validation set, and the model itself. The reason for this is going to become quickly apparent as we explore the UI down below.
After you run this code, you’ll notice a new directory called “mlruns” should appear adjacent to where you ran your code. When you first glance at it, it might look weird, but if you dig deep down far enough, you’ll get to the stuff you actually care about. Notably, you should be able to dig up everything you just logged, including the serialized model artifact itself.
Now before we move onto the next section, I want to take note that I ran this code both in a Jupyter notebook and in a basic Python script. You don’t need to do this, but it’ll make sense down below why I’m noting that now.
Alright, now here comes the fun part! In your command line, navigate to the directory above “mlruns”. (If you just ran the Python script, you should already be there.) Run the following command, and you should see the following screen:
mlflow ui
If you’re like me, this interface might look familiar to you as it appears to be using Gunicorn behind the scenes to serve out this UI. As you’ll notice in my case, it looks like the UI is being served on localhost:5000. Fire up your favorite browser and navigate on over to that address. This is the screen you should see.
Neat, huh? Notice within that table that there are two runs showing. If you look at the source of those runs, you’ll notice that one was run from my mlflow-wine.py Python script while the other was run from an iPykernel, which of course is my Jupyter notebook! Additionally, you’ll see our model parameters listed in the next few columns, and finally you can see the model metrics in the last set of columns.
Next, go ahead and open one of those models by clicking one of the hyperlinks under “Start Time”. This is what you should see.
Again, we see things like our model parameters and validation metrics, but we also see other additional things here, like the duration and associated git commit. I couldn’t capture everything in a single screenshot, but if you were to scroll down from here, you’d also be able to navigate to the model artifacts themselves directly from the UI, which I think is just too darned cool!
And that wraps it up for this first introduction! We’re barely scratching the surface here, so I look forward to learning alongside you all in the next few posts as we continue to explore MLflow together. Hope you enjoyed this quick intro! See you all again soon. 😃