8000 DOC Fixes visualization docs and runs code to plot (#22579) · scikit-learn/scikit-learn@42cc05c · GitHub
[go: up one dir, main page]

Skip to content

Commit 42cc05c

Browse files
authored
DOC Fixes visualization docs and runs code to plot (#22579)
1 parent 578e3eb commit 42cc05c

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

doc/conf.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
from github_link import make_linkcode_resolve
3131
import sphinx_gallery
32-
import matplotlib as mpl
3332
from sphinx_gallery.sorting import ExampleTitleSortKey
3433

3534
# -- General configuration ---------------------------------------------------
@@ -50,22 +49,19 @@
5049
"sphinx-prompt",
5150
"sphinxext.opengraph",
5251
"doi_role",
52+
"matplotlib.sphinxext.plot_directive",
5353
]
5454

55-
# Support for `plot::` directives in sphinx 3.2 requires matplotlib 3.1.0 or newer
56-
if parse(mpl.__version__) >= parse("3.1.0"):
57-
extensions.append("matplotlib.sphinxext.plot_directive")
55+
# Produce `plot::` directives for examples that contain `import matplotlib` or
56+
# `from matplotlib import`.
57+
numpydoc_use_plots = True
5858

59-
# Produce `plot::` directives for examples that contain `import matplotlib` or
60-
# `from matplotlib import`.
61-
numpydoc_use_plots = True
62-
63-
# Options for the `::plot` directive:
64-
# https://matplotlib.org/stable/api/sphinxext_plot_directive_api.html
65-
plot_formats = ["png"]
66-
plot_include_source = True
67-
plot_html_show_formats = False
68-
plot_html_show_source_link = False
59+
# Options for the `::plot` directive:
60+
# https://matplotlib.org/stable/api/sphinxext_plot_directive_api.html
61+
plot_formats = ["png"]
62+
plot_include_source = True
63+
plot_html_show_formats = False
64+
plot_html_show_source_link = False
6965

7066
# this is needed for some reason...
7167
# see https://github.com/numpy/numpydoc/issues/69

doc/visualizations.rst

Lines changed: 9 additions & 13 deletions
instead. In the following example, we plot a ROC curve for a fitted support
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@ like to only compute the predictions once and one should use `from_predictions`
2020
2121
vector machine:
2222

23-
.. code-block:: python
23+
.. plot::
24+
:context:
25+
:align: center
2426

2527
from sklearn.model_selection import train_test_split
2628
from sklearn.svm import SVC
2729
from sklearn.metrics import RocCurveDisplay
2830
from sklearn.datasets import load_wine
2931

32+
X, y = load_wine(return_X_y=True)
33+
y = y == 2 # make binary
3034
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
3135
svc = SVC(random_state=42)
3236
svc.fit(X_train, y_train)
3337

3438
svc_disp = RocCurveDisplay.from_estimator(svc, X_test, y_test)
3539

36-
.. figure:: auto_examples/miscellaneous/images/sphx_glr_plot_roc_curve_visualization_api_001.png
37-
:target: auto_examples/miscellaneous/plot_roc_curve_visualization_api.html
38-
:align: center
39-
:scale: 75%
40-
4140
The returned `svc_disp` object allows us to continue using the already computed
4241
ROC curve for SVC in future plots. In this case, the `svc_disp` is a
4342
:class:`~sklearn.metrics.RocCurveDisplay` that stores the computed values as
@@ -47,23 +46,20 @@ instead of `from_estimator` Next, we train a random forest classifier and plot
4746
the previously computed roc curve again by using the `plot` method of the
4847
`Display` object.
4948

50-
.. code-block:: python
49+
.. plot::
50+
:context: close-figs
51+
:align: center
5152

5253
import matplotlib.pyplot as plt
5354
from sklearn.ensemble import RandomForestClassifier
5455

55-
rfc = RandomForestClassifier(random_state=42)
56+
rfc = RandomForestClassifier(n_estimators=10, random_state=42)
5657
rfc.fit(X_train, y_train)
5758

5859
ax = plt.gca()
5960
rfc_disp = RocCurveDisplay.from_estimator(rfc, X_test, y_test, ax=ax, alpha=0.8)
6061
svc_disp.plot(ax=ax, alpha=0.8)
6162

6 62E0 2-
.. figure:: auto_examples/miscellaneous/images/sphx_glr_plot_roc_curve_visualization_api_002.png
63-
:target: auto_examples/miscellaneous/plot_roc_curve_visualization_api.html
64-
:align: center
65-
:scale: 75%
66-
6763
Notice that we pass `alpha=0.8` to the plot functions to adjust the alpha
6864
values of the curves.
6965

0 commit comments

Comments
 (0)
0