Multi-label Classification Target Function

Assigning multiple labels to the same entity

⚠️

Check This First!

This article refers to BaseModel accessed via Docker container. Please refer to Snowflake Native App section if you are using BaseModel as SF GUI application.


In this subpage we will look at some examples of target functions for Multi-label Classification - a machine learning model that can assign multiple labels to a single data point.

These models help to address scenarios where more than one correct answer is possible, eg.:

  • Predict customer's buying propensity of multiple products to personalize a campaign,
  • Predict services a customer is likely to purchase, to construct an attractive bundle,
  • Recommend multiple game genres a player might enjoy.
  • Tag reviews with multiple sentiments and aspects.
  • Assign multiple attributes to products, eg. dietary regimens.
  • Assess creditworthiness based on multiple financial behaviors for credit scoring.

Standard Template for Multi-label Classification Target Functions

Each function for classification problem will:

  • accept as parameters history, future, entity and ctx, as described here,
  • perform some transformation on these inputs as explained in this section
  • output a one-dimensional numpy array of float32 data type and with a size equal to number of classes.

def target_fn(_history: Events, future: Events, _entity: Attributes, _ctx: Dict) -> np.ndarray:
    
    # transformation of events into the desired target

    return np.array(target, dtype=np.float32)

We will now explore the transformations looking at two examples of functions for multi-label classification problems.

Will a customer opt in for a service add-on?

An ideal model would return '1' for every service add-on the customer will buy, and 0 for others.
This is the target we want to give the model to train - and that's how we implement it in a code:

  • Specify target service names and the column name where the entity should be looked up,
  • Group all future transaction by that entity (= service add-on, a class to predict) with groupBy(),
  • Use exists() to flag the groups with predicted interactions as 1, and others as 0,
  • Exclude all customer who did not buy any add-on from the output.

Click on the recipe below to see the entire code.

ℹ️


Which product categories is the customer likely to buy?

This example is similar to the above one, so we will add one more complication: the product information such as name, category etc. are stored in a separate data source (articles) and were joined by the configuration in YAML file at foundation model training stage. We need one more extra step in order to enable this information to be used by the function:

  • Specify target product categories names
  • Enable the function to access this information with get_qualified_column_name(), pointing to column and data source name
  • Group all future transactions by that column (= "department", a class to predict) with groupBy,
  • Use exists() to flag the groups with predicted interactions as 1, and others as 0,
  • Exclude all customer who did not buy any products in these departments from the output.

Click on the recipe below to see the entire code.

ℹ️