H2O Sonar Demo of Driverless AI MOJO Models

[15]:
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 fi_naive_shapley_explainer as explainer
from h2o_sonar.lib.api.models import ModelApi
[16]:
# explainer description
interpret.describe_explainer(explainer.NaiveShapleyMojoFeatureImportanceExplainer)
[16]:
{'id': 'h2o_sonar.explainers.fi_naive_shapley_explainer.NaiveShapleyMojoFeatureImportanceExplainer',
 'name': 'NaiveShapleyMojoFeatureImportanceExplainer',
 'display_name': 'Shapley Values for Original Features of MOJO Models (Naive Method)',
 'tagline': 'NaiveShapleyMojoFeatureImportanceExplainer.',
 'description': 'Shapley values for original features of (Driverless AI) MOJO models are approximated from the accompanying Shapley values for transformed features with the Naive Shapley method. This method makes the assumption that input features to a transformer are independent. For example, if the transformed feature, feature1_feature2, has a Shapley value of 0.5, then the Shapley value of the original features feature1 and feature2 will be 0.25 each.',
 'brief_description': 'NaiveShapleyMojoFeatureImportanceExplainer.',
 'model_types': ['iid'],
 'can_explain': ['regression', 'binomial', 'multinomial'],
 'explanation_scopes': ['global_scope', 'local_scope'],
 'explanations': [{'explanation_type': 'global-feature-importance',
   'name': 'GlobalFeatImpExplanation',
   'category': '',
   'scope': 'global',
   'has_local': '',
   'formats': []},
  {'explanation_type': 'local-feature-importance',
   'name': 'LocalFeatImpExplanation',
   'category': '',
   'scope': 'local',
   'has_local': '',
   'formats': []}],
 'keywords': ['run-by-default',
  'explains-original-feature-importance',
  'is_fast',
  'h2o-sonar'],
 'parameters': [{'name': 'sample_size',
   'description': 'Sample size.',
   'comment': '',
   'type': 'int',
   'val': 100000,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''},
  {'name': 'fast_approx_contribs',
   'description': 'Speed up predictions with fast contributions predictions approximation.',
   'comment': '',
   'type': 'bool',
   'val': True,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''},
  {'name': 'leakage_warning_threshold',
   'description': 'The threshold above which to report a potentially detected feature importance leak problem.',
   'comment': '',
   'type': 'float',
   'val': 0.95,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''}],
 'metrics_meta': []}

Interpretation

[17]:
# dataset
dataset_path = "../../data/predictive/creditcard.csv"
target_col = "default payment next month"

# model
mojo_path = "../../data/predictive/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)
[18]:
%%capture
interpretation = interpret.run_interpretation(
    dataset=dataset_path,
    model=model,
    target_col=target_col,
    results_location=results_location,
    explainers=[explainer.NaiveShapleyMojoFeatureImportanceExplainer.explainer_id()],
    log_level=logging.INFO,
)

Explainer Result

[19]:
# retrieve the result
result = interpretation.get_explainer_result(
    explainer.NaiveShapleyMojoFeatureImportanceExplainer.explainer_id()
)
[20]:
# open interpretation HTML report in web browser
webbrowser.open(interpretation.result.get_html_report_location())
[20]:
True
[21]:
# summary
result.summary()
[21]:
{'id': 'h2o_sonar.explainers.fi_naive_shapley_explainer.NaiveShapleyMojoFeatureImportanceExplainer',
 'name': 'NaiveShapleyMojoFeatureImportanceExplainer',
 'display_name': 'Shapley Values for Original Features of MOJO Models (Naive Method)',
 'tagline': 'NaiveShapleyMojoFeatureImportanceExplainer.',
 'description': 'Shapley values for original features of (Driverless AI) MOJO models are approximated from the accompanying Shapley values for transformed features with the Naive Shapley method. This method makes the assumption that input features to a transformer are independent. For example, if the transformed feature, feature1_feature2, has a Shapley value of 0.5, then the Shapley value of the original features feature1 and feature2 will be 0.25 each.',
 'brief_description': 'NaiveShapleyMojoFeatureImportanceExplainer.',
 'model_types': ['iid'],
 'can_explain': ['regression', 'binomial', 'multinomial'],
 'explanation_scopes': ['global_scope', 'local_scope'],
 'explanations': [{'explanation_type': 'global-feature-importance',
   'name': 'Shapley Values for Original Features of MOJO Models (Naive Method)',
   '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 Original Features of MOJO Models (Naive Method)',
   'category': 'DAI MODEL',
   'scope': 'local',
   'has_local': None,
   'formats': ['application/vnd.h2oai.json+datatable.jay']},
  {'explanation_type': 'global-html-fragment',
   'name': 'Shapley Values for Original Features of MOJO Models (Naive Method)',
   'category': 'DAI MODEL',
   'scope': 'global',
   'has_local': None,
   'formats': ['text/html']}],
 'keywords': ['run-by-default',
  'explains-original-feature-importance',
  'is_fast',
  'h2o-sonar'],
 'parameters': [{'name': 'sample_size',
   'description': 'Sample size.',
   'comment': '',
   'type': 'int',
   'val': 100000,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''},
  {'name': 'fast_approx_contribs',
   'description': 'Speed up predictions with fast contributions predictions approximation.',
   'comment': '',
   'type': 'bool',
   'val': True,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''},
  {'name': 'leakage_warning_threshold',
   'description': 'The threshold above which to report a potentially detected feature importance leak problem.',
   'comment': '',
   'type': 'float',
   'val': 0.95,
   'predefined': [],
   'tags': [],
   'min_': 0.0,
   'max_': 0.0,
   'category': ''}],
 'metrics_meta': []}
