
    -i4                       S r SSKrSSKJr  SSKJrJr  SSKrSSK	J
r
  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJrJr  SSKJrJr  SSKJrJrJr  SSK J!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'J(r(  SSK)J*r*J+r+  \" S/S/S.SS9S 5       r,\" S/S/\" 1 Sk5      S/\\-S/SS/S.SS9SSSS.S j5       r.\" S/S/\\-SS/SS/S/S.SS9 SES! j5       r/SFS" jr0\" S/S/\" 1 Sk5      S/SS/\" \S#SS$S%9S/\" 1 S&k5      /SS/S'.SS9SSSS(SS).S* j5       r1S+ r2SFS, jr3\" S/S/\\-SS/SS/S/S.SS9SSS S-.S. j5       r4\" S/S/\\-SS/SS/S/S.SS9SSSS-.S/ j5       r5\" SS0/S/SS/S1.SS9SS2.S3 j5       r6\" S/S/SS/S1.SS9SS2.S4 j5       r7\" SS0/S/SS/S1.SS9SS2.S5 j5       r8SGS6 jr9S7 r:S8 r;\" S/S/\" \SSS9S%9S/\" \S#SS:S%9/SS/S/S;.SS9SSSS S<.S= j5       r<SHS> jr=\" S/S/\" \SSS9S%9S/SS/S/S?.SS9SSS S@.SA j5       r>\" S/S/\" \SSS9S%9/S/SS/SS/SB.SS9SSSSSC.SD j5       r?g)Ia  Metrics to assess performance on classification task given scores.

Functions named as ``*_score`` return a scalar value to maximize: the higher
the better.

Function named as ``*_error`` or ``*_loss`` return a scalar value to minimize:
the lower the better.
    N)partial)IntegralReal)	trapezoid)
csr_matrixissparse)rankdata   )UndefinedMetricWarning)label_binarize)assert_all_finitecheck_arraycheck_consistent_lengthcolumn_or_1d)_encode_unique)Interval
StrOptionsvalidate_params)stable_cumsum)type_of_target)count_nonzero)_check_pos_label_consistency_check_sample_weight   )_average_binary_score_average_multiclass_ovo_scorez
array-like)xyT)prefer_skip_nested_validationc                 "   [        X5        [        U 5      n [        U5      nU R                  S   S:  a  [        SU R                  -  5      eSn[        R
                  " U 5      n[        R                  " US:  5      (       a;  [        R                  " US:*  5      (       a  SnO[        SR                  U 5      5      eU[        X5      -  n[        U[        R                  5      (       a  UR                  R                  U5      n[        U5      $ )a  Compute Area Under the Curve (AUC) using the trapezoidal rule.

This is a general function, given points on a curve.  For computing the
area under the ROC-curve, see :func:`roc_auc_score`.  For an alternative
way to summarize a precision-recall curve, see
:func:`average_precision_score`.

Parameters
----------
x : array-like of shape (n,)
    X coordinates. These must be either monotonic increasing or monotonic
    decreasing.
y : array-like of shape (n,)
    Y coordinates.

Returns
-------
auc : float
    Area Under the Curve.

See Also
--------
roc_auc_score : Compute the area under the ROC curve.
average_precision_score : Compute average precision from prediction scores.
precision_recall_curve : Compute precision-recall pairs for different
    probability thresholds.

Examples
--------
>>> import numpy as np
>>> from sklearn import metrics
>>> y_true = np.array([1, 1, 2, 2])
>>> y_score = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y_true, y_score, pos_label=2)
>>> metrics.auc(fpr, tpr)
0.75
r   r
   zJAt least 2 points are needed to compute area under curve, but x.shape = %sr   z,x is neither increasing nor decreasing : {}.)r   r   shape
