Reacting to Existing vs New Cases#

Objectives: what you will take away#

  • Definitions & an understanding how to react on existing vs. new cases.

Prerequisites: before you begin#

Data#

Our example dataset for this guide is the well-known Adult dataset, accessible via the pmlb package installed in the prerequisites using the fetch_data() function.

Concepts & Terminology#

How-To Guide#

Setup#

The user guide assumes you have created and setup a Trainee as demonstrated in basic workflow. The Trainee will be referenced as trainee in the sections below.

New Cases#

Using Trainee.react() with new cases is straightforward and simple. This is often the default use case of most workflows. When cases are passed into react, Trainee.react(), it acts upon the cases as if they were new cases.

results = trainee.react(
    test_case[context_features],
    context_features=context_features,
    action_features=action_features,
)

Existing Cases#

In certain workflows, you might want to examine cases already trained into the trainee. To do this, a list of cases must be passed into the case_indices parameter in Trainee.react(). This list passed into case_indices must consist of iterables of cases where the first value is the session_id and the second value is the session_training_index.

To get the session_id, Trainee.get_sessions() retrieves the available sessions. Once you have the session_id, using Trainee.get_cases() with the session_id allows you to see the session_training_index, where are the indexes of the returning dataframe.

# Get data from the first session
session = trainee.get_sessions()[0]['id']

# See the case indices
trainee.get_cases(session='db3ebfc0-0bb3-4b33-b475-7ca7d21267e2')

# React to the first case in this session
case = [(session, 1)]

Once you have the case(s) in the right format, then they must be passed into the case_indices parameter. Generally, leave_case_out is set to True to indicate that this case must be excluded when reacting. This is typically desired behavior because we don’t want the react to be skewed by an identical case.

preserve_feature_values is also generally set to the context features to preserve their values.

new_result = trainee.react(
    case_indices=case,
    preserve_feature_values=context_features,
    leave_case_out=True
    action_features=action_features
)

API References#