Multiclass Classification Target Function

Choosing the single, most likely option of many candidates

⚠️

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 Multiclass Classification - a machine learning model that classifies entities or observations into one of multiple classes.

These models are set to choose only one of many options and are useful in many business scenarios, eg.:

  • Allocate a favorite brand to a customer, to run a personalized marketing campaign,
  • Classify a customer to particular segment, eg. creditworthiness for credit scoring,
  • Predict a subscription plan with the highest likelihood of customer opt-in,
  • Determine the trim level of the car the customer is most likely to buy, to make the most relevant offer.
  • Recommend an insurance or banking service most likely to be bought by a customer,
  • Categorize a product to a category, for efficient new products' listing process,
  • Classify a customer's complaint to a particular resolution category.

With multiclass classifiers businesses can tailor their strategies to target individuals with an option most likely to appeal to them, or to change their behavior in a desired way.

Standard Template for Multiclass 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 multiclass classification problems.

What is a customer's favorite brand?

We want the model to indicate the class (ie. brand) with the highest likelihood of purchase in the future. An ideal model would allow us to assign 1 for the correct (most liked) brand, and 0 for others. This is equivalent to providing an array of scores that can be interpreted as probability, summing to 1.

The steps to achieve that are as follow:

  • We filter future transactions to our list of target brands,
  • We then group them by brand, counting the instances of future purchases,
  • Finally, we normalize the resulting array, so that all scores sum to 1,
  • And output as 1-dimensional numpy array of float32 data type.

Click on the recipe below to see the entire code.

ℹ️


Which color of the clothing to display to a customer?

We intend to illustrate a promotional offer with a picture of clothing, or accessory, in a color most likely to appeal to our customer. Our model should point us to the class (ie. color) the customer is most likely to like. Since we can only feature one picture, a multiclass classifier is again the answer: we need an array of probability scores that sum to 1.

We do that like this:

  • We group future transactions by color,
  • Count the instances of future purchases in each group,
  • Normalize the resulting array, so that all scores sum to 1,
  • And output as 1-dimensional numpy array of float32 data type.

Click on the recipe below to see the complete function.

ℹ️