ValueErrornpdiffanyallformatr   
isinstancememmapdtypetypefloat)r   r   	directiondxareas        K/var/www/html/venv/lib/python3.13/site-packages/sklearn/metrics/_ranking.pyaucr3   '   s    T A!QAQAwwqzA~Xgg
 	

 I	B	vvb1f~~66"'??IKRRSTUVVy&D$		"" zzt$;    >   macromicrosamplesweightedboolean)y_truey_scoreaverage	pos_labelsample_weightr5   )r<   r=   r>   c                X    SS jn[        U SS9n[        R                  " U 5      R                  5       nUS:X  a&  [	        U5      S:X  a  X7;  a  [        SU SU 35      eO7US	:X  a  US:w  a  [        S
5      eUS:X  a  US:w  a  [        S5      e[        XS9n [        XSS9n[        XXUS9$ )a  Compute average precision (AP) from prediction scores.

AP summarizes a precision-recall curve as the weighted mean of precisions
achieved at each threshold, with the increase in recall from the previous
threshold used as the weight:

.. math::
    \text{AP} = \sum_n (R_n - R_{n-1}) P_n

where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
threshold [1]_. This implementation is not interpolated and is different
from computing the area under the precision-recall curve with the
trapezoidal rule, which uses linear interpolation and can be too
optimistic.

Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.

Parameters
----------
y_true : array-like of shape (n_samples,) or (n_samples, n_classes)
    True binary labels or binary label indicators.

y_score : array-like of shape (n_samples,) or (n_samples, n_classes)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by :term:`decision_function` on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

average : {'micro', 'samples', 'weighted', 'macro'} or None,             default='macro'
    If ``None``, the scores for each class are returned. Otherwise,
    this determines the type of averaging performed on the data:

    ``'micro'``:
        Calculate metrics globally by considering each element of the label
        indicator matrix as a label.
    ``'macro'``:
        Calculate metrics for each label, and find their unweighted
        mean.  This does not take label imbalance into account.
    ``'weighted'``:
        Calculate metrics for each label, and find their average, weighted
        by support (the number of true instances for each label).
    ``'samples'``:
        Calculate metrics for each instance, and find their average.

    Will be ignored when ``y_true`` is binary.

pos_label : int, float, bool or str, default=1
    The label of the positive class. Only applied to binary ``y_true``.
    For multilabel-indicator ``y_true``, ``pos_label`` is fixed to 1.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
average_precision : float
    Average precision score.

See Also
--------
roc_auc_score : Compute the area under the ROC curve.
precision_recall_curve : Compute precision-recall pairs for different
    probability thresholds.
PrecisionRecallDisplay.from_estimator : Plot the precision recall curve
    using an estimator and data.
PrecisionRecallDisplay.from_predictions : Plot the precision recall curve
    using true and predicted labels.

Notes
-----
.. versionchanged:: 0.19
  Instead of linearly interpolating between operating points, precisions
  are weighted by the change in recall since the last operating point.

References
----------
.. [1] `Wikipedia entry for the Average precision
       <https://en.wikipedia.org/w/index.php?title=Information_retrieval&
       oldid=793358396#Average_precision>`_

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import average_precision_score
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> average_precision_score(y_true, y_scores)
0.83
>>> y_true = np.array([0, 0, 1, 1, 2, 2])
>>> y_scores = np.array([
...     [0.7, 0.2, 0.1],
...     [0.4, 0.3, 0.3],
...     [0.1, 0.8, 0.1],
...     [0.2, 0.3, 0.5],
...     [0.4, 0.4, 0.2],
...     [0.1, 0.2, 0.7],
... ])
>>> average_precision_score(y_true, y_scores)
0.77
r   c                     [        XX#S9u  pEn[        [        S[        R                  " [        R
                  " U5      [        R                  " U5      S S -  5      * 5      5      $ )Nr=   r>           r"   )precision_recall_curver.   maxr%   sumr&   array)r:   r;   r=   r>   	precisionrecall_s          r2   (_binary_uninterpolated_average_precisionIaverage_precision_score.<locals>._binary_uninterpolated_average_precision   sU      6y 
	1 Srvvbggfo8KCR8P&PQQRSSr4   r:   
input_namebinaryr
   z
pos_label=z+ is not a valid label. It should be one of multilabel-indicatorznParameter pos_label is fixed to 1 for multilabel-indicator y_true. Do not set pos_label or set pos_label to 1.
multiclasszdParameter pos_label is fixed to 1 for multiclass y_true. Do not set pos_label or set pos_label to 1.classes)r=   r>   )r   N)	r   r%   uniquetolistlenr$   r   r   r   )	r:   r;   r<   r=   r>   rJ   y_typepresent_labelsaverage_precisions	            r2   average_precision_scorerZ   l   s    j 59
T Fx8F YYv&--/N~!#	(GYK (()+ 
 
)	)i1n:
 	

 
<	>>   ?0 !7= r4   )r:   r;   r=   r>   drop_intermediateFc                 d   [        XX#S9u  pVn[        R                  " S/U45      n[        R                  " S/U45      n[        R                  " [        R                  /U45      nU(       a  [	        U5      S:  a  [        R
                  " [        R                  " S/[        R                  " [        R                  " USS 5      [        R                  " USS 5      5      S//5      5      S   nXX   nXh   nXx   n[	        [        R                  " U 5      5      S:w  a  [        S5      eUS   U-
  n	US   n
US   nUR                  US   S	S
9S:  a  UR                  US   S	S
9S-
  OSnUR                  US   5      S-   n[        X5      nX^   SSS2   U-  X   SSS2   U
-  X~   SSS2   4$ )a  Compute Detection Error Tradeoff (DET) for different probability thresholds.

.. note::
   This metric is used for evaluation of ranking and error tradeoffs of
   a binary classification task.

Read more in the :ref:`User Guide <det_curve>`.

.. versionadded:: 0.24

.. versionchanged:: 1.7
   An arbitrary threshold at infinity is added to represent a classifier
   that always predicts the negative class, i.e. `fpr=0` and `fnr=1`, unless
   `fpr=0` is already reached at a finite threshold.

Parameters
----------
y_true : ndarray of shape (n_samples,)
    True binary labels. If labels are not either {-1, 1} or {0, 1}, then
    pos_label should be explicitly given.

y_score : ndarray of shape of (n_samples,)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by "decision_function" on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

pos_label : int, float, bool or str, default=None
    The label of the positive class.
    When ``pos_label=None``, if `y_true` is in {-1, 1} or {0, 1},
    ``pos_label`` is set to 1, otherwise an error will be raised.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

drop_intermediate : bool, default=False
    Whether to drop thresholds where true positives (tp) do not change from
    the previous or subsequent threshold. All points with the same tp value
    have the same `fnr` and thus same y coordinate.

    .. versionadded:: 1.7

Returns
-------
fpr : ndarray of shape (n_thresholds,)
    False positive rate (FPR) such that element i is the false positive
    rate of predictions with score >= thresholds[i]. This is occasionally
    referred to as false acceptance probability or fall-out.

fnr : ndarray of shape (n_thresholds,)
    False negative rate (FNR) such that element i is the false negative
    rate of predictions with score >= thresholds[i]. This is occasionally
    referred to as false rejection or miss rate.

thresholds : ndarray of shape (n_thresholds,)
    Decreasing thresholds on the decision function (either `predict_proba`
    or `decision_function`) used to compute FPR and FNR.

    .. versionchanged:: 1.7
       An arbitrary threshold at infinity is added for the case `fpr=0`
       and `fnr=1`.

See Also
--------
DetCurveDisplay.from_estimator : Plot DET curve given an estimator and
    some data.
DetCurveDisplay.from_predictions : Plot DET curve given the true and
    predicted labels.
DetCurveDisplay : DET curve visualization.
roc_curve : Compute Receiver operating characteristic (ROC) curve.
precision_recall_curve : Compute precision-recall curve.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import det_curve
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, fnr, thresholds = det_curve(y_true, y_scores)
>>> fpr
array([0.5, 0.5, 0. ])
>>> fnr
array([0. , 0.5, 0.5])
>>> thresholds
array([0.35, 0.4 , 0.8 ])
rA   r   r
   TNr"   r   z`Only one class is present in y_true. Detection error tradeoff curve is not defined in that case.right)side)_binary_clf_curver%   concatenateinfrV   where
logical_orr&   rT   r$   searchsortedslice)r:   r;   r=   r>   r[   fpstps
thresholdsoptimal_idxsfnsp_countn_count	first_indlast_indsls                  r2   	det_curverp     s   H -9Cj ..1#s
$C
..1#s
$C"&&: 67JSX\ xxNNrwws3Bx'8"''#ab':JKdVT
 	
 -

299V":
 	

 b'C-C"gG"gG
 CF1A5 	Qg.2  B(1,H	y	#B GDbDMG#SWTrT]W%<jnTrT>RSSr4   c                 H   [        [        R                  " U 5      5      S:w  a+  [        R                  " S[
        5        [        R                  $ [        XUS9u  pEnUb  US:X  a  [        XE5      $ US::  d  US:  a  [        SU-  5      e[        R                  " XCS5      nXGS-
     XG   /nXWS-
     XW   /n	[        R                  " USU [        R                  " X8U	5      5      n[        R                  " USU U5      n[        XE5      n
S	US-  -  nUnS	SX-
  X-
  -  -   -  $ )
zBinary roc auc score.r
   zOOnly one class is present in y_true. ROC AUC score is not defined in that case.rS   Nr   r   z)Expected max_fpr in range (0, 1], got: %rr]         ?)rV   r%   rT   warningswarnr   nan	roc_curver3   r$   rd   appendinterp)r:   r;   r>   max_fprfprtprrI   stopx_interpy_interppartial_aucmin_areamax_areas                r2   _binary_roc_auc_scorer     s"   
299V"/ #	
 vvF=IKCa'Q,3}!|w{DwNOO ??31D1Hsy)H1Hsy)H
))CJ		'X F
GC
))CJ
(Cc-K WaZHH!{-(2EFFGGr4   rB   r]   )closed>   ovoovrraise)r:   r;   r<   r>   ry   multi_classlabelsr   )r<   r>   ry   r   r   c                   [        U SS9n[        U SSS9n [        USS9nUS:X  d)  US:X  ad  UR                  S	:X  aT  UR                  S
   S	:  aA  Ub   US:w  a  [	        SR                  U5      5      eUS:X  a  [	        S5      e[        XXeX#5      $ US:X  a?  [        R                  " U 5      n[        XS9SS2S4   n [        [        [        US9U UUUS9$ [        [        [        US9U UUUS9$ )a\  Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC)     from prediction scores.

Note: this implementation can be used with binary, multiclass and
multilabel classification, but some restrictions apply (see Parameters).

Read more in the :ref:`User Guide <roc_metrics>`.

Parameters
----------
y_true : array-like of shape (n_samples,) or (n_samples, n_classes)
    True labels or binary label indicators. The binary and multiclass cases
    expect labels with shape (n_samples,) while the multilabel case expects
    binary label indicators with shape (n_samples, n_classes).

y_score : array-like of shape (n_samples,) or (n_samples, n_classes)
    Target scores.

    * In the binary case, it corresponds to an array of shape
      `(n_samples,)`. Both probability estimates and non-thresholded
      decision values can be provided. The probability estimates correspond
      to the **probability of the class with the greater label**,
      i.e. `estimator.classes_[1]` and thus
      `estimator.predict_proba(X, y)[:, 1]`. The decision values
      corresponds to the output of `estimator.decision_function(X, y)`.
      See more information in the :ref:`User guide <roc_auc_binary>`;
    * In the multiclass case, it corresponds to an array of shape
      `(n_samples, n_classes)` of probability estimates provided by the
      `predict_proba` method. The probability estimates **must**
      sum to 1 across the possible classes. In addition, the order of the
      class scores must correspond to the order of ``labels``,
      if provided, or else to the numerical or lexicographical order of
      the labels in ``y_true``. See more information in the
      :ref:`User guide <roc_auc_multiclass>`;
    * In the multilabel case, it corresponds to an array of shape
      `(n_samples, n_classes)`. Probability estimates are provided by the
      `predict_proba` method and the non-thresholded decision values by
      the `decision_function` method. The probability estimates correspond
      to the **probability of the class with the greater label for each
      output** of the classifier. See more information in the
      :ref:`User guide <roc_auc_multilabel>`.

average : {'micro', 'macro', 'samples', 'weighted'} or None,             default='macro'
    If ``None``, the scores for each class are returned.
    Otherwise, this determines the type of averaging performed on the data.
    Note: multiclass ROC AUC currently only handles the 'macro' and
    'weighted' averages. For multiclass targets, `average=None` is only
    implemented for `multi_class='ovr'` and `average='micro'` is only
    implemented for `multi_class='ovr'`.

    ``'micro'``:
        Calculate metrics globally by considering each element of the label
        indicator matrix as a label.
    ``'macro'``:
        Calculate metrics for each label, and find their unweighted
        mean.  This does not take label imbalance into account.
    ``'weighted'``:
        Calculate metrics for each label, and find their average, weighted
        by support (the number of true instances for each label).
    ``'samples'``:
        Calculate metrics for each instance, and find their average.

    Will be ignored when ``y_true`` is binary.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

max_fpr : float > 0 and <= 1, default=None
    If not ``None``, the standardized partial AUC [2]_ over the range
    [0, max_fpr] is returned. For the multiclass case, ``max_fpr``,
    should be either equal to ``None`` or ``1.0`` as AUC ROC partial
    computation currently is not supported for multiclass.

multi_class : {'raise', 'ovr', 'ovo'}, default='raise'
    Only used for multiclass targets. Determines the type of configuration
    to use. The default value raises an error, so either
    ``'ovr'`` or ``'ovo'`` must be passed explicitly.

    ``'ovr'``:
        Stands for One-vs-rest. Computes the AUC of each class
        against the rest [3]_ [4]_. This
        treats the multiclass case in the same way as the multilabel case.
        Sensitive to class imbalance even when ``average == 'macro'``,
        because class imbalance affects the composition of each of the
        'rest' groupings.
    ``'ovo'``:
        Stands for One-vs-one. Computes the average AUC of all
        possible pairwise combinations of classes [5]_.
        Insensitive to class imbalance when
        ``average == 'macro'``.

labels : array-like of shape (n_classes,), default=None
    Only used for multiclass targets. List of labels that index the
    classes in ``y_score``. If ``None``, the numerical or lexicographical
    order of the labels in ``y_true`` is used.

Returns
-------
auc : float
    Area Under the Curve score.

See Also
--------
average_precision_score : Area under the precision-recall curve.
roc_curve : Compute Receiver operating characteristic (ROC) curve.
RocCurveDisplay.from_estimator : Plot Receiver Operating Characteristic
    (ROC) curve given an estimator and some data.
RocCurveDisplay.from_predictions : Plot Receiver Operating Characteristic
    (ROC) curve given the true and predicted values.

Notes
-----
The Gini Coefficient is a summary measure of the ranking ability of binary
classifiers. It is expressed using the area under of the ROC as follows:

G = 2 * AUC - 1

Where G is the Gini coefficient and AUC is the ROC-AUC score. This normalisation
will ensure that random guessing will yield a score of 0 in expectation, and it is
upper bounded by 1.

References
----------
.. [1] `Wikipedia entry for the Receiver operating characteristic
        <https://en.wikipedia.org/wiki/Receiver_operating_characteristic>`_

.. [2] `Analyzing a portion of the ROC curve. McClish, 1989
        <https://www.ncbi.nlm.nih.gov/pubmed/2668680>`_

.. [3] Provost, F., Domingos, P. (2000). Well-trained PETs: Improving
       probability estimation trees (Section 6.2), CeDER Working Paper
       #IS-00-04, Stern School of Business, New York University.

.. [4] `Fawcett, T. (2006). An introduction to ROC analysis. Pattern
        Recognition Letters, 27(8), 861-874.
        <https://www.sciencedirect.com/science/article/pii/S016786550500303X>`_

.. [5] `Hand, D.J., Till, R.J. (2001). A Simple Generalisation of the Area
        Under the ROC Curve for Multiple Class Classification Problems.
        Machine Learning, 45(2), 171-186.
        <http://link.springer.com/article/10.1023/A:1010920819831>`_
.. [6] `Wikipedia entry for the Gini coefficient
        <https://en.wikipedia.org/wiki/Gini_coefficient>`_

Examples
--------
Binary case:

>>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.metrics import roc_auc_score
>>> X, y = load_breast_cancer(return_X_y=True)
>>> clf = LogisticRegression(solver="newton-cholesky", random_state=0).fit(X, y)
>>> roc_auc_score(y, clf.predict_proba(X)[:, 1])
0.99
>>> roc_auc_score(y, clf.decision_function(X))
0.99

Multiclass case:

>>> from sklearn.datasets import load_iris
>>> X, y = load_iris(return_X_y=True)
>>> clf = LogisticRegression(solver="newton-cholesky").fit(X, y)
>>> roc_auc_score(y, clf.predict_proba(X), multi_class='ovr')
0.99

Multilabel case:

>>> import numpy as np
>>> from sklearn.datasets import make_multilabel_classification
>>> from sklearn.multioutput import MultiOutputClassifier
>>> X, y = make_multilabel_classification(random_state=0)
>>> clf = MultiOutputClassifier(clf).fit(X, y)
>>> # get a list of n_output containing probability arrays of shape
>>> # (n_samples, n_classes)
>>> y_score = clf.predict_proba(X)
>>> # extract the positive columns for each output
>>> y_score = np.transpose([score[:, 1] for score in y_score])
>>> roc_auc_score(y, y_score, average=None)
array([0.828, 0.852, 0.94, 0.869, 0.95])
>>> from sklearn.linear_model import RidgeClassifierCV
>>> clf = RidgeClassifierCV().fit(X, y)
>>> roc_auc_score(y, clf.decision_function(X), average=None)
array([0.82, 0.847, 0.93, 0.872, 0.944])
r:   rL   FN	ensure_2dr,   r   rP   rN   r
   r         ?z|Partial AUC computation not available in multiclass setting, 'max_fpr' must be set to `None`, received `max_fpr={0}` insteadr   z%multi_class must be in ('ovo', 'ovr')rQ   r   )ry   rS   )r   r   ndimr#   r$   r)   _multiclass_roc_auc_scorer%   rT   r   r   r   r   )r:   r;   r<   r>   ry   r   r   rW   s           r2   roc_auc_scorer     s    b Fx8F5=F'U3G(w||q0W]]15E5I 7c> !&/	  '!DEE(V'
 	
 
8	6"71=$)7;'
 	
 %)7;'
 	
r4   c                    [         R                  " SUR                  SS95      (       d  [        S5      eSnUS:X  a  SU-   nXF;  a  [        SR	                  U5      5      eSnX7;  a  [        S	R	                  X75      5      eUc  US
:X  a  [        S5      eUb  [        U5      n[        U5      n[        U5      [        U5      :w  a  [        S5      e[         R                  " X5      (       d  [        S5      e[        U5      UR                  S   :w  a1  [        SR	                  [        U5      UR                  S   5      5      e[        [         R                  " X5      5      (       a  [        S5      eO2[        U 5      n[        U5      UR                  S   :w  a  [        S5      eUS
:X  a&  Ub  [        S5      e[        XS9n	[        [        XUS9$ [        XS9n
[!        [        U
UUUS9$ )a  Multiclass roc auc score.

Parameters
----------
y_true : array-like of shape (n_samples,)
    True multiclass labels.

y_score : array-like of shape (n_samples, n_classes)
    Target scores corresponding to probability estimates of a sample
    belonging to a particular class

labels : array-like of shape (n_classes,) or None
    List of labels to index ``y_score`` used for multiclass. If ``None``,
    the lexical order of ``y_true`` is used to index ``y_score``.

multi_class : {'ovr', 'ovo'}
    Determines the type of multiclass configuration to use.
    ``'ovr'``:
        Calculate metrics for the multiclass case using the one-vs-rest
        approach.
    ``'ovo'``:
        Calculate metrics for the multiclass case using the one-vs-one
        approach.

average : {'micro', 'macro', 'weighted'}
    Determines the type of averaging performed on the pairwise binary
    metric scores
    ``'micro'``:
        Calculate metrics for the binarized-raveled classes. Only supported
        for `multi_class='ovr'`.

    .. versionadded:: 1.2

    ``'macro'``:
        Calculate metrics for each label, and find their unweighted
        mean. This does not take label imbalance into account. Classes
        are assumed to be uniformly distributed.
    ``'weighted'``:
        Calculate metrics for each label, taking into account the
        prevalence of the classes.

sample_weight : array-like of shape (n_samples,) or None
    Sample weights.

r   axiszjTarget scores need to be probabilities for multiclass roc_auc, i.e. they should sum up to 1.0 over classes)r5   r8   Nr   )r6   z2average must be one of {0} for multiclass problems)r   r   zUmulti_class='{0}' is not supported for multiclass ROC AUC, multi_class must be in {1}r   z6average=None is not implemented for multi_class='ovo'.z!Parameter 'labels' must be uniquez"Parameter 'labels' must be orderedzQNumber of given labels, {0}, not equal to the number of columns in 'y_score', {1}z2'y_true' contains labels not in parameter 'labels'zKNumber of classes in y_true not equal to the number of columns in 'y_score'zlsample_weight is not supported for multiclass one-vs-one ROC AUC, 'sample_weight' must be None in this case.uniques)r<   rQ   rS   )r%   allcloserE   r$   r)   NotImplementedErrorr   r   rV   array_equalr#   	setdiff1dr   r   r   r   r   )r:   r;   r   r   r<   r>   average_optionsmulticlass_optionsrR   y_true_encodedy_true_multilabels              r2   r   r     s   b ;;q'++1+-..C
 	
 2Oe$6%@GGX
 	
 (,VK<
 	
 ;%/!D
 	
 f%&/w<3v;&@AA~~g..ABBw<7==++//5vc'lGMMRSDT/U  r||F,--QRR . &/w<7==++' 
 e$= 
 !9,!>G
 	

 +6C$!'
 	
r4   c                    [        U SS9nUS:X  d#  US:X  a  Uc  [        SR                  U5      5      e[        XU5        [	        U 5      n [	        U5      n[        U 5        [        U5        Ub'  [	        U5      n[        X05      nUS:g  nX   n X   nX5   n[        X 5      nX:H  n [        R                  " USS	9SSS
2   nX   nX   n Ub  X6   nOSn[        R                  " [        R                  " U5      5      S   n[        R                  XR                  S-
  4   n	[        X-  5      U	   n
Ub  [        SU -
  U-  5      U	   nOSU	-   U
-
  nXX   4$ )aP  Calculate true and false positives per binary classification threshold.

Parameters
----------
y_true : ndarray of shape (n_samples,)
    True targets of binary classification.

y_score : ndarray of shape (n_samples,)
    Estimated probabilities or output of a decision function.

pos_label : int, float, bool or str, default=None
    The label of the positive class.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
fps : ndarray of shape (n_thresholds,)
    A count of false positives, at index i being the number of negative
    samples assigned a score >= thresholds[i]. The total number of
    negative samples is equal to fps[-1] (thus true negatives are given by
    fps[-1] - fps).

tps : ndarray of shape (n_thresholds,)
    An increasing count of true positives, at index i being the number
    of positive samples assigned a score >= thresholds[i]. The total
    number of positive samples is equal to tps[-1] (thus false negatives
    are given by tps[-1] - tps).

thresholds : ndarray of shape (n_thresholds,)
    Decreasing score values.
r:   rL   rN   rP   N{0} format is not supportedr   	mergesort)kindr"   r   r   )r   r$   r)   r   r   r   r   r   r%   argsortrb   r&   r_sizer   )r:   r;   r=   r>   rW   nonzero_weight_maskdesc_score_indicesweightdistinct_value_indicesthreshold_idxsrg   rf   s               r2   r_   r_   :  s   F Fx8Fh6\#9i>S6==fEFFF];&!F7#Gfg  $]3,]C+q0,.%:,Y?I  F G+>ttD)G'F 2
  XXbggg&67:UU1;;?BCN 
(
8C  QZ612>B. 3&W,,,r4   )r=   r>   r[   c                   [        XX#S9u  pVnU(       a  [        U5      S:  a  [        R                  " [        R                  " S/[        R
                  " [        R                  " USS 5      [        R                  " USS 5      5      S//5      5      S   nXX   nXh   nXx   nXe-   n	[        R                  " U5      n
[        R                  " XiXS:g  S9  US   S:X  a-  [        R                  " S	5        [        R                  " U5      nOXfS   -  n[        SSS5      n[        R                  " X   S45      [        R                  " X   S45      X|   4$ )
a  Compute precision-recall pairs for different probability thresholds.

Note: this implementation is restricted to the binary classification task.

The precision is the ratio ``tp / (tp + fp)`` where ``tp`` is the number of
true positives and ``fp`` the number of false positives. The precision is
intuitively the ability of the classifier not to label as positive a sample
that is negative.

The recall is the ratio ``tp / (tp + fn)`` where ``tp`` is the number of
true positives and ``fn`` the number of false negatives. The recall is
intuitively the ability of the classifier to find all the positive samples.

The last precision and recall values are 1. and 0. respectively and do not
have a corresponding threshold. This ensures that the graph starts on the
y axis.

The first precision and recall values are precision=class balance and recall=1.0
which corresponds to a classifier that always predicts the positive class.

Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.

Parameters
----------
y_true : array-like of shape (n_samples,)
    True binary labels. If labels are not either {-1, 1} or {0, 1}, then
    pos_label should be explicitly given.

y_score : array-like of shape (n_samples,)
    Target scores, can either be probability estimates of the positive
    class, or non-thresholded measure of decisions (as returned by
    `decision_function` on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

pos_label : int, float, bool or str, default=None
    The label of the positive class.
    When ``pos_label=None``, if y_true is in {-1, 1} or {0, 1},
    ``pos_label`` is set to 1, otherwise an error will be raised.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

drop_intermediate : bool, default=False
    Whether to drop some suboptimal thresholds which would not appear
    on a plotted precision-recall curve. This is useful in order to create
    lighter precision-recall curves.

    .. versionadded:: 1.3

Returns
-------
precision : ndarray of shape (n_thresholds + 1,)
    Precision values such that element i is the precision of
    predictions with score >= thresholds[i] and the last element is 1.

recall : ndarray of shape (n_thresholds + 1,)
    Decreasing recall values such that element i is the recall of
    predictions with score >= thresholds[i] and the last element is 0.

thresholds : ndarray of shape (n_thresholds,)
    Increasing thresholds on the decision function used to compute
    precision and recall where `n_thresholds = len(np.unique(y_score))`.

See Also
--------
PrecisionRecallDisplay.from_estimator : Plot Precision Recall Curve given
    a binary classifier.
PrecisionRecallDisplay.from_predictions : Plot Precision Recall Curve
    using predictions from a binary classifier.
average_precision_score : Compute average precision from prediction scores.
det_curve: Compute error rates for different probability thresholds.
roc_curve : Compute Receiver operating characteristic (ROC) curve.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import precision_recall_curve
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> precision, recall, thresholds = precision_recall_curve(
...     y_true, y_scores)
>>> precision
array([0.5       , 0.66666667, 0.5       , 1.        , 1.        ])
>>> recall
array([1. , 1. , 0.5, 0.5, 0. ])
>>> thresholds
array([0.1 , 0.35, 0.4 , 0.8 ])
rA   r
   TNr"   r   r   )outrb   zKNo positive class found in y_true, recall is set to one for all thresholds.)r_   rV   r%   rb   r`   rc   r&   
zeros_likedividers   rt   	ones_likere   hstack)r:   r;   r=   r>   r[   rf   rg   rh   ri   psrG   rH   ro   s                r2   rC   rC     s?   V -9Cj SX\ xxNNrwws3Bx'8"''#ab':JKdVT
 	
 -
	B c"IIIc91W6 2w!|7	
 c"2w 
tT2	B99imQ'("))VZO*DjnTTr4   c                F   [        XX#S9u  pVnU(       a  [        U5      S:  ax  [        R                  " [        R                  S[        R
                  " [        R                  " US5      [        R                  " US5      5      S4   5      S   nXX   nXh   nXx   n[        R                  SU4   n[        R                  SU4   n[        R                  [        R                  U4   nUS   S::  aK  [        R                  " S[        5        [        R                  " [        R                  UR                  5      n	OXUS   -  n	US   S::  aK  [        R                  " S[        5        [        R                  " [        R                  UR                  5      n
OXfS   -  n
XU4$ )a  Compute Receiver operating characteristic (ROC).

Note: this implementation is restricted to the binary classification task.

Read more in the :ref:`User Guide <roc_metrics>`.

Parameters
----------
y_true : array-like of shape (n_samples,)
    True binary labels. If labels are not either {-1, 1} or {0, 1}, then
    pos_label should be explicitly given.

y_score : array-like of shape (n_samples,)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by "decision_function" on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

pos_label : int, float, bool or str, default=None
    The label of the positive class.
    When ``pos_label=None``, if `y_true` is in {-1, 1} or {0, 1},
    ``pos_label`` is set to 1, otherwise an error will be raised.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

drop_intermediate : bool, default=True
    Whether to drop thresholds where the resulting point is collinear with
    its neighbors in ROC space. This has no effect on the ROC AUC or visual
    shape of the curve, but reduces the number of plotted points.

    .. versionadded:: 0.17
       parameter *drop_intermediate*.

Returns
-------
fpr : ndarray of shape (>2,)
    Increasing false positive rates such that element i is the false
    positive rate of predictions with score >= `thresholds[i]`.

tpr : ndarray of shape (>2,)
    Increasing true positive rates such that element `i` is the true
    positive rate of predictions with score >= `thresholds[i]`.

thresholds : ndarray of shape (n_thresholds,)
    Decreasing thresholds on the decision function used to compute
    fpr and tpr. The first threshold is set to `np.inf`.

    .. versionchanged:: 1.3
       An arbitrary threshold at infinity (stored in `thresholds[0]`) is
       added to represent a classifier that always predicts the negative
       class, i.e. `fpr=0` and `tpr=0`.

See Also
--------
RocCurveDisplay.from_estimator : Plot Receiver Operating Characteristic
    (ROC) curve given an estimator and some data.
RocCurveDisplay.from_predictions : Plot Receiver Operating Characteristic
    (ROC) curve given the true and predicted values.
det_curve: Compute error rates for different probability thresholds.
roc_auc_score : Compute the area under the ROC curve.

Notes
-----
Since the thresholds are sorted from low to high values, they
are reversed upon returning them to ensure they correspond to both ``fpr``
and ``tpr``, which are sorted in reversed order during their calculation.

References
----------
.. [1] `Wikipedia entry for the Receiver operating characteristic
        <https://en.wikipedia.org/wiki/Receiver_operating_characteristic>`_

.. [2] Fawcett T. An introduction to ROC analysis[J]. Pattern Recognition
       Letters, 2006, 27(8):861-874.

Examples
--------
>>> import numpy as np
>>> from sklearn import metrics
>>> y = np.array([1, 1, 2, 2])
>>> scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
>>> fpr
array([0. , 0. , 0.5, 0.5, 1. ])
>>> tpr
array([0. , 0.5, 0.5, 1. , 1. ])
>>> thresholds
array([ inf, 0.8 , 0.4 , 0.35, 0.1 ])
rA   r
   Tr   r"   zINo negative samples in y_true, false positive value should be meaninglesszHNo positive samples in y_true, true positive value should be meaningless)r_   rV   r%   rb   r   rc   r&   ra   rs   rt   r   repeatru   r#   )r:   r;   r=   r>   r[   rf   rg   rh   ri   rz   r{   s              r2   rv   rv   #  sT   P -9Cj SX\xxEE$bggc1orwwsAGMN

 -
 %%3-C
%%3-Crvvz)*J
2w!|W"	
 ii		*Gm
2w!|V"	
 ii		*GmZr4   zsparse matrix)r:   r;   r>   rS   c                   [        XU5        [        U SSS9n [        USS9nU R                  UR                  :w  a  [        S5      e[	        U SS9nUS:w  a0  US	:X  a  U R
                  S
:X  d  [        SR                  U5      5      e[        U 5      (       d  [        U 5      n U* nU R                  u  pESn[        [        U R                  U R                  SS 5      5       Hz  u  nu  pU R                  X n
U
R                  S:X  d  U
R                  U:X  a  SnO3X   n[        US5      U
   n[        X   S5      nX-  R                  5       nUb  XU   -  nXk-  nM|     Uc  Xd-  nOU[         R"                  " U5      -  n[%        U5      $ )a  Compute ranking-based average precision.

Label ranking average precision (LRAP) is the average over each ground
truth label assigned to each sample, of the ratio of true vs. total
labels with lower score.

This metric is used in multilabel ranking problem, where the goal
is to give better rank to the labels associated to each sample.

The obtained score is always strictly greater than 0 and
the best value is 1.

Read more in the :ref:`User Guide <label_ranking_average_precision>`.

Parameters
----------
y_true : {array-like, sparse matrix} of shape (n_samples, n_labels)
    True binary labels in binary indicator format.

y_score : array-like of shape (n_samples, n_labels)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by "decision_function" on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

    .. versionadded:: 0.20

Returns
-------
score : float
    Ranking-based average precision score.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import label_ranking_average_precision_score
>>> y_true = np.array([[1, 0, 0], [0, 0, 1]])
>>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]])
>>> label_ranking_average_precision_score(y_true, y_score)
0.416
Fcsrr   accept_sparser   'y_true and y_score have different shaper:   rL   rO   rN   r
   r   rB   r   Nr   r   rD   )r   r   r#   r$   r   r   r)   r   r   	enumeratezipindptrindicesr   r	   meanr%   rE   r.   )r:   r;   r>   rW   	n_samplesn_labelsr   istartr|   relevantauxscores_irankLs                  r2   %label_ranking_average_precision_scorer     s   l F];5FF'U3G||w}}$BCC Fx8F''(v{{a/6==fEFFFF#hG ,,I
C%c&--qr9J&KL=E>>%-==A(!: CzHHe,X6D+U3A8//#C$a((C
 M" rvvm$$:r4   c                   [        U SS9n [        USS9n[        XU5        [        U SS9nUS:w  a  [        SR	                  U5      5      eU R
                  UR
                  :w  a  [        S5      e[        R                  R                  U[        R                  " U 5      S9nUR                  S	S
9R                  S5      nX:  R                  S	S
9nUR                  S5      n[        [        R                  " XbS95      $ )a  Coverage error measure.

Compute how far we need to go through the ranked scores to cover all
true labels. The best value is equal to the average number
of labels in ``y_true`` per sample.

Ties in ``y_scores`` are broken by giving maximal rank that would have
been assigned to all tied values.

Note: Our implementation's score is 1 greater than the one given in
Tsoumakas et al., 2010. This extends it to handle the degenerate case
in which an instance has 0 true labels.

Read more in the :ref:`User Guide <coverage_error>`.

Parameters
----------
y_true : array-like of shape (n_samples, n_labels)
    True binary labels in binary indicator format.

y_score : array-like of shape (n_samples, n_labels)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by "decision_function" on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
coverage_error : float
    The coverage error.

References
----------
.. [1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010).
       Mining multi-label data. In Data mining and knowledge discovery
       handbook (pp. 667-685). Springer US.

Examples
--------
>>> from sklearn.metrics import coverage_error
>>> y_true = [[1, 0, 0], [0, 1, 1]]
>>> y_score = [[1, 0, 0], [0, 1, 1]]
>>> coverage_error(y_true, y_score)
1.5
Tr   r:   rL   rO   r   r   )maskr   r   )r"   r   r   weights)r   r   r   r$   r)   r#   r%   mamasked_arraylogical_notminreshaperE   filledr.   r<   )r:   r;   r>   rW   y_score_masky_min_relevantcoverages          r2   coverage_errorr   !  s    t 40F'T2GF];Fx8F''6==fEFF||w}}$BCC55%%gBNN64J%KL!%%1%-55g>N)..A.6Hq!HH<==r4   c                   [        U SSS9n [        USS9n[        XU5        [        U SS9nUS;  a  [        SR	                  U5      5      eU R
                  UR
                  :w  a  [        S	5      eU R
                  u  pE[        U 5      n [        R                  " U5      n[        [        U R                  U R                  S
S 5      5       H  u  nu  p[        R                  " X   SS9u  p[        R                  " XR                  X    [        U
5      S9n[        R                  " U[        U
5      S9nX-
  n[        R                   " UR#                  5       U5      Xg'   M     [%        U S
S9n[        R&                  " SSS9   XeU-
  U-  -  nSSS5        SU[        R(                  " US:H  X:H  5      '   [+        [        R,                  " XbS95      $ ! , (       d  f       NJ= f)a  Compute Ranking loss measure.

Compute the average number of label pairs that are incorrectly ordered
given y_score weighted by the size of the label set and the number of
labels not in the label set.

This is similar to the error set size, but weighted by the number of
relevant and irrelevant labels. The best performance is achieved with
a ranking loss of zero.

Read more in the :ref:`User Guide <label_ranking_loss>`.

.. versionadded:: 0.17
   A function *label_ranking_loss*

Parameters
----------
y_true : {array-like, sparse matrix} of shape (n_samples, n_labels)
    True binary labels in binary indicator format.

y_score : array-like of shape (n_samples, n_labels)
    Target scores, can either be probability estimates of the positive
    class, confidence values, or non-thresholded measure of decisions
    (as returned by "decision_function" on some classifiers).
    For :term:`decision_function` scores, values greater than or equal to
    zero should indicate the positive class.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
loss : float
    Average number of label pairs that are incorrectly ordered given
    y_score weighted by the size of the label set and the number of labels not
    in the label set.

References
----------
.. [1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010).
       Mining multi-label data. In Data mining and knowledge discovery
       handbook (pp. 667-685). Springer US.

Examples
--------
>>> from sklearn.metrics import label_ranking_loss
>>> y_true = [[1, 0, 0], [0, 0, 1]]
>>> y_score = [[0.75, 0.5, 1], [1, 0.2, 0.1]]
>>> label_ranking_loss(y_true, y_score)
0.75
Fr   r   r   r:   rL   )rO   r   r   r   NT)return_inverse)	minlengthr   ignore)r   invalidrB   r   r   )r   r   r   r$   r)   r#   r   r%   zerosr   r   r   rT   bincountr   rV   dotcumsumr   errstaterc   r.   r<   )r:   r;   r>   rW   r   r   lossr   r   r|   unique_scoresunique_inversetrue_at_reversed_rankall_at_reversed_rankfalse_at_reversed_rankn_positivess                   r2   label_ranking_lossr   n  s   x 5FF'U3GF];Fx8F..6==fEFF||w}}$BCC ,,IF88ID%c&--qr9J&KL=E(*		'*T(R% ">>%56#mBT!
  "{{>SEWX!5!M &&.5579OP M  Q/K	Hh	7K';66 
8
 FID{a')@	ABD899 
8	7s   G
G"c           	      t   S[         R                  " [         R                  " U R                  S   5      S-   5      [         R                  " U5      -  -  nUb  SXRS& U(       az  [         R                  " U5      SS2SSS24   nU [         R                  " UR                  S   5      SS2[         R
                  4   U4   nUR                  UR                  5      nU$ [         R                  " U5      n	[        X5       V
Vs/ s H  u  p[        XU	5      PM     nn
n[         R                  " U5      nU$ s  snn
f )a\  Compute Discounted Cumulative Gain.

Sum the true scores ranked in the order induced by the predicted scores,
after applying a logarithmic discount.

This ranking metric yields a high value if true labels are ranked high by
``y_score``.

Parameters
----------
y_true : ndarray of shape (n_samples, n_labels)
    True targets of multilabel classification, or true scores of entities
    to be ranked.

y_score : ndarray of shape (n_samples, n_labels)
    Target scores, can either be probability estimates, confidence values,
    or non-thresholded measure of decisions (as returned by
    "decision_function" on some classifiers).

k : int, default=None
    Only consider the highest k scores in the ranking. If `None`, use all
    outputs.

log_base : float, default=2
    Base of the logarithm used for the discount. A low value means a
    sharper discount (top results are more important).

ignore_ties : bool, default=False
    Assume that there are no ties in y_score (which is likely to be the
    case if y_score is continuous) for efficiency gains.

Returns
-------
discounted_cumulative_gain : ndarray of shape (n_samples,)
    The DCG score for each sample.

See Also
--------
ndcg_score : The Discounted Cumulative Gain divided by the Ideal Discounted
    Cumulative Gain (the DCG obtained for a perfect ranking), in order to
    have a score between 0 and 1.
r   r
   Nr   r"   )r%   logaranger#   r   newaxisr   Tr   r   _tie_averaged_dcgasarray)r:   r;   klog_baseignore_tiesdiscountrankingrankedcumulative_gainsdiscount_cumsumy_ty_ss               r2   _dcg_sample_scoresr     s   V BFF299V\\!_59:RVVH=MMNH}**W%a2g.		'--"23ArzzMBGKL#<<1  ))H-  0
0 c80 	 
 ::&67
s   D4c                    [         R                  " U* SSS9u  p4n[         R                  " [        U5      5      n[         R                  R                  XdU 5        Xe-  n[         R                  " U5      S-
  n[         R                  " [        U5      5      nX'S      US'   [         R                  " X'   5      USS& Xh-  R                  5       $ )a  
Compute DCG by averaging over possible permutations of ties.

The gain (`y_true`) of an index falling inside a tied group (in the order
induced by `y_score`) is replaced by the average gain within this group.
The discounted gain for a tied group is then the average `y_true` within
this group times the sum of discounts of the corresponding ranks.

This amounts to averaging scores for all possible orderings of the tied
groups.

(note in the case of dcg@k the discount is 0 after index k)

Parameters
----------
y_true : ndarray
    The true relevance scores.

y_score : ndarray
    Predicted scores.

discount_cumsum : ndarray
    Precomputed cumulative sum of the discounts.

Returns
-------
discounted_cumulative_gain : float
    The discounted cumulative gain.

References
----------
McSherry, F., & Najork, M. (2008, March). Computing information retrieval
performance measures efficiently in the presence of tied scores. In
European conference on information retrieval (pp. 414-421). Springer,
Berlin, Heidelberg.
T)r   return_countsr   r   N)
r%   rT   r   rV   addatr   emptyr&   rE   )	r:   r;   r   rI   invcountsr   groupsdiscount_sumss	            r2   r   r     s    J YYxDQNAFXXc&k"FFFIIf6"
FYYv"FHHS[)M&ay1M! 78M!""''))r4   c                 Z    [        U SS9nSnX;  a  [        SR                  X!5      5      eg )Nr:   rL   )rO   zcontinuous-multioutputzmulticlass-multioutputz-Only {} formats are supported. Got {} instead)r   r$   r)   )r:   rW   supported_fmts      r2   _check_dcg_target_typer  @  s@    Fx8FM
 ";BB
 	
 #r4   leftneitherr:   r;   r   r   r>   r   )r   r   r>   r   c                    [        U SS9n [        USS9n[        XU5        [        U 5        [        [        R
                  " [        XX#US9US95      $ )a  Compute Discounted Cumulative Gain.

Sum the true scores ranked in the order induced by the predicted scores,
after applying a logarithmic discount.

This ranking metric yields a high value if true labels are ranked high by
``y_score``.

Usually the Normalized Discounted Cumulative Gain (NDCG, computed by
ndcg_score) is preferred.

Parameters
----------
y_true : array-like of shape (n_samples, n_labels)
    True targets of multilabel classification, or true scores of entities
    to be ranked.

y_score : array-like of shape (n_samples, n_labels)
    Target scores, can either be probability estimates, confidence values,
    or non-thresholded measure of decisions (as returned by
    "decision_function" on some classifiers).

k : int, default=None
    Only consider the highest k scores in the ranking. If None, use all
    outputs.

log_base : float, default=2
    Base of the logarithm used for the discount. A low value means a
    sharper discount (top results are more important).

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights. If `None`, all samples are given the same weight.

ignore_ties : bool, default=False
    Assume that there are no ties in y_score (which is likely to be the
    case if y_score is continuous) for efficiency gains.

Returns
-------
discounted_cumulative_gain : float
    The averaged sample DCG scores.

See Also
--------
ndcg_score : The Discounted Cumulative Gain divided by the Ideal Discounted
    Cumulative Gain (the DCG obtained for a perfect ranking), in order to
    have a score between 0 and 1.

References
----------
`Wikipedia entry for Discounted Cumulative Gain
<https://en.wikipedia.org/wiki/Discounted_cumulative_gain>`_.

Jarvelin, K., & Kekalainen, J. (2002).
Cumulated gain-based evaluation of IR techniques. ACM Transactions on
Information Systems (TOIS), 20(4), 422-446.

Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May).
A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th
Annual Conference on Learning Theory (COLT 2013).

McSherry, F., & Najork, M. (2008, March). Computing information retrieval
performance measures efficiently in the presence of tied scores. In
European conference on information retrieval (pp. 414-421). Springer,
Berlin, Heidelberg.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import dcg_score
>>> # we have ground-truth relevance of some answers to a query:
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])
>>> # we predict scores for the answers
>>> scores = np.asarray([[.1, .2, .3, 4, 70]])
>>> dcg_score(true_relevance, scores)
9.49
>>> # we can set k to truncate the sum; only top k answers contribute
>>> dcg_score(true_relevance, scores, k=2)
5.63
>>> # now we have some ties in our prediction
>>> scores = np.asarray([[1, 0, 0, 0, 1]])
>>> # by default ties are averaged, so here we get the average true
>>> # relevance of our top predictions: (10 + 5) / 2 = 7.5
>>> dcg_score(true_relevance, scores, k=1)
7.5
>>> # we can choose to ignore ties for faster results, but only
>>> # if we know there aren't ties in our scores, otherwise we get
>>> # wrong results:
>>> dcg_score(true_relevance,
...           scores, k=1, ignore_ties=True)
5.0
Fr   )r   r   r   r   )r   r   r  r.   r%   r<   r   r  s         r2   	dcg_scorer	  O  s\    T 51F'U3GF];6"


1[ "		
 r4   c                 b    [        XX#S9n[        X USS9nUS:H  nSXF'   XF) ==   XV)    -  ss'   U$ )a  Compute Normalized Discounted Cumulative Gain.

Sum the true scores ranked in the order induced by the predicted scores,
after applying a logarithmic discount. Then divide by the best possible
score (Ideal DCG, obtained for a perfect ranking) to obtain a score between
0 and 1.

This ranking metric yields a high value if true labels are ranked high by
``y_score``.

Parameters
----------
y_true : ndarray of shape (n_samples, n_labels)
    True targets of multilabel classification, or true scores of entities
    to be ranked.

y_score : ndarray of shape (n_samples, n_labels)
    Target scores, can either be probability estimates, confidence values,
    or non-thresholded measure of decisions (as returned by
    "decision_function" on some classifiers).

k : int, default=None
    Only consider the highest k scores in the ranking. If None, use all
    outputs.

ignore_ties : bool, default=False
    Assume that there are no ties in y_score (which is likely to be the
    case if y_score is continuous) for efficiency gains.

Returns
-------
normalized_discounted_cumulative_gain : ndarray of shape (n_samples,)
    The NDCG score for each sample (float in [0., 1.]).

See Also
--------
dcg_score : Discounted Cumulative Gain (not normalized).

)r   Tr   )r   )r:   r;   r   r   gainnormalizing_gainall_irrelevants          r2   _ndcg_sample_scoresr    sO    P fqJD *&!N%*ND-o>>Kr4   )r:   r;   r   r>   r   )r   r>   r   c                b   [        U SS9n [        USS9n[        XU5        U R                  5       S:  a  [        S5      eU R                  S:  a/  U R
                  S   S::  a  [        SU R
                  S    S35      e[        U 5        [        XX$S9n[        [        R                  " XSS	95      $ )
aS  Compute Normalized Discounted Cumulative Gain.

Sum the true scores ranked in the order induced by the predicted scores,
after applying a logarithmic discount. Then divide by the best possible
score (Ideal DCG, obtained for a perfect ranking) to obtain a score between
0 and 1.

This ranking metric returns a high value if true labels are ranked high by
``y_score``.

Parameters
----------
y_true : array-like of shape (n_samples, n_labels)
    True targets of multilabel classification, or true scores of entities
    to be ranked. Negative values in `y_true` may result in an output
    that is not between 0 and 1.

y_score : array-like of shape (n_samples, n_labels)
    Target scores, can either be probability estimates, confidence values,
    or non-thresholded measure of decisions (as returned by
    "decision_function" on some classifiers).

k : int, default=None
    Only consider the highest k scores in the ranking. If `None`, use all
    outputs.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights. If `None`, all samples are given the same weight.

ignore_ties : bool, default=False
    Assume that there are no ties in y_score (which is likely to be the
    case if y_score is continuous) for efficiency gains.

Returns
-------
normalized_discounted_cumulative_gain : float in [0., 1.]
    The averaged NDCG scores for all samples.

See Also
--------
dcg_score : Discounted Cumulative Gain (not normalized).

References
----------
`Wikipedia entry for Discounted Cumulative Gain
<https://en.wikipedia.org/wiki/Discounted_cumulative_gain>`_

Jarvelin, K., & Kekalainen, J. (2002).
Cumulated gain-based evaluation of IR techniques. ACM Transactions on
Information Systems (TOIS), 20(4), 422-446.

Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May).
A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th
Annual Conference on Learning Theory (COLT 2013)

McSherry, F., & Najork, M. (2008, March). Computing information retrieval
performance measures efficiently in the presence of tied scores. In
European conference on information retrieval (pp. 414-421). Springer,
Berlin, Heidelberg.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import ndcg_score
>>> # we have ground-truth relevance of some answers to a query:
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])
>>> # we predict some scores (relevance) for the answers
>>> scores = np.asarray([[.1, .2, .3, 4, 70]])
>>> ndcg_score(true_relevance, scores)
0.69
>>> scores = np.asarray([[.05, 1.1, 1., .5, .0]])
>>> ndcg_score(true_relevance, scores)
0.49
>>> # we can set k to truncate the sum; only top k answers contribute.
>>> ndcg_score(true_relevance, scores, k=4)
0.35
>>> # the normalization takes k into account so a perfect answer
>>> # would still get 1.0
>>> ndcg_score(true_relevance, true_relevance, k=4)
1.0...
>>> # now we have some ties in our prediction
>>> scores = np.asarray([[1, 0, 0, 0, 1]])
>>> # by default ties are averaged, so here we get the average (normalized)
>>> # true relevance of our top predictions: (10 / 10 + 5 / 10) / 2 = .75
>>> ndcg_score(true_relevance, scores, k=1)
0.75
>>> # we can choose to ignore ties for faster results, but only
>>> # if we know there aren't ties in our scores, otherwise we get
>>> # wrong results:
>>> ndcg_score(true_relevance,
...           scores, k=1, ignore_ties=True)
0.5...
Fr   r   z8ndcg_score should not be used on negative y_true values.r   zJComputing NDCG is only meaningful when there is more than 1 document. Got z	 instead.)r   r   r   )r   r   r   r$   r   r#   r  r  r.   r%   r<   )r:   r;   r   r>   r   r  s         r2   
ndcg_scorer    s    P 51F'U3GF];zz|aSTT{{Q6<<?a/<<?#9.
 	
 6"v!MDD899r4   )r:   r;   r   	normalizer>   r   )r   r  r>   r   c                &   [        U SSS9n [        U 5      n [        U SS9nUS:X  a  Ub  [        U5      S:  a  SnUS	;  a  [	        S
U S35      e[        USS9nUS:X  aJ  UR
                  S:X  a/  UR                  S   S:w  a  [	        SUR                  S    S35      e[        U5      n[        XU5        UR
                  S:X  a  UR                  S   OSnUc.  [        U 5      n[        U5      n	X:w  a  [	        SU	 SU S35      eO[        U5      n[        U5      n[        U5      n
[        U5      n	X:w  a  [	        S5      e[        R                  " X5      (       d  [	        S5      eX:w  a  [	        SU	 SU S35      e[        [        R                  " X5      5      (       a  [	        S5      eX):  a"  [        R                  " SU SU	 S3[        5        [        XS9nUS:X  a|  US:X  aR  UR!                  5       S:  a  UR#                  5       S::  a  SOSnX:  R%                  [        R&                  5      nX:H  nOn[        R(                  " U[        R*                  S9nOJUS:X  aD  [        R,                  " USSS 9SS2SSS!24   nXSS2SU24   R.                  :H  R1                  SS"9nU(       a  [3        [        R4                  " WUS#95      $ Uc  [3        [        R6                  " W5      5      $ [3        [        R8                  " WU5      5      $ )$aC  Top-k Accuracy classification score.

This metric computes the number of times where the correct label is among
the top `k` labels predicted (ranked by predicted scores). Note that the
multilabel case isn't covered here.

Read more in the :ref:`User Guide <top_k_accuracy_score>`

Parameters
----------
y_true : array-like of shape (n_samples,)
    True labels.

y_score : array-like of shape (n_samples,) or (n_samples, n_classes)
    Target scores. These can be either probability estimates or
    non-thresholded decision values (as returned by
    :term:`decision_function` on some classifiers).
    The binary case expects scores with shape (n_samples,) while the
    multiclass case expects scores with shape (n_samples, n_classes).
    In the multiclass case, the order of the class scores must
    correspond to the order of ``labels``, if provided, or else to
    the numerical or lexicographical order of the labels in ``y_true``.
    If ``y_true`` does not contain all the labels, ``labels`` must be
    provided.

k : int, default=2
    Number of most likely outcomes considered to find the correct label.

normalize : bool, default=True
    If `True`, return the fraction of correctly classified samples.
    Otherwise, return the number of correctly classified samples.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights. If `None`, all samples are given the same weight.

labels : array-like of shape (n_classes,), default=None
    Multiclass only. List of labels that index the classes in ``y_score``.
    If ``None``, the numerical or lexicographical order of the labels in
    ``y_true`` is used. If ``y_true`` does not contain all the labels,
    ``labels`` must be provided.

Returns
-------
score : float
    The top-k accuracy score. The best performance is 1 with
    `normalize == True` and the number of samples with
    `normalize == False`.

See Also
--------
accuracy_score : Compute the accuracy score. By default, the function will
    return the fraction of correct predictions divided by the total number
    of predictions.

Notes
-----
In cases where two or more labels are assigned equal predicted scores,
the labels with the highest indices will be chosen first. This might
impact the result if the correct label falls after the threshold because
of that.

Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import top_k_accuracy_score
>>> y_true = np.array([0, 1, 2, 2])
>>> y_score = np.array([[0.5, 0.2, 0.2],  # 0 is in top 2
...                     [0.3, 0.4, 0.2],  # 1 is in top 2
...                     [0.2, 0.4, 0.3],  # 2 is in top 2
...                     [0.7, 0.2, 0.1]]) # 2 isn't in top 2
>>> top_k_accuracy_score(y_true, y_score, k=2)
0.75
>>> # Not normalizing gives the number of "correctly" classified samples
>>> top_k_accuracy_score(y_true, y_score, k=2, normalize=False)
3.0
FNr   r:   rL   rN   r
   rP   >   rN   rP   z.y type must be 'binary' or 'multiclass', got 'z
' instead.r   r   z,`y_true` is binary while y_score is 2d with zQ classes. If `y_true` does not contain all the labels, `labels` must be provided.zNumber of classes in 'y_true' (z3) not equal to the number of classes in 'y_score' (zX).You can provide a list of all known classes by assigning it to the `labels` parameter.z"Parameter 'labels' must be unique.z#Parameter 'labels' must be ordered.zNumber of given labels (z).z3'y_true' contains labels not in parameter 'labels'.z'k' (z() greater than or equal to 'n_classes' (z>) will result in a perfect score and is therefore meaningless.r   r   rr   )r,   r   )r   r   r"   r   r   )r   r   r   rV   r$   r   r#   r   r   r%   r   r   rs   rt   r   r   r   rD   astypeint64r   bool_r   r   r'   r.   r<   rE   r   )r:   r;   r   r  r>   r   rW   y_score_n_classesrR   	n_classesr   r   	thresholdy_predhitssorted_preds                   r2   top_k_accuracy_scorer  r  s   t 5=F&!FFx8Ff0S[1_--<VHJO
 	
 'U3G<<1q!1Q!6MM!$% &66 
 w'F];,3LLA,=a(1~&/L	)1) =::K9L M--  * f%&/v;L	 ABB~~g..BCC)*9+ 633D2ERI 
 r||F,--RSS~sB9+ NO O #	
 V5N6&{{}1gkkmq6HaI)11"((;F+D<<rxx8D	<	jjq{CAttGLa!e"4"6"66;;;CRZZm<==		RVVD\""RVVD-011r4   )NNF)NN)Nr
   F)NF)@__doc__rs   	functoolsr   numbersr   r   numpyr%   scipy.integrater   scipy.sparser   r   scipy.statsr	   
exceptionsr   preprocessingr   utilsr   r   r   r   utils._encoder   r   utils._param_validationr   r   r   utils.extmathr   utils.multiclassr   utils.sparsefuncsr   utils.validationr   r   _baser   r   r3   strrZ   rp   r   r   r   r_   rC   rv   r   r   r   r   r   r  r	  r  r  r   r4   r2   <module>r0     sU     "  % -   / *  - K K ) - - Q G .~."&>	>B . >HI4PC+&- #'	 !(1DV	Vr . >CD1&-'[ #'	 LQIT	ITXHB . >HI4P&-T3':DA"#:;<& #'  j
j
Zx
vR-j . >CD1&-'[ #'	 GU	GUT . >CD1&-'[ #'	 #'ddL 	L ^ 1 >&-
 #' MQ ZZz . >&-
 #' 6: B>B>J 1 >&-
 #' :> [:[:|9x-*`
 . >xD8$?dCi@A&-!{ #'
 $Ej
jZ0f . >xD8$?&-!{ #'	 &*5 k:	k:\ . >xD89[&-& #'
 t4`2
`2r4   