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()
/home/docs/checkouts/readthedocs.org/user_builds/diveplane-howso-docs/envs/latest/lib/python3.11/site-packages/howso/utilities/feature_attributes/pandas.py:145: UserWarning: You have one or more suggestions to consider for your feature attributes configuration. Please view them by printing the `suggestions` property of your returned feature attributes object (`your_attributes_object.suggestions`).
warnings.warn(suggestion_warning, UserWarning)
[1]:
| type | decimal_places | bounds | data_type | original_type | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| min | max | allow_null | observed_min | observed_max | nulls_observed | data_type | size | ||||
| age | continuous | 0 | 0.0 | 137.0 | True | 17.0 | 90.0 | False | number | numeric | 8 |
| workclass | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| fnlwgt | continuous | 0 | 0.0 | 1944119.0 | True | 13862.0 | 1184622.0 | False | number | numeric | 8 |
| education | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| education-num | continuous | 0 | 0.0 | 26.0 | True | 1.0 | 16.0 | False | number | numeric | 8 |
| marital-status | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| occupation | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| relationship | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| race | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| sex | nominal | 0 | NaN | NaN | True | NaN | NaN | False | number | integer | 8 |
| capital-gain | continuous | 0 | 0.0 | 164870.0 | True | 0.0 | 99999.0 | False | number | numeric | 8 |
| capital-loss | continuous | 0 | 0.0 | 3982.0 | True | 0.0 | 2415.0 | False | number | numeric | 8 |
| hours-per-week | continuous | 0 | 0.0 | 163.0 | True | 1.0 | 99.0 | False | number | numeric | 8 |
| native-country | continuous | 0 | 0.0 | 66.0 | True | 0.0 | 40.0 | False | number | integer | 8 |
| target | nominal | 0 | NaN | NaN | True | NaN | NaN | False | 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]:
| workclass | sex | race | native-country | education-num | occupation | education | marital-status | capital-loss | fnlwgt | target | age | capital-gain | relationship | hours-per-week | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | [0.17753964974126968, 0] | [0.359369678865173, 0] | [0.13143175714420807, 0] | [3.2401110524445897, 0] | [1.7763568394002505e-15, 0] | [0.7372711123793414, 0] | [0, 0] | [0, 0] | [0, 0] | [191808.99970349576, 0] | [0, 0] | [2.649609932850961, 0] | [0, 0] | [0.3934297424337464, 0] | [0.40326033227631797, 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]:
| workclass | sex | race | native-country | education-num | occupation | education | marital-status | target | fnlwgt | capital-loss | capital-gain | age | relationship | hours-per-week | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| feature_full_residuals | [0.3589348618269888, 0] | [0.2417996938574392, 0] | [0.2397361995437431, 0] | [2.749922760136048, 0] | [0.05464532505609074, 0] | [0.837073721474876, 0] | [0.005077383747108766, 0] | [0.22988024313953254, 0] | [0.21531142446852702, 0] | [82894.50674566082, 0] | [83.20948637979849, 0] | [1615.258946011626, 0] | [9.380478286325173, 0] | [0.32641103508447417, 0] | [8.060335593020671, 0] |