|
30 | 30 | from sklearn.linear_model import Ridge |
31 | 31 | from sklearn.metrics import mean_squared_error |
32 | 32 | from sklearn.model_selection import train_test_split |
| 33 | + |
33 | 34 | from sklearn.externals import joblib |
34 | 35 | import numpy as np |
35 | 36 |
|
| 37 | +from interpret.ext.blackbox import TabularExplainer |
| 38 | +from azureml.contrib.explain.model.explanation.explanation_client import ( |
| 39 | + ExplanationClient |
| 40 | +) |
| 41 | + |
36 | 42 | parser = argparse.ArgumentParser("train") |
37 | 43 | parser.add_argument( |
38 | 44 | "--release_id", |
|
57 | 63 | run = Run.get_context() |
58 | 64 | exp = run.experiment |
59 | 65 | ws = run.experiment.workspace |
| 66 | +client = ExplanationClient.from_run(run) |
60 | 67 |
|
61 | 68 | X, y = load_diabetes(return_X_y=True) |
62 | 69 | columns = ["age", "gender", "bmi", "bp", "s1", "s2", "s3", "s4", "s5", "s6"] |
|
77 | 84 | preds = reg.predict(data["test"]["X"]) |
78 | 85 | run.log("mse", mean_squared_error(preds, data["test"]["y"])) |
79 | 86 |
|
| 87 | +# create an explainer to validate or debug the model |
| 88 | +tabular_explainer = TabularExplainer(reg, |
| 89 | + initialization_examples=X_train, |
| 90 | + features=columns) |
| 91 | +# explain overall model predictions (global explanation) |
| 92 | +# passing in test dataset for evaluation examples |
| 93 | + |
| 94 | +global_explanation = tabular_explainer.explain_global(X_test) |
| 95 | + |
| 96 | +# uploading model explanation data for storage or visualization |
| 97 | +comment = 'Global explanation on of Diabetes Regression' |
| 98 | +client.upload_model_explanation(global_explanation, comment=comment) |
| 99 | + |
80 | 100 | with open(model_name, "wb") as file: |
81 | 101 | joblib.dump(value=reg, filename=model_name) |
82 | 102 |
|
|
0 commit comments