Residuals#

Objectives: what you will take away#

  • How-To Retrieve global and local residuals.

Prerequisites: before you begin#

Data#

Our example dataset for this recipe is the well known Adult dataset. It is accessible via the pmlb package installed earlier. We use the fetch_data() function to retrieve the dataset in Step 1 below.

Concepts & Terminology#

How-To Guide#

Setup#

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

Local Residuals#

Local metrics are retrieved through using Trainee.react(). Both Robust and non-robust (full) versions are available.

# Get robust residuals
details = {'feature_residuals_full': True}

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

residuals = results['details']['feature_residuals_full']

Global Residuals#

Howso has the ability to retrieve both local vs global metrics. Global metrics are retrieved through using Trainee.react_aggregate(). Both Robust and non-robust (full) versions are also available.

residuals = trainee.react_react_aggregate(
    action_feature=action_features[0],
    details={'feature_residuals_full': True}
)

Complete Code#

The code from all of the steps in this guide is combined below:

from pmlb import fetch_data

from howso.engine import Trainee
from howso.utilities import infer_feature_attributes

# import data
df = fetch_data('adult')

# Subsample the data to ensure the example runs quickly
df = df.sample(1001)
# Split out the last row for a test case and drop the Action Feature
test_case = df.iloc[[-1]].copy()
df.drop(df.index[-1], inplace=True)
test_case = test_case.drop('target', axis=1)


features = infer_feature_attributes(df)

action_features = ['target']
context_features = features.get_names(without=action_features)

trainee = Trainee(features=features)

trainee.train(df)

trainee.analyze(context_features=context_features, action_features=action_features)

# Get local robust residuals
details = {'feature_residuals_full': True}

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

residuals = results['details']['feature_residuals_full']

# Get global robust residuals
residuals = trainee.react_react_aggregate(
    action_feature=action_features[0],
    details={'feature_residuals_full': True}
)

API References#