Residuals#
Objectives: what you will take away#
How-To Retrieve global and local residuals.
Prerequisites: before you begin#
You’ve successfully installed Howso Engine
You have an understanding of Howso’s basic workflow.
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.
[1]:
import pandas as pd
from pmlb import fetch_data
from howso.engine import Trainee
from howso.utilities import infer_feature_attributes
df = fetch_data('adult').sample(1_000)
features = infer_feature_attributes(df)
trainee = Trainee(features=features)
trainee.train(df)
trainee.analyze()
features.to_dataframe()
[1]:
| type | decimal_places | bounds | data_type | original_type | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| min | max | allow_null | observed_min | observed_max | data_type | size | ||||
| age | continuous | 0 | 0.0 | 137.0 | True | 17.0 | 90.0 | number | numeric | 8 |
| workclass | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| fnlwgt | continuous | 0 | 0.0 | 1590234.0 | True | 19896.0 | 972354.0 | number | numeric | 8 |
| education | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| education-num | continuous | 0 | 0.0 | 26.0 | True | 1.0 | 16.0 | number | numeric | 8 |
| marital-status | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| occupation | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| relationship | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| race | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| sex | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| capital-gain | continuous | 0 | 0.0 | 164870.0 | True | 0.0 | 99999.0 | number | numeric | 8 |
| capital-loss | continuous | 0 | 0.0 | 6216.0 | True | 0.0 | 3770.0 | number | numeric | 8 |
| hours-per-week | continuous | 0 | 0.0 | 162.0 | True | 2.0 | 99.0 | number | numeric | 8 |
| native-country | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
| target | nominal | 0 | NaN | NaN | False | NaN | NaN | number | integer | 8 |
Local Residuals#
Local metrics are retrieved through using Trainee.react().
Both Robust and non-robust (full) versions are available, although full
is recommended for residuals.
[2]:
# Get local full residuals
details = {'feature_full_residuals_for_case': True}
results = trainee.react(
df.iloc[[-1]],
context_features=features.get_names(without=["target"]),
action_features=["target"],
details=details
)
residuals = results['details']['feature_full_residuals_for_case']
residuals
[2]:
| fnlwgt | marital-status | target | race | capital-loss | age | education | occupation | sex | hours-per-week | workclass | education-num | relationship | capital-gain | native-country | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | [152775.59743629294, 0] | [0, 0] | [0.291982313854123, 0] | [0.9307332621982874, 0] | [78.93948758118927, 0] | [13.794169342356668, 0] | [0, 0] | [0.8456927249679234, 0] | [0, 0] | [0.014684201390615215, 0] | [0.06518279565913454, 0] | [1.7763568394002505e-15, 0] | [0, 0] | [239.1724376805735, 0] | [0, 0] |
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.
[3]:
# Get global full residuals
residuals = trainee.react_aggregate(
details={'feature_full_residuals': True},
).to_dataframe()
residuals
[3]:
| fnlwgt | marital-status | target | race | capital-loss | age | education | occupation | sex | workclass | hours-per-week | education-num | relationship | capital-gain | native-country | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| feature_full_residuals | [80702.38340740163, 0] | [0.24972513297818139, 0] | [0.22142206109226897, 0] | [0.20782668520488648, 0] | [149.8266306601822, 0] | [8.43773094129456, 0] | [0, 0] | [0.7729749032231602, 0] | [0.2441306514878831, 0] | [0.37222984205566645, 0] | [7.878912166286146, 0] | [0.49593079559383846, 0] | [0.36449702912475407, 0] | [1277.1246434946027, 0] | [0.1734581778544932, 0] |