Spec-Zone.ru › scikit-learn

Note

Go to the end to download the full example code or to run this example in your browser via JupyterLite or Binder.

ROC Curve with Visualization API

Scikit-learn defines a simple API for creating visualizations for machine learning. The key features of this API is to allow for quick plotting and visual adjustments without recalculation. In this example, we will demonstrate how to use the visualization API by comparing ROC curves.

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

Load Data and Train an SVC

First, we load the wine dataset and convert it to a binary classification problem. Then, we train a support vector classifier on a training dataset.

import matplotlib.pyplot as plt

from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import RocCurveDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

X, y = load_wine(return_X_y=True)
y = y == 2

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
svc = SVC(random_state=42)
svc.fit(X_train, y_train)
SVC(random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
random_state random_state: int, RandomState instance or None, default=None

Controls the pseudo random number generation for shuffling the data for
probability estimates. Ignored when `probability` is False.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
42
C C: float, default=1.0

Regularization parameter. The strength of the regularization is
inversely proportional to C. Must be strictly positive. The penalty
is a squared l2 penalty. For an intuitive visualization of the effects
of scaling the regularization parameter C, see
:ref:`sphx_glr_auto_examples_svm_plot_svm_scale_c.py`.
1.0
kernel kernel: {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, default='rbf'

Specifies the kernel type to be used in the algorithm. If
none is given, 'rbf' will be used. If a callable is given it is used to
pre-compute the kernel matrix from data matrices; that matrix should be
an array of shape ``(n_samples, n_samples)``. For an intuitive
visualization of different kernel types see
:ref:`sphx_glr_auto_examples_svm_plot_svm_kernels.py`.
'rbf'
degree degree: int, default=3

Degree of the polynomial kernel function ('poly').
Must be non-negative. Ignored by all other kernels.
3
gamma gamma: {'scale', 'auto'} or float, default='scale'

Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.

- if ``gamma='scale'`` (default) is passed then it uses
1 / (n_features * X.var()) as value of gamma,
- if 'auto', uses 1 / n_features
- if float, must be non-negative.

.. versionchanged:: 0.22
The default value of ``gamma`` changed from 'auto' to 'scale'.
'scale'
coef0 coef0: float, default=0.0

Independent term in kernel function.
It is only significant in 'poly' and 'sigmoid'.
0.0
shrinking shrinking: bool, default=True

Whether to use the shrinking heuristic.
See the :ref:`User Guide <shrinking_svm>`.
True
probability probability: bool, default=False

Whether to enable probability estimates. This must be enabled prior
to calling `fit`, will slow down that method as it internally uses
5-fold cross-validation, and `predict_proba` may be inconsistent with
`predict`. Read more in the :ref:`User Guide <scores_probabilities>`.

..deprecated:: 1.9
The `probability` parameter is deprecated and will be removed in 1.11.
Use `CalibratedClassifierCV(SVC(), ensemble=False)` instead of
`SVC(probability=True)`.
'deprecated'
tol tol: float, default=1e-3

Tolerance for stopping criterion.
0.001
cache_size cache_size: float, default=200

Specify the size of the kernel cache (in MB).
200
class_weight class_weight: dict or 'balanced', default=None

Set the parameter C of class i to class_weight[i]*C for
SVC. If not given, all classes are supposed to have
weight one.
The "balanced" mode uses the values of y to automatically adjust
weights inversely proportional to class frequencies in the input data
as ``n_samples / (n_classes * np.bincount(y))``.
None
verbose verbose: bool, default=False

Enable verbose output. Note that this setting takes advantage of a
per-process runtime setting in libsvm that, if enabled, may not work
properly in a multithreaded context.
False
max_iter max_iter: int, default=-1

Hard limit on iterations within solver, or -1 for no limit.
-1
decision_function_shape decision_function_shape: {'ovo', 'ovr'}, default='ovr'

Whether to return a one-vs-rest ('ovr') decision function of shape
(n_samples, n_classes) as all other classifiers, or the original
one-vs-one ('ovo') decision function of libsvm which has shape
(n_samples, n_classes * (n_classes - 1) / 2). However, note that
internally, one-vs-one ('ovo') is always used as a multi-class strategy
to train models; an ovr matrix is only constructed from the ovo matrix.
The parameter is ignored for binary classification.

.. versionchanged:: 0.19
decision_function_shape is 'ovr' by default.

.. versionadded:: 0.17
*decision_function_shape='ovr'* is recommended.

.. versionchanged:: 0.17
Deprecated *decision_function_shape='ovo' and None*.
'ovr'
break_ties break_ties: bool, default=False

If true, ``decision_function_shape='ovr'``, and number of classes > 2,
:term:`predict` will break ties according to the confidence values of
:term:`decision_function`; otherwise the first class among the tied
classes is returned. Please note that breaking ties comes at a
relatively high computational cost compared to a simple predict. See
:ref:`sphx_glr_auto_examples_svm_plot_svm_tie_breaking.py` for an
example of its usage with ``decision_function_shape='ovr'``.

.. versionadded:: 0.22
False
Fitted attributes
Name Type Value
class_weight_ class_weight_: ndarray of shape (n_classes,)

Multipliers of parameter C for each class.
Computed based on the ``class_weight`` parameter.
ndarray[float64](2,) [1.,1.]
classes_ classes_: ndarray of shape (n_classes,)

The classes labels.
ndarray[bool](2,) [False, True]
dual_coef_ dual_coef_: ndarray or sparse array/matrix of shape (n_classes -1, n_SV)

Dual coefficients of the support vector in the decision
function (see :ref:`sgd_mathematical_formulation`), multiplied by
their targets.
For multiclass, coefficient for all 1-vs-1 classifiers.
The layout of the coefficients in the multiclass case is somewhat
non-trivial. See the :ref:`multi-class section of the User Guide
<svm_multi_class>` for details.
If `X` is sparse, these will also be sparse.
ndarray[float64](1, 73) [[-1.,-1.,-1.,..., 1., 1., 1.]]
fit_status_ fit_status_: int

0 if correctly fitted, 1 otherwise (will raise warning)
int 0
intercept_ intercept_: ndarray of shape (n_classes * (n_classes - 1) / 2,)

Constants in decision function.
ndarray[float64](1,) [-1.1]
n_features_in_ n_features_in_: int

Number of features seen during :term:`fit`.

.. versionadded:: 0.24
int 13
n_iter_ n_iter_: ndarray of shape (n_classes * (n_classes - 1) // 2,)

Number of iterations run by the optimization routine to fit the model.
The shape of this attribute depends on the number of models optimized
which in turn depends on the number of classes.

.. versionadded:: 1.1
ndarray[int32](1,) [45]
n_support_ n_support_: ndarray of shape (n_classes,), dtype=int32

Number of support vectors for each class.
ndarray[int32](2,) [37,36]
probA_ probA_: ndarray of shape (n_classes * (n_classes - 1) / 2)

If `probability=True`, it corresponds to the parameters learned in
Platt scaling to produce probability estimates from decision values.
If `probability=False`, it's an empty array. Platt scaling uses the
logistic function
ndarray[float64](0,) []
probB_ probB_: ndarray of shape (n_classes * (n_classes - 1) / 2)

If `probability=True`, it corresponds to the parameters learned in
Platt scaling. Platt scaling uses the logistic function
``1 / (1 + exp(decision_value * probA_ + probB_))``
where ``probA_`` and ``probB_`` are learned from the dataset [2]_. For
more information on the multiclass case and training procedure see
section 8 of [1]_.

.. deprecated:: 1.9
The attributes `probA_` and `probB_` are deprecated in version 1.9 and will
be removed in 1.11.
ndarray[float64](0,) []
shape_fit_ shape_fit_: tuple of int of shape (n_dimensions_of_X,)

Array dimensions of training vector ``X``.
tuple (133, 13)
support_ support_: ndarray of shape (n_SV)

Indices of support vectors.
ndarray[int32](73,) [ 1, 10, 11,...,118,119,126]
support_vectors_ support_vectors_: ndarray or sparse array/matrix of shape (n_SV, n_features)

Support vectors. An empty array if kernel is precomputed.
If `X` is sparse, these will also be sparse.
ndarray[float64](73, 13) [[ 12.08, 2.08, 1.7 ,..., 1.27, 2.96,710. ], [ 12.51, 1.73, 1.98,..., 1.04, 3.57,672. ], [ 12.33, 0.99, 1.95,..., 1.06, 2.31,750. ], ..., [ 12.86, 1.35, 2.32,..., 0.76, 1.29,630. ], [ 12.79, 2.67, 2.48,..., 0.48, 1.47,480. ], [ 14.13, 4.1 , 2.74,..., 0.61, 1.6 ,560. ]]


Plotting the ROC Curve

Next, we plot the ROC curve with a single call to sklearn.metrics.RocCurveDisplay.from_estimator. The returned svc_disp object allows us to continue using the already computed ROC curve for the SVC in future plots.

svc_disp = RocCurveDisplay.from_estimator(svc, X_test, y_test)
plt.show()
plot roc curve visualization api

Training a Random Forest and Plotting the ROC Curve

We train a random forest classifier and create a plot comparing it to the SVC ROC curve. Notice how svc_disp uses plot to plot the SVC ROC curve without recomputing the values of the roc curve itself. Furthermore, we pass alpha=0.8 to the plot functions to adjust the alpha values of the curves.

rfc = RandomForestClassifier(n_estimators=10, random_state=42)
rfc.fit(X_train, y_train)
ax = plt.gca()
rfc_disp = RocCurveDisplay.from_estimator(
    rfc, X_test, y_test, ax=ax, curve_kwargs=dict(alpha=0.8)
)
svc_disp.plot(ax=ax, curve_kwargs=dict(alpha=0.8))
plt.show()
plot roc curve visualization api

Total running time of the script: (0 minutes 0.152 seconds)

Launch binder
Launch JupyterLite

Download Jupyter notebook: plot_roc_curve_visualization_api.ipynb

Download Python source code: plot_roc_curve_visualization_api.py

Download zipped: plot_roc_curve_visualization_api.zip

Related examples

Receiver Operating Characteristic (ROC) with cross validation

Multiclass Receiver Operating Characteristic (ROC)

Release Highlights for scikit-learn 0.22

Detection error tradeoff (DET) curve

© 2007–2026 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.9/auto_examples/miscellaneous/plot_roc_curve_visualization_api.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API