Examples · Compiling

build_artifact

Every parameter of the main constructor, and what each one changes in the emitted JSON document.

Code#

build_artifact.py
import numpy as np
from sklearn.metrics import roc_auc_score
 
from _setup import (
REASONS,
X_test,
X_train,
bands,
feature_names,
latent,
teacher_test_scores,
whitebox,
y_test,
y_train,
)
 
from compileml.artifact import build_artifact, save_artifact
from compileml.runtime import decide, load_artifact
 
artifact = build_artifact(
whitebox,
feature_names,
baseline=np.median(X_train, axis=0), # -> features.baseline, the attribution reference
band_edges=bands, # -> bands.edges_int
calibration_latent=latent, # -> calibration.f_micro
calibration_y=y_train, # -> calibration.pd_ppm
reasons=REASONS, # -> reasons{}
missing_policy="baseline", # -> features.missing_policy
)
 
save_artifact(artifact, "decision.json")
 
print("schema version ", artifact["schema_version"])
print("hash ", artifact["artifact_hash"])
print("features ", len(artifact["features"]["names"]))
print("trees ", len(artifact["model"]["trees"]))
print("bands ", len(artifact["bands"]["labels"]))
print("missing policy ", artifact["features"]["missing_policy"])
 
# What the compilation cost, on this data. Retention is a property of a
# dataset, a teacher and a depth budget — not of the library — so it is
# reported here rather than quoted as a constant anywhere.
gini = lambda y, s: 2 * roc_auc_score(y, s) - 1 # noqa: E731
 
compiled = load_artifact("decision.json")
scores = [decide(compiled, row.tolist(), explain=False)["latent_int"] for row in X_test]
 
teacher_gini = gini(y_test, teacher_test_scores)
artifact_gini = gini(y_test, scores)
 
print()
print(f"teacher gini {teacher_gini:.3f}")
print(f"artifact gini {artifact_gini:.3f}")
print(f"retained {100 * artifact_gini / teacher_gini:.1f}%")

Output#

Captured from an actual run against compileml 0.9.0 and the UCI credit panel. If this script stops working, the build fails.

captured in CIbuild_artifact.py
schema version 2
hash a31afc9bf476ce94714946a120a525f5b0049dcfe1461e4abf6c01cde434e66c
features 19
trees 30
bands 10
missing policy baseline
 
teacher gini 0.549
artifact gini 0.542
retained 98.7%

Notes#

  • baseline doubles as the imputation value and the attribution reference; changing it changes every explanation.
  • Omitting calibration_y produces an artifact with calibration: null — scores and bands still work, pd does not.
  • The hash is computed last, over the canonical form of everything above.

API reference: build_artifact →