Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable parallel processing for serializable models #646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions doc/notebooks/Tutorial - parallel explanation generation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"source": [
"from functools import partial\n",
"\n",
"import sklearn\n",
"import sklearn.datasets\n",
"import sklearn.ensemble\n",
"import multiprocessing as mp\n",
"import numpy as np\n",
"import lime\n",
"import lime.lime_tabular\n",
"np.random.seed(1)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Loading data, training a model"
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"For this part, we'll use the Iris dataset, and we'll train a random forest. "
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"iris = sklearn.datasets.load_iris()"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"train, test, labels_train, labels_test = sklearn.model_selection.train_test_split(iris.data, iris.target, train_size=0.80)\n"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"rf = sklearn.ensemble.RandomForestClassifier(n_estimators=500)\n",
"rf.fit(train, labels_train)\n"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"RandomForestClassifier(n_estimators=500)"
]
},
"metadata": {},
"execution_count": 4
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"sklearn.metrics.accuracy_score(labels_test, rf.predict(test))"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.9666666666666667"
]
},
"metadata": {},
"execution_count": 5
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Create the explainer"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"explainer = lime.lime_tabular.LimeTabularExplainer(train, feature_names=iris.feature_names, class_names=iris.target_names, discretize_continuous=True)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Explaining multiple instances in parallel"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 11,
"source": [
"%%time\n",
"explainer_partial = partial(\n",
" explainer.explain_instance, predict_fn=rf.predict_proba, num_features=2\n",
")\n",
"with mp.Pool(mp.cpu_count() - 1) as p:\n",
" exp_parallel = p.map(explainer_partial, test[:20])\n",
"\n",
"print(exp_parallel)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[<lime.explanation.Explanation object at 0x7fc166148190>, <lime.explanation.Explanation object at 0x7fc166148710>, <lime.explanation.Explanation object at 0x7fc166034690>, <lime.explanation.Explanation object at 0x7fc166148650>, <lime.explanation.Explanation object at 0x7fc165aa8b90>, <lime.explanation.Explanation object at 0x7fc166148d10>, <lime.explanation.Explanation object at 0x7fc16610f810>, <lime.explanation.Explanation object at 0x7fc166148050>, <lime.explanation.Explanation object at 0x7fc166197510>, <lime.explanation.Explanation object at 0x7fc166197610>, <lime.explanation.Explanation object at 0x7fc166197d90>, <lime.explanation.Explanation object at 0x7fc15de91a90>, <lime.explanation.Explanation object at 0x7fc15de91890>, <lime.explanation.Explanation object at 0x7fc15de91790>, <lime.explanation.Explanation object at 0x7fc15de91bd0>, <lime.explanation.Explanation object at 0x7fc166191e50>, <lime.explanation.Explanation object at 0x7fc166191e90>, <lime.explanation.Explanation object at 0x7fc166191b90>, <lime.explanation.Explanation object at 0x7fc166170f10>, <lime.explanation.Explanation object at 0x7fc166170310>]\n",
"CPU times: user 443 ms, sys: 77.7 ms, total: 521 ms\n",
"Wall time: 5.51 s\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"source": [
"exp_parallel[0].show_in_notebook(show_table=True, show_all=False)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.7 64-bit ('.venv': venv)"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
},
"interpreter": {
"hash": "c5610125cf5e6a650be88d971b2a640487825e036e54247d3d2fb29abd9ffd91"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading