Get metric history from MLflow in Python

tl;dr

import pandas as pd
import mlflow

def get_metric_history(run_id, metric):
    client = mlflow.tracking.MlflowClient()
    history = client.get_metric_history(run_id, metric)
    history = [dict(key=m.key, value=m.value, timestamp=m.timestamp, step=m.step) for m in history]
    history = pd.DataFrame(history).sort_values("step")
    history.timestamp = pd.to_datetime(history.timestamp, unit="ms")
    return history

train_loss = get_metric_history(run_id, "train_loss")
valid_loss = get_metric_history(run_id, "valid_loss")
history = pd.concat((train_loss, valid_loss))
history.pivot(index="step", columns="key", values="value").plot()

How to If you want to interact with the server via MLFlow, use mlflow.tracking.MlflowClient.

You can use MlflowClient.get_metric_history (run_id, key) to get all the metric history of a key in a run. Here is a function that uses this to get the metric history as pandas.DataFrame. Timestamp is easier to handle if converted to datetime.

def get_metric_history(run_id, metric):
    client = mlflow.tracking.MlflowClient()
    history = client.get_metric_history(run_id, metric)
    history = [dict(key=m.key, value=m.value, timestamp=m.timestamp, step=m.step) for m in history]
    history = pd.DataFrame(history).sort_values("step")
    history.timestamp = pd.to_datetime(history.timestamp, unit="ms")
    return history

train_loss = get_metric_history(run_id, "train_loss")

As a side note, if you want to compare multiple metric and plot, you can easily do it by using pivot after combining the Dataframes vertically as shown below.

train_loss = get_metric_history(run_id, "train_loss")
valid_loss = get_metric_history(run_id, "valid_loss")
history = pd.concat((train_loss, valid_loss))
history.pivot(index="step", columns="key", values="value").plot()

Recommended Posts

Get metric history from MLflow in Python
Get data from Quandl in Python
Get exchange rates from open exchange rates in Python
Get Precipitation Probability from XML in Python
Get date in Python
Get time series data from k-db.com in Python
[Python] Get one year's message history from Slack
Get only articles from web pages in Python
Get YouTube Comments in Python
Get last month in python
OCR from PDF in Python
Get data from GPS module at 10Hz in Python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Get the value while specifying the default value from dict in Python
Hit REST in Python to get data from New Relic
Get macro constants from C (++) header file (.h) in Python
Get message from first offset with kafka consumer in python
Extract text from images in Python
Get, post communication memo in Python
Get upcoming weather from python weather api
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Extract strings from files in Python
How to get a string from a command line argument in python
Get additional data in LDAP with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Get Suica balance in Python (using libpafe)
Get keystrokes from / dev / input (python evdev)
Revived from "no internet access" in Python
Prevent double boot from cron in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Download images from URL list in Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Generate a class from a string in Python
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Convert from Markdown to HTML in Python
Get Cloud Logging available in Python in 10 minutes
[Python] Get the main color from the screenshot
Get your own IP address in Python
How to get a value from a parameter store in lambda (using python)
Get options in Python from both JSON files and command line arguments
Python in optimization
CURL in python
Get the contents of git diff from python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Meta-analysis in Python
Unittest in python
Get compliments from new girls in C # paizahack_01