<jupyter_start><jupyter_text>Quick Global Evaluation  1. Visualizing decision boundaries<jupyter_code>%matplotlib inline
from skater.core.visualizer import decision_boundary as db

# Uncomment the the below function to generate decision plot.
# _, _ = db.plot_decision_boundary(clf, X0=X_train.iloc[:, 3], X1=X_train.iloc[:, 9], Y=y_train, 
#                                  x0_label=feature_names[3], x1_label=feature_names[9],
#                                  mode='interactive', height=6, width=10, file_name='iplot')<jupyter_output><empty_output><jupyter_text>2. Using pruned Tree Surrogates<jupyter_code>from skater.core.explanations import Interpretation
from skater.model import InMemoryModel
from skater.util.dataops import show_in_notebook
from skater.util.logger import _INFO, _DEBUG

target_names = ['class 0', 'class 1']
interpreter = Interpretation(X_train, feature_names=feature_names)
model_inst = InMemoryModel(clf.predict, examples=X_train, model_type='classifier', 
                           unique_values=[0, 1], probability=False,
                           feature_names=feature_names, target_names=target_names, log_level=_INFO)
# Learn or Fit a TreeSurrogate(Decision Tree - with no pruning). This setting will most likely overfit the 
# previously trained instance.
# Note: TreeSurrogate is being trained with respect to the predicted labels retrieved by querying the base estimator
# (for this e.g. previously trained RandomForest). 
surrogate_explainer = interpreter.tree_surrogate(oracle=model_inst, seed=5)
surrogate_explainer.fit(X_train, y_train, use_oracle=True, prune=None, scorer_type='f1')
y_hat = surrogate_explainer.predict(X_test)
print("\n--------Test dataset classification report----------\n")
target_names = ['class 0', 'class 1']
print(classification_report(y_test, y_hat, target_names=target_names))
%matplotlib inline
surrogate_explainer.plot_global_decisions(colors=['lightsteelblue','darkkhaki'], 
                                          file_name='tree_experiment.png', show_img=False)
#show_in_notebook('tree_experiment.png', width=800, height=400)
# One can specify the external params if default setting is not producing acceptable results. 
# Currently, prepruning is done with the help of CV.
params = {"criterion": ['gini', 'entropy'], "min_samples_leaf": [4, 6],
"max_leaf_nodes": [12, 20], "max_depth": [10, 12]
}

surrogate_explainer.fit(X_train, y_train, use_oracle=True, prune='pre', scorer_type='f1', param_grid=params)
y_hat = surrogate_explainer.predict(X_test)
print("\n--------Test dataset classification report----------\n")
target_names = ['class 0', 'class 1']
print(classification_report(y_test, y_hat, target_names=target_names))
# Estimator used for final scoring ...
surrogate_explainer.estimator_
surrogate_explainer.plot_global_decisions(colors=['lightsteelblue','darkkhaki'], file_name='tree_pruned.png', 
                                          show_img=False)
#show_in_notebook('tree_pruned.png', width=600, height=600)
# as text:
surrogate_explainer.decisions_as_txt()<jupyter_output>if [0;32;49mX_I <= -1.6024528741836548[0;30;49m {
   [1;34;49m Predicted Label: 1
[0;30;49m} else {
  if [0;32;49mX_J <= 1.2718250751495361[0;30;49m {
    if [0;32;49mX_D <= -0.6693952679634094[0;30;49m {
      if [0;32;49mX_C <= 1.4347039461135864[0;30;49m {
        if [0;32;49mX_I <= -0.0651244968175888[0;30;49m {
           [1;34;49m Predicted Label: 1
        [0;30;49m} else {
          if [0;32;49mX_C <= 0.4285590648651123[0;30;49m {
             [1;34;49m Predicted Label: 0
          [0;30;49m} else {
             [1;34;49m Predicted Label: 0
          [0;30;49m}
        [0;30;49m}
      [0;30;49m} else {
         [1;34;49m Predicted Label: 1
      [0;30;49m}
    [0;30;49m} else {
      if [0;32;49mX_C <= -0.25395074486732483[0;30;49m {
        if [0;32;49mX_D <= 0.4669700264930725[0;30;49m {
           [1;34;49m Predicted Label: 0
        [0;30;49m} else {
          if [0;32;49mX_E <= 0.15138494968414307[0;30;49m {
             [1;34;49m Predict[...]<jupyter_text>Quick Local Evaluation<jupyter_code># Filter for in-correct predictions
in_correct_prediction = np.where(y_hat != y_test)
in_correct_prediction
sample_index = 306
print("Data row: {}".format(X_test.iloc[sample_index]))
print("-----------------------------------------------")
print("Target Label: {}".format(y_test[sample_index]))
print("Ground Truth : {}".format(y_test[sample_index]))
print("Oracle Model {}:".format(clf.predict(X_test.iloc[sample_index].values.reshape(1, -1))))
print("Using TreeSurrogate {}:".format(surrogate_explainer.predict(X_test.iloc[sample_index].values.reshape(1, -1))))
surrogate_explainer.decisions_as_txt(scope="local", X=X_test.iloc[sample_index])<jupyter_output>As [0;32;49mX_I[-1.6968063342595445] <= -1.6024528741836548[0;30;49m then,
   [1;34;49m Predicted Label: 1
{'fraction_non_alphanumeric': 0.15024529844644316, 'fraction_numerical': 0.08871627146361406, 'mean_word_length': 4.150526315789474, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '189223', 'license': 'UPL-1.0', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1715, 'n_words': 305, 'path': 'examples/rule_list_notebooks/explanation_using_prepruning_synthetic_classification.ipynb', 'repo_name': 'BouncyButton/Skater', 'size': '4892'}