[22]:
# Parameters
result.params()
[22]:
{'sample_size': 100000,
 'fast_approx_contribs': True,
 'leakage_warning_threshold': 0.95}

Display Data

[23]:
result.data()
[23]:
featureimportance
▪▪▪▪▪▪▪▪▪▪▪▪
0PAY_00.592187
1PAY_20.224423
2LIMIT_BAL0.159352
3PAY_AMT40.14868
4PAY_AMT20.125437
5BILL_AMT10.101179
6PAY_30.0576715
7PAY_AMT30.0495318
8PAY_60.0453093
9PAY_40.0391064
10BILL_AMT20.0371473
11BILL_AMT60.0298867
12PAY_50.0270603
13PAY_AMT10.01873
14EDUCATION0.0131732
15AGE0.0110948
16MARRIAGE0.00884818
17PAY_AMT60.00759522
18PAY_AMT50.00719935
19BILL_AMT50.00589392
20BILL_AMT40.00189465
21BILL_AMT30.00108067

Plot Feature Importance Data

[24]:
result.plot()
../_images/notebooks_h2o-sonar-dai-mojo-models_14_0.png

Save Explainer Log and Data

[25]:
# save the explainer log
log_file_path = "./feature-importance-demo.log"
result.log(path=log_file_path)
[26]:
!cat $log_file_path
[27]:
# save the explainer data
result.zip(file_path="./feature-importance-demo-archive.zip")
[28]:
!unzip -l feature-importance-demo-archive.zip
Archive:  feature-importance-demo-archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     3553  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/result_descriptor.json
        2  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/problems/problems_and_actions.json
      110  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_html_fragment/text_html.meta
     4566  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_html_fragment/text_html/explanation.html
    25126  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_html_fragment/text_html/fi-class-0.png
        0  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/log/explainer_run_d911afc4-5dac-49f7-9c36-bf892bcd05da.log
  1762120  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/work/shapley.orig.feat.bin
  1903889  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/work/shapley_formatted_orig_feat.zip
  4674153  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/work/shapley.orig.feat.csv
    40216  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/work/y_hat.bin
        2  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/insights/insights_and_actions.json
      185  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_datatable_jay.meta
      143  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_json.meta
      163  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_csv.meta
     1277  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_datatable_jay/explanation.json
      880  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_datatable_jay/feature_importance_class_0.jay
      594  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_json/explanation.json
     1593  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_json/feature_importance_class_0.json
      593  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_csv/explanation.json
      725  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/global_feature_importance/application_vnd_h2oai_json_csv/feature_importance_class_0.csv
      201  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/local_feature_importance/application_vnd_h2oai_json_datatable_jay.meta
      863  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/local_feature_importance/application_vnd_h2oai_json_datatable_jay/explanation.json
    40216  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/local_feature_importance/application_vnd_h2oai_json_datatable_jay/y_hat.bin
  1762120  2026-01-29 15:51   explainer_h2o_sonar_explainers_fi_naive_shapley_explainer_NaiveShapleyMojoFeatureImportanceExplainer_d911afc4-5dac-49f7-9c36-bf892bcd05da/local_feature_importance/application_vnd_h2oai_json_datatable_jay/feature_importance_class_0.jay
---------                     -------
 10223290                     24 files
[ ]: