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 versions are available.

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

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

residuals = results['details']['robust_residuals']

Global Residuals#

Howso has the ability to retrieve both local vs global metrics. Global metrics are retrieved through using Trainee.react_into_trainee(). Both robust and non-robust versions are also available. For global residuals, they are retrieved by selecting the mean absolute error, mae, stat in Trainee.get_prediction_stats().

t.react_into_trainee(
    context_features=context_features,
    action_feature=action_features[0],
    residuals_robust=True
)
residuals = t.get_prediction_stats(
    action_feature=action_features[0],
    stats=['mae']
)

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 = {
    'robust_residuals': True,
    'feature_residuals': True
}

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

residuals = results['details']['feature_residuals']

# Get global robust residuals
trainee.react_into_trainee(
    context_features=context_features,
    action_feature=action_features[0],
    residuals_robust=True
)
residuals = trainee.get_prediction_stats(
    action_feature=action_features[0],
    stats=['mae']
)

API References#