Adding a Custom Metric through NannyML SDK

Adding Custom Metrics programmatically through NannML SDK

The SDK Custom Metrics is part of the monitoring class and it can be created by instantiating a new nml_sdk.monitoring.CustomMetric(). Before all, you will need to set up the NannyML SDK address and your token. You can see how to do this on the Cloud SDK Getting Started page.

import nannyml_cloud_sdk as nml_sdk

## First, authenticate to NannyML cloud
nml_sdk.url = "https://beta.app.nannyml.com"
nml_sdk.api_token = r"api token goes here"

## Then create a new custom metric instance
custom_metric = nml_sdk.monitoring.CustomMetric()

The class CustomMetric allows the user to perform 4 main actions:

  • Create a new custom metric

  • List all the existing custom metrics

  • Get the details of a custom metric

  • Delete a custom metric

Create a custom metric

The custom metric function

To create a new custom metric you need to provide a python function or a string containing a python function that receives a list of named arguments and returns a value. This python function needs to be named either calculate, estimate, aggregate or loss depending on the problem type this function serves.

For example, the calculate function, as seen below, is used by Binary Classification or Multiclass Classification.

Passing a function

Passing a string

The calculate function is expected to receive a set of arguments and return a float value. This means that when this function is called a list of arguments will be passed to the function and a float value is returned. Not providing a function that accepts the correct arguments and returns the float value will generate errors during the execution.

For the calculate function, some of the arguments being passed require the pandas library to be imported.

Creating the custom metric

After defining a function, you can call custom_metric.create to register your custom function as a custom metric. Here the custom metric is being created as a Binary Classification and passing just the calculate function.

If the provided function for the custom metric is not a valid python function, i.e. there is a syntax error in your function, an error will be thrown:

Custom metric problem types

If you need a custom metric for Binary Classification or Multiclass Classification you need to provide the calculate function and you can also pass an optional estimate function. The estimate function has the following structure:

The import statements needs to be repeated on every custom function defined.

The custom_metric.create function for the Binary Classification or Multiclass Classification is called like this:

If you need a custom metric for a Regression problem type, instead of an estimate or calculate function you need to provide an aggregate and a loss function.

The Aggregate function structure

The Loss function structure:

Then creating the function would be like:

Assign custom metric to a model

A new custom metric, when created, is not assigned to any model. To assign this custom metric to a model, first you will need to retrieve the unique identifier of a model (model_id) and then call monitoring.Model.add_custom_metric passing the model_id and the metric_id as parameters.

Retrieving the model_id can be done by calling nml_sdk.monitoring.Model.list(). This function either lists all the models available of filter the models by name or problem type. The return will be an array of dictionaries, the model_id will be the value of the id key of a dictionary.

From now on, every time you run your model "Model1", the new custom metric will be calculated among the standard metrics.

A custom metric can be assigned to any existing model as long as their problem types matches.

Listing the custom metrics

It is possible to list the existing custom metrics filtering by name and problem type:

Listing a custom metric doesn't expose the functions implementation. To check the code inside the custom metrics you need to run the custom_metric.get function.

Getting custom metrics details

The function implementations here are converted to a raw one-line string. Line breaks are replaced by '\n' and possibly tabs will be replaced by '\t' if your test editor does not use spaces.

Removing a custom metric from a model

Just like it is possible to assign a custom metric to a model, you can also remove a custom metrics from a model:

After removing a custom metric from a model, this custom metric will not be calculated anymore when running this model and its previous results will not be shown anymore.

Deleting a custom metric

If a custom metric is not necessary anymore, it is possible to delete it.

SDK custom metrics end-to-end examples

Binary classification

This is an example of a custom F_2 function. Note that it is possible to add external libraries to the custom code. In this example, the fbeta_score from sklearn.metrics will be used. For more context on custom metrics for binary classification, you can refer to the tutorial Writing functions for Binary Classification where the concept of the calculate and estimate functions are better defined.

Multiclass Classification

This is an example of a custom F_2 Multiclass classification function. For more context on custom metrics for binary classification you can refer to the tutorial Writing Function for Multiclass Classification where the concept of the calculate and estimate functions are better defined.

Regression

To define a Regression custom metric, you need to set up a loss function and an aggregate function. Those functions are needed to calculate realized performance and estimate performance. Please refer to the document Writing Functions for Regression where the concept of the loss and aggregate functions are better defined.