Skip to content

Commit 9d72ec7

Browse files
docs: add cross referencing to several plots functions (#3005)
* update beeswarm.py * update bar.py * update decision.py * update force.py * update heatmap.py * update image.py * update scatter.py * update text.py * update violin.py * update waterfall.py * update changelog entry
1 parent 2797db5 commit 9d72ec7

11 files changed

+231
-129
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4545
### Changed
4646

4747
- Updates to docstrings of several `shap.plots` functions
48-
([#3003](https://github.com/slundberg/shap/pull/3003) by @thatlittleboy).
48+
([#3003](https://github.com/slundberg/shap/pull/3003),
49+
[#3005](https://github.com/slundberg/shap/pull/3005) by @thatlittleboy).
4950

5051
### Removed
5152

shap/plots/_bar.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,31 @@
2020
# TODO: Have the Explanation object track enough data so that we can tell (and so show) how many instances are in each cohort
2121
def bar(shap_values, max_display=10, order=Explanation.abs, clustering=None, clustering_cutoff=0.5,
2222
merge_cohorts=False, show_data="auto", show=True):
23-
""" Create a bar plot of a set of SHAP values.
23+
"""Create a bar plot of a set of SHAP values.
2424
25-
If a single sample is passed then we plot the SHAP values as a bar chart. If an
26-
Explanation with many samples is passed then we plot the mean absolute value for
27-
each feature column as a bar chart.
25+
If a single sample is passed, then we plot the SHAP values as a bar chart. If an
26+
:class:`.Explanation` with many samples is passed, then we plot the mean absolute
27+
value for each feature column as a bar chart.
2828
2929
3030
Parameters
3131
----------
3232
shap_values : shap.Explanation or shap.Cohorts or dictionary of shap.Explanation objects
33-
A single row of a SHAP Explanation object (i.e. shap_values[0]) or a multi-row Explanation
34-
object that we want to summarize.
33+
A single row of a SHAP :class:`.Explanation` object (i.e. ``shap_values[0]``) or
34+
a multi-row Explanation object that we want to summarize.
3535
3636
max_display : int
37-
The maximum number of bars to display.
37+
How many top features to include in the bar plot (default is 10).
3838
3939
show : bool
40-
If show is set to False then we don't call the matplotlib.pyplot.show() function. This allows
41-
further customization of the plot by the caller after the bar() function is finished.
40+
Whether ``matplotlib.pyplot.show()`` is called before returning.
41+
Setting this to ``False`` allows the plot
42+
to be customized further after it has been created.
43+
44+
Examples
45+
--------
46+
47+
See `bar plot examples <https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/bar.html>`_.
4248
4349
"""
4450

shap/plots/_beeswarm.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,33 @@ def beeswarm(shap_values, max_display=10, order=Explanation.abs.mean(0),
3434
Parameters
3535
----------
3636
shap_values : Explanation
37-
This is an Explanation object containing a matrix of SHAP values (# samples x # features).
37+
This is an :class:`.Explanation` object containing a matrix of SHAP values
38+
(# samples x # features).
3839
3940
max_display : int
40-
How many top features to include in the plot (default is 20, or 7 for interaction plots)
41+
How many top features to include in the plot (default is 10, or 7 for
42+
interaction plots).
43+
44+
show : bool
45+
Whether ``matplotlib.pyplot.show()`` is called before returning.
46+
Setting this to ``False`` allows the plot
47+
to be customized further after it has been created.
48+
49+
color_bar : bool
50+
Whether to draw the color bar (legend).
4151
4252
plot_size : "auto" (default), float, (float, float), or None
43-
What size to make the plot. By default the size is auto-scaled based on the number of
44-
features that are being displayed. Passing a single float will cause each row to be that
45-
many inches high. Passing a pair of floats will scale the plot by that
46-
number of inches. If None is passed then the size of the current figure will be left
47-
unchanged.
53+
What size to make the plot. By default, the size is auto-scaled based on the
54+
number of features that are being displayed. Passing a single float will cause
55+
each row to be that many inches high. Passing a pair of floats will scale the
56+
plot by that number of inches. If ``None`` is passed, then the size of the
57+
current figure will be left unchanged.
58+
59+
Examples
60+
--------
61+
62+
See `beeswarm plot examples <https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/beeswarm.html>`_.
63+
4864
"""
4965

5066
if not isinstance(shap_values, Explanation):

shap/plots/_decision.py

+42-28
Original file line numberDiff line numberDiff line change
@@ -244,29 +244,32 @@ def decision(
244244
----------
245245
base_value : float or numpy.ndarray
246246
This is the reference value that the feature contributions start from. Usually, this is
247-
explainer.expected_value.
247+
``explainer.expected_value``.
248248
249249
shap_values : numpy.ndarray
250-
Matrix of SHAP values (# features) or (# samples x # features) from explainer.shap_values(). Or cube of SHAP
251-
interaction values (# samples x # features x # features) from explainer.shap_interaction_values().
250+
Matrix of SHAP values (# features) or (# samples x # features) from
251+
``explainer.shap_values()``. Or cube of SHAP interaction values (# samples x
252+
# features x # features) from ``explainer.shap_interaction_values()``.
252253
253254
features : numpy.array or pandas.Series or pandas.DataFrame or numpy.ndarray or list
254255
Matrix of feature values (# features) or (# samples x # features). This provides the values of all the
255256
features and, optionally, the feature names.
256257
257258
feature_names : list or numpy.ndarray
258-
List of feature names (# features). If None, names may be derived from the features argument if a Pandas
259-
object is provided. Otherwise, numeric feature names will be generated.
259+
List of feature names (# features). If ``None``, names may be derived from the
260+
``features`` argument if a Pandas object is provided. Otherwise, numeric feature
261+
names will be generated.
260262
261263
feature_order : str or None or list or numpy.ndarray
262-
Any of "importance" (the default), "hclust" (hierarchical clustering), "none", or a list/array of indices.
264+
Any of "importance" (the default), "hclust" (hierarchical clustering), ``None``,
265+
or a list/array of indices.
263266
264267
feature_display_range: slice or range
265-
The slice or range of features to plot after ordering features by feature_order. A step of 1 or None
268+
The slice or range of features to plot after ordering features by ``feature_order``. A step of 1 or ``None``
266269
will display the features in ascending order. A step of -1 will display the features in descending order. If
267-
feature_display_range=None, slice(-1, -21, -1) is used (i.e. show the last 20 features in descending order).
268-
If shap_values contains interaction values, the number of features is automatically expanded to include all
269-
possible interactions: N(N + 1)/2 where N = shap_values.shape[1].
270+
``feature_display_range=None``, ``slice(-1, -21, -1)`` is used (i.e. show the last 20 features in descending order).
271+
If ``shap_values`` contains interaction values, the number of features is automatically expanded to include all
272+
possible interactions: N(N + 1)/2 where N = ``shap_values.shape[1]``.
270273
271274
highlight : Any
272275
Specify which observations to draw in a different line style. All numpy indexing methods are supported. For
@@ -277,7 +280,7 @@ def decision(
277280
log-odds into probabilities.
278281
279282
plot_color : str or matplotlib.colors.ColorMap
280-
Color spectrum used to draw the plot lines. If str, a registered matplotlib color name is assumed.
283+
Color spectrum used to draw the plot lines. If ``str``, a registered matplotlib color name is assumed.
281284
282285
axis_color : str or int
283286
Color used to draw plot axes.
@@ -289,39 +292,46 @@ def decision(
289292
Alpha blending value in [0, 1] used to draw plot lines.
290293
291294
color_bar : bool
292-
Whether to draw the color bar.
295+
Whether to draw the color bar (legend).
293296
294297
auto_size_plot : bool
295-
Whether to automatically size the matplotlib plot to fit the number of features displayed. If `False`,
296-
specify the plot size using matplotlib before calling this function.
298+
Whether to automatically size the matplotlib plot to fit the number of features
299+
displayed. If ``False``, specify the plot size using matplotlib before calling
300+
this function.
297301
298302
title : str
299303
Title of the plot.
300304
301305
xlim: tuple[float, float]
302-
The extents of the x-axis (e.g. (-1.0, 1.0)). If not specified, the limits are determined by the
303-
maximum/minimum predictions centered around base_value when link='identity'. When link='logit', the
304-
x-axis extents are (0, 1) centered at 0.5. x_lim values are not transformed by the link function. This
305-
argument is provided to simplify producing multiple plots on the same scale for comparison.
306+
The extents of the x-axis (e.g. ``(-1.0, 1.0)``). If not specified, the limits
307+
are determined by the maximum/minimum predictions centered around base_value
308+
when ``link="identity"``. When ``link="logit"``, the x-axis extents are ``(0,
309+
1)`` centered at 0.5. ``xlim`` values are not transformed by the ``link``
310+
function. This argument is provided to simplify producing multiple plots on the
311+
same scale for comparison.
306312
307313
show : bool
308-
Whether to automatically display the plot.
314+
Whether ``matplotlib.pyplot.show()`` is called before returning.
315+
Setting this to ``False`` allows the plot
316+
to be customized further after it has been created.
309317
310318
return_objects : bool
311-
Whether to return a DecisionPlotResult object containing various plotting features. This can be used to
312-
generate multiple decision plots using the same feature ordering and scale.
319+
Whether to return a :obj:`DecisionPlotResult` object containing various plotting
320+
features. This can be used to generate multiple decision plots using the same
321+
feature ordering and scale.
313322
314323
ignore_warnings : bool
315324
Plotting many data points or too many features at a time may be slow, or may create very large plots. Set
316-
this argument to `True` to override hard-coded limits that prevent plotting large amounts of data.
325+
this argument to ``True`` to override hard-coded limits that prevent plotting large amounts of data.
317326
318327
new_base_value : float
319-
SHAP values are relative to a base value; by default, the expected value of the model's raw predictions. Use
320-
`new_base_value` to shift the base value to an arbitrary value (e.g. the cutoff point for a binary
328+
SHAP values are relative to a base value. By default, this base value is the
329+
expected value of the model's raw predictions. Use ``new_base_value`` to shift
330+
the base value to an arbitrary value (e.g. the cutoff point for a binary
321331
classification task).
322332
323333
legend_labels : list of str
324-
List of legend labels. If `None`, legend will not be shown.
334+
List of legend labels. If ``None``, legend will not be shown.
325335
326336
legend_location : str
327337
Legend location. Any of "best", "upper right", "upper left", "lower left", "lower right", "right",
@@ -330,15 +340,19 @@ def decision(
330340
Returns
331341
-------
332342
DecisionPlotResult or None
333-
Returns a DecisionPlotResult object if `return_objects=True`. Returns `None` otherwise (the default).
343+
Returns a :obj:`DecisionPlotResult` object if ``return_objects=True``. Returns ``None`` otherwise (the default).
344+
345+
Examples
346+
--------
334347
335-
Example
336-
-------
337348
Plot two decision plots using the same feature order and x-axis.
349+
338350
>>> range1, range2 = range(20), range(20, 40)
339351
>>> r = decision_plot(base, shap_values[range1], features[range1], return_objects=True)
340352
>>> decision_plot(base, shap_values[range2], features[range2], feature_order=r.feature_idx, xlim=r.xlim)
341353
354+
See more `decision plot examples here <https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/decision_plot.html>`_.
355+
342356
"""
343357

344358
# code taken from force_plot. auto unwrap the base_value

shap/plots/_force.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@
2828
def force(base_value, shap_values=None, features=None, feature_names=None, out_names=None, link="identity",
2929
plot_cmap="RdBu", matplotlib=False, show=True, figsize=(20,3), ordering_keys=None, ordering_keys_time_format=None,
3030
text_rotation=0, contribution_threshold=0.05):
31-
""" Visualize the given SHAP values with an additive force layout.
31+
"""Visualize the given SHAP values with an additive force layout.
3232
3333
Parameters
3434
----------
3535
base_value : float
36-
This is the reference value that the feature contributions start from. For SHAP values it should
37-
be the value of explainer.expected_value.
36+
This is the reference value that the feature contributions start from.
37+
For SHAP values, it should be the value of ``explainer.expected_value``.
3838
3939
shap_values : numpy.array
40-
Matrix of SHAP values (# features) or (# samples x # features). If this is a 1D array then a single
41-
force plot will be drawn, if it is a 2D array then a stacked force plot will be drawn.
40+
Matrix of SHAP values (# features) or (# samples x # features). If this is a
41+
1D array, then a single force plot will be drawn. If it is a 2D array, then a
42+
stacked force plot will be drawn.
4243
4344
features : numpy.array
4445
Matrix of feature values (# features) or (# samples x # features). This provides the values of all the
45-
features, and should be the same shape as the shap_values argument.
46+
features, and should be the same shape as the ``shap_values`` argument.
4647
4748
feature_names : list
4849
List of feature names (# features).
@@ -51,12 +52,13 @@ def force(base_value, shap_values=None, features=None, feature_names=None, out_n
5152
The name of the output of the model (plural to support multi-output plotting in the future).
5253
5354
link : "identity" or "logit"
54-
The transformation used when drawing the tick mark labels. Using logit will change log-odds numbers
55+
The transformation used when drawing the tick mark labels. Using "logit" will change log-odds numbers
5556
into probabilities.
5657
5758
matplotlib : bool
58-
Whether to use the default Javascript output, or the (less developed) matplotlib output. Using matplotlib
59-
can be helpful in scenarios where rendering Javascript/HTML is inconvenient.
59+
Whether to use the default Javascript output, or the (less developed) matplotlib output.
60+
Using matplotlib can be helpful in scenarios where rendering Javascript/HTML
61+
is inconvenient.
6062
6163
contribution_threshold : float
6264
Controls the feature names/values that are displayed on force plot.
@@ -257,16 +259,18 @@ def save_html(out_file, plot, full_html=True):
257259
Parameters
258260
----------
259261
out_file : str or file
260-
Location or file to be written to
262+
Location or file to be written to.
263+
261264
plot : BaseVisualizer
262-
Visualizer returned by shap.force_plot()
265+
Visualizer returned by :func:`shap.plots.force()`.
266+
263267
full_html : boolean (default: True)
264-
If True, writes a complete HTML document starting
265-
with an <html> tag. If False, only script and div
268+
If ``True``, writes a complete HTML document starting
269+
with an ``<html>`` tag. If ``False``, only script and div
266270
tags are included.
267271
"""
268272

269-
assert isinstance(plot, BaseVisualizer), "save_html requires a Visualizer returned by shap.force_plot()."
273+
assert isinstance(plot, BaseVisualizer), "`save_html` requires a Visualizer returned by `shap.plots.force()`."
270274
internal_open = False
271275
if type(out_file) == str:
272276
out_file = open(out_file, "w", encoding="utf-8")

shap/plots/_heatmap.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,46 @@ def heatmap(shap_values, instance_order=Explanation.hclust(), feature_values=Exp
1414
"""Create a heatmap plot of a set of SHAP values.
1515
1616
This plot is designed to show the population substructure of a dataset using supervised
17-
clustering and a heatmap. Supervised clustering involves clustering data points not by their original
18-
feature values but by their explanations. By default we cluster using shap.utils.hclust_ordering
17+
clustering and a heatmap.
18+
Supervised clustering involves clustering data points not by their original
19+
feature values but by their explanations.
20+
By default, we cluster using :func:`shap.utils.hclust_ordering`,
1921
but any clustering can be used to order the samples.
2022
2123
Parameters
2224
----------
2325
shap_values : shap.Explanation
24-
A multi-row Explanation object that we want to visualize in a cluster ordering.
26+
A multi-row :class:`.Explanation` object that we want to visualize in a
27+
cluster ordering.
2528
2629
instance_order : OpChain or numpy.ndarray
2730
A function that returns a sort ordering given a matrix of SHAP values and an axis, or
28-
a direct sample ordering given as an numpy.ndarray.
31+
a direct sample ordering given as an ``numpy.ndarray``.
2932
3033
feature_values : OpChain or numpy.ndarray
3134
A function that returns a global summary value for each input feature, or an array of such values.
3235
3336
feature_order : None, OpChain, or numpy.ndarray
3437
A function that returns a sort ordering given a matrix of SHAP values and an axis, or
35-
a direct input feature ordering given as an numpy.ndarray. If None then we use
36-
feature_values.argsort
38+
a direct input feature ordering given as an ``numpy.ndarray``.
39+
If ``None``, then we use ``feature_values.argsort``.
3740
3841
max_display : int
39-
The maximum number of features to display.
42+
The maximum number of features to display (default is 10).
4043
4144
show : bool
42-
If show is set to False then we don't call the matplotlib.pyplot.show() function. This allows
43-
further customization of the plot by the caller after the bar() function is finished.
45+
Whether ``matplotlib.pyplot.show()`` is called before returning.
46+
Setting this to ``False`` allows the plot
47+
to be customized further after it has been created.
4448
4549
plot_width: int, default 8
4650
The width of the heatmap plot.
4751
52+
Examples
53+
--------
54+
55+
See `heatmap plot examples <https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/heatmap.html>`_.
56+
4857
"""
4958

5059
# sort the SHAP values matrix by rows and columns

0 commit comments

Comments
 (0)