Shapley Values for Transformed Features of MOJO Models Demo
This example demonstrates how to interpret a Driverless AI MOJO model using the H2O Eval Studio library and retrieve the data and plot with transformed features importances.
[1]:
import os
import logging
import datatable
import daimojo
import webbrowser
from h2o_sonar import interpret
from h2o_sonar.lib.api import commons
from h2o_sonar.lib.api import explainers
from h2o_sonar.explainers import transformed_fi_shapley_explainer as explainer
from h2o_sonar.lib.api.models import ModelApi
[2]:
# explainer description
interpret.describe_explainer(explainer.ShapleyMojoTransformedFeatureImportanceExplainer)
[2]:
{'id': 'h2o_sonar.explainers.transformed_fi_shapley_explainer.ShapleyMojoTransformedFeatureImportanceExplainer',
'name': 'ShapleyMojoTransformedFeatureImportanceExplainer',
'display_name': 'Shapley Values for Transformed Features of MOJO Models',
'description': 'Shapley explanations are a technique with credible theoretical support that presents consistent global and local variable contributions. Local numeric Shapley values are calculated by tracing single rows of data through a trained tree ensemble and aggregating the contribution of each input variable as the row of data moves through the trained ensemble. For regression tasks Shapley values sum to the prediction of the (Driverless AI) MOJO model. For classification problems, Shapley values sum to the prediction of the MOJO model before applying the link function. Global Shapley values are the average of the absolute local Shapley values over every row of a data set.',
'model_types': ['iid', 'time_series'],
'can_explain': ['regression', 'binomial', 'multinomial'],
'explanation_scopes': ['global_scope', 'local_scope'],
'explanations': [{'explanation_type': 'global-feature-importance',
'name': 'GlobalFeatImpExplanation',
'category': None,
'scope': 'global',
'has_local': None,
'formats': []},
{'explanation_type': 'local-feature-importance',
'name': 'LocalFeatImpExplanation',
'category': None,
'scope': 'local',
'has_local': None,
'formats': []}],
'parameters': [{'name': 'sample_size',
'description': 'Sample size.',
'comment': '',
'type': 'int',
'val': 100000,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''},
{'name': 'calculate_predictions',
'description': 'Score dataset and include predictions in the explanation (local explanations speed-up cache).',
'comment': '',
'type': 'bool',
'val': False,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''},
{'name': 'fast_approx_contrib',
'description': 'Speed up predictions with fast contributions predictions approximation.',
'comment': '',
'type': 'bool',
'val': True,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''}],
'keywords': ['run-by-default',
'explains-transformed_feature-importance',
'is_fast']}
Interpretation
[3]:
# dataset
dataset_path = "../../data/creditcard.csv"
target_col = "default payment next month"
# model
mojo_path = "../../data/models/creditcard-binomial.mojo"
mojo_model = daimojo.model(mojo_path)
model = ModelApi().create_model(
model_src=mojo_model,
target_col=target_col,
used_features=list(mojo_model.feature_names),
)
# results
results_location = "./results"
os.makedirs(results_location, exist_ok=True)
[4]:
interpretation = interpret.run_interpretation(
dataset=dataset_path,
model=model,
target_col=target_col,
results_location=results_location,
explainers=[explainer.ShapleyMojoTransformedFeatureImportanceExplainer.explainer_id()],
log_level=logging.INFO,
)
/home/dvorka/h/mli/git/h2o-sonar/.venv/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
h2o_sonar.explainers.transformed_fi_shapley_explainer.ShapleyMojoTransformedFeatureImportanceExplainer: progress 20.0%
h2o_sonar.explainers.transformed_fi_shapley_explainer.ShapleyMojoTransformedFeatureImportanceExplainer: progress 90.0%
h2o_sonar.explainers.transformed_fi_shapley_explainer.ShapleyMojoTransformedFeatureImportanceExplainer: progress 90.0%
Explainer Result
[5]:
# retrieve the result
result = interpretation.get_explainer_result(
explainer.ShapleyMojoTransformedFeatureImportanceExplainer.explainer_id()
)
[6]:
# open interpretation HTML report in web browser
webbrowser.open(interpretation.result.get_html_report_location())
[6]:
True
[7]:
# summary
result.summary()
[7]:
{'id': 'h2o_sonar.explainers.transformed_fi_shapley_explainer.ShapleyMojoTransformedFeatureImportanceExplainer',
'name': 'ShapleyMojoTransformedFeatureImportanceExplainer',
'display_name': 'Shapley Values for Transformed Features of MOJO Models',
'description': 'Shapley explanations are a technique with credible theoretical support that presents consistent global and local variable contributions. Local numeric Shapley values are calculated by tracing single rows of data through a trained tree ensemble and aggregating the contribution of each input variable as the row of data moves through the trained ensemble. For regression tasks Shapley values sum to the prediction of the (Driverless AI) MOJO model. For classification problems, Shapley values sum to the prediction of the MOJO model before applying the link function. Global Shapley values are the average of the absolute local Shapley values over every row of a data set.',
'model_types': ['iid', 'time_series'],
'can_explain': ['regression', 'binomial', 'multinomial'],
'explanation_scopes': ['global_scope', 'local_scope'],
'explanations': [{'explanation_type': 'global-feature-importance',
'name': 'Shapley Values for Transformed Features of MOJO Models',
'category': 'DAI MODEL',
'scope': 'global',
'has_local': 'local-feature-importance',
'formats': ['application/vnd.h2oai.json+datatable.jay',
'application/vnd.h2oai.json+csv',
'application/json']},
{'explanation_type': 'local-feature-importance',
'name': 'Shapley Values for Transformed Features of MOJO Models',
'category': 'DAI MODEL',
'scope': 'local',
'has_local': None,
'formats': ['application/vnd.h2oai.json+datatable.jay']},
{'explanation_type': 'global-html-fragment',
'name': 'Shapley Values for Transformed Features of MOJO Models',
'category': 'DAI MODEL',
'scope': 'global',
'has_local': None,
'formats': ['text/html']}],
'parameters': [{'name': 'sample_size',
'description': 'Sample size.',
'comment': '',
'type': 'int',
'val': 100000,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''},
{'name': 'calculate_predictions',
'description': 'Score dataset and include predictions in the explanation (local explanations speed-up cache).',
'comment': '',
'type': 'bool',
'val': False,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''},
{'name': 'fast_approx_contrib',
'description': 'Speed up predictions with fast contributions predictions approximation.',
'comment': '',
'type': 'bool',
'val': True,
'predefined': [],
'tags': [],
'min_': 0.0,
'max_': 0.0,
'category': ''}],
'keywords': ['run-by-default',
'explains-transformed_feature-importance',
'is_fast']}
[8]:
# parameters
result.params()
[8]:
{'sample_size': 100000,
'calculate_predictions': False,
'fast_approx_contrib': True}
Display Data
[9]:
result.data()
[9]:
feature | importance | |
---|---|---|
▪▪▪▪ | ▪▪▪▪▪▪▪▪ | |
0 | 10_PAY_0 | 0.484314 |
1 | 11_PAY_2 | 0.177288 |
2 | 8_LIMIT_BAL | 0.14293 |
3 | 19_PAY_AMT4 | 0.125988 |
4 | 17_PAY_AMT2 | 0.109839 |
5 | 1_BILL_AMT1 | 0.0856685 |
6 | 12_PAY_3 | 0.0460469 |
7 | 18_PAY_AMT3 | 0.0405643 |
8 | 15_PAY_6 | 0.035129 |
9 | 2_BILL_AMT2 | 0.0307533 |
10 | 13_PAY_4 | 0.0292232 |
11 | 6_BILL_AMT6 | 0.0230935 |
12 | 14_PAY_5 | 0.0203799 |
13 | 16_PAY_AMT1 | 0.014774 |
14 | 7_EDUCATION | 0.00983341 |
15 | 0_AGE | 0.00831316 |
16 | 9_MARRIAGE | 0.00725285 |
17 | 21_PAY_AMT6 | 0.00588243 |
18 | 20_PAY_AMT5 | 0.00541516 |
19 | 5_BILL_AMT5 | 0.00464642 |
20 | 4_BILL_AMT4 | 0.00144066 |
21 | 3_BILL_AMT3 | 0.000818275 |
Plot Feature Importance Data
[10]:
result.plot()
Save Explainer Log and Data
[11]:
# save the explainer log
log_file_path = "./feature-importance-demo.log"
result.log(path=log_file_path)
[12]:
!cat $log_file_path
[13]:
# save the explainer data
result.zip(file_path="./feature-importance-demo-archive.zip")
[14]:
!unzip -l feature-importance-demo-archive.zip
Archive: feature-importance-demo-archive.zip
Length Date Time Name
--------- ---------- ----- ----
3597 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/result_descriptor.json
110 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_html_fragment/text_html.meta
390 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_html_fragment/text_html/explanation.html
32992 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_html_fragment/text_html/fi-class-0.png
0 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/log/explainer_run_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a.log
1826902 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/work/shapley_formatted.zip
1842216 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/work/shapley.bin
4883188 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/work/shapley.csv
185 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_datatable_jay.meta
143 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_json.meta
163 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_csv.meta
1499 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_datatable_jay/explanation.json
944 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_datatable_jay/feature_importance_class_0.jay
808 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_json/explanation.json
1672 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_json/feature_importance_class_0.json
807 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_csv/explanation.json
803 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/global_feature_importance/application_vnd_h2oai_json_csv/feature_importance_class_0.csv
2 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/model_problems/problems_and_actions.json
201 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/local_feature_importance/application_vnd_h2oai_json_datatable_jay.meta
855 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/local_feature_importance/application_vnd_h2oai_json_datatable_jay/explanation.json
1842216 2022-10-10 22:56 explainer_h2o_sonar_explainers_transformed_fi_shapley_explainer_ShapleyMojoTransformedFeatureImportanceExplainer_4aafdf97-d1d8-4a00-82d0-42dacf8ebb3a/local_feature_importance/application_vnd_h2oai_json_datatable_jay/feature_importance_class_0.jay
--------- -------
10439693 21 files
[ ]: