Skip to content

Commit

Permalink
DOCS: Fix precommit regex statement, and lint remaining tabular examp…
Browse files Browse the repository at this point in the history
…les (#3298)

* Fixup precommit regex pattern

* Lint two remaining notebooks

* Minimally fixup Boston Demo, ignore undefined variable

* Simplify regex exclude pattern

---------

Co-authored-by: Jeremy Goh <30731072+thatlittleboy@users.noreply.github.com>
  • Loading branch information
connortann and thatlittleboy committed Oct 4, 2023
1 parent bc65544 commit a8efcc8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 65 deletions.
8 changes: 5 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ci:
autoupdate_schedule: monthly

exclude: '.*tree_shap_paper.*|.*user_studies.*'
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
Expand Down Expand Up @@ -30,14 +31,14 @@ repos:
hooks:
- id: black-jupyter
name: black (fixed notebooks)
# This YAML anchor selects which notebooks are linted with ruff and black.
# Add a notebook here once it has been cleaned up.
# This regular expression selects which notebooks are linted with ruff and black.
# Add a notebook (or directory of notebooks) here once it has been cleaned up.
files: &cleanNotebooks |
(?x)^(
notebooks/api_examples/.*.ipynb|
notebooks/benchmarks/.*.ipynb|
notebooks/genomic_examples/.*.ipynb|
notebooks/tabular_examples/(?!.*tree_shap_paper).ipynb|
notebooks/tabular_examples/.*.ipynb|
)$
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
Expand All @@ -46,3 +47,4 @@ repos:
name: ruff (fixed notebooks)
types_or: [jupyter]
files: *cleanNotebooks
args: [ --fix, --exit-non-zero-on-fix ]
76 changes: 24 additions & 52 deletions notebooks/tabular_examples/model_agnostic/Simple Boston Demo.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"import shap\n",
"\n",
"# train XGBoost model\n",
"X,y = shap.datasets.boston()\n",
"X, y = shap.datasets.boston()\n",
"model = xgboost.XGBRegressor(max_depth=1).fit(X, y)\n",
"\n",
"# explain the model's predictions using SHAP values\n",
Expand Down Expand Up @@ -72,14 +72,18 @@
"import shap\n",
"\n",
"# train XGBoost model\n",
"X,y = shap.datasets.adult()\n",
"X, y = shap.datasets.adult()\n",
"model = xgboost.XGBClassifier(max_depth=1, learning_rate=0.5).fit(X, y)\n",
"\n",
"# explain the model's predictions using SHAP values\n",
"# (same syntax works for LightGBM, CatBoost, and scikit-learn models)\n",
"background = shap.maskers.TabularPartitions(X, sample=100)\n",
"\n",
"\n",
"def f(x):\n",
" return shap.links.identity(model.predict_proba(x, validate_features=False)[:,1])\n",
" return shap.links.identity(model.predict_proba(x, validate_features=False)[:, 1])\n",
"\n",
"\n",
"explainer = shap.Explainer(f, background, link=shap.links.logit)\n",
"shap_values = explainer(X[:100])\n",
"\n",
Expand Down Expand Up @@ -184,7 +188,7 @@
}
],
"source": [
"shap.plots.dependence(shap_values[:,\"Age\"])"
"shap.plots.dependence(shap_values[:, \"Age\"])"
]
},
{
Expand All @@ -210,7 +214,7 @@
}
],
"source": [
"shap.plots.dependence(shap_values[:,\"Age\"])"
"shap.plots.dependence(shap_values[:, \"Age\"])"
]
},
{
Expand Down Expand Up @@ -383,7 +387,7 @@
],
"source": [
"# plot the importance of a single feature across all samples\n",
"shap.plots.dependence(shap_values[:,\"RM\"], color=shap_values)"
"shap.plots.dependence(shap_values[:, \"RM\"], color=shap_values)"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
}
],
"source": [
"shap.summary_plot(shap_values[:1000,:], X.iloc[:1000,:], plot_type=\"layered_violin\", color='#cccccc')"
"shap.summary_plot(\n",
" shap_values[:1000, :], X.iloc[:1000, :], plot_type=\"layered_violin\", color=\"#cccccc\"\n",
")"
]
},
{
Expand All @@ -80,7 +82,12 @@
}
],
"source": [
"shap.summary_plot(shap_values[:1000,:], X.iloc[:1000,:], plot_type=\"layered_violin\", color='coolwarm')"
"shap.summary_plot(\n",
" shap_values[:1000, :],\n",
" X.iloc[:1000, :],\n",
" plot_type=\"layered_violin\",\n",
" color=\"coolwarm\",\n",
")"
]
},
{
Expand Down Expand Up @@ -140,7 +147,7 @@
}
],
"source": [
"shap.summary_plot(shap_values[:1000,:], X.iloc[:1000,:])"
"shap.summary_plot(shap_values[:1000, :], X.iloc[:1000, :])"
]
},
{
Expand Down Expand Up @@ -186,7 +193,7 @@
}
],
"source": [
"shap.summary_plot(shap_values[:1000,:], X.iloc[:1000,:], plot_type=\"violin\")"
"shap.summary_plot(shap_values[:1000, :], X.iloc[:1000, :], plot_type=\"violin\")"
]
}
],
Expand Down

0 comments on commit a8efcc8

Please sign in to comment.