§
    ¦˜ºh4)  ã                   óX   — d Z ddlZddlmZ ddlmZ ddlmZm	Z	 d„ Z
d	„ Z	 dd
„Zdd„ZdS )zwUtilities to get the response values of a classifier or a regressor.

It allows to make uniform checks and validation.
é    Né   ©Úis_classifieré   )Útype_of_target)Ú_check_response_methodÚcheck_is_fittedc                 óD  — |dk    r)| j         d         dk     rt          d| j         › d�¦  «        ‚|dk    r*t          j        ||k    ¦  «        d         }| dd…|f         S |dk    r:t	          | t
          ¦  «        r#t          j        d	„ | D ¦   «         ¦  «        j        S | S | S )
aÇ  Get the response values when the response method is `predict_proba`.

    This function process the `y_pred` array in the binary and multi-label cases.
    In the binary case, it selects the column corresponding to the positive
    class. In the multi-label case, it stacks the predictions if they are not
    in the "compressed" format `(n_samples, n_outputs)`.

    Parameters
    ----------
    y_pred : ndarray
        Output of `estimator.predict_proba`. The shape depends on the target type:

        - for binary classification, it is a 2d array of shape `(n_samples, 2)`;
        - for multiclass classification, it is a 2d array of shape
          `(n_samples, n_classes)`;
        - for multilabel classification, it is either a list of 2d arrays of shape
          `(n_samples, 2)` (e.g. `RandomForestClassifier` or `KNeighborsClassifier`) or
          an array of shape `(n_samples, n_outputs)` (e.g. `MLPClassifier` or
          `RidgeClassifier`).

    target_type : {"binary", "multiclass", "multilabel-indicator"}
        Type of the target.

    classes : ndarray of shape (n_classes,) or list of such arrays
        Class labels as reported by `estimator.classes_`.

    pos_label : int, float, bool or str
        Only used with binary and multiclass targets.

    Returns
    -------
    y_pred : ndarray of shape (n_samples,), (n_samples, n_classes) or             (n_samples, n_output)
        Compressed predictions format as requested by the metrics.
    Úbinaryr   r   zGot predict_proba of shape z', but need classifier with two classes.r   Nzmultilabel-indicatorc                 ó&   — g | ]}|d d …df         ‘ŒS )Néÿÿÿÿ© )Ú.0Úps     ú_/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/sklearn/utils/_response.pyú
<listcomp>z*_process_predict_proba.<locals>.<listcomp>@   s$   € Ð7Ð7Ð7¨1˜a    2 œhÐ7Ð7Ð7ó    )ÚshapeÚ
ValueErrorÚnpÚflatnonzeroÚ
isinstanceÚlistÚvstackÚT)Úy_predÚtarget_typeÚclassesÚ	pos_labelÚcol_idxs        r   Ú_process_predict_probar!      sÑ   € ðH �hÒÐ 6¤<°¤?°QÒ#6Ð#6åð+¨&¬,ð +ð +ð +ñ
ô 
ð 	
ð
 �hÒÐÝ”. ¨IÒ!5Ñ6Ô6°qÔ9ˆØ�a�a�a˜�jÔ!Ð!Ø	Ð.Ò	.Ð	.õ �f�dÑ#Ô#ð 	å”9Ð7Ð7°Ð7Ñ7Ô7Ñ8Ô8Ô:Ð:ð ˆMà€Mr   c                 ó4   — |dk    r||d         k    rd| z  S | S )aw  Get the response values when the response method is `decision_function`.

    This function process the `y_pred` array in the binary and multi-label cases.
    In the binary case, it inverts the sign of the score if the positive label
    is not `classes[1]`. In the multi-label case, it stacks the predictions if
    they are not in the "compressed" format `(n_samples, n_outputs)`.

    Parameters
    ----------
    y_pred : ndarray
        Output of `estimator.predict_proba`. The shape depends on the target type:

        - for binary classification, it is a 1d array of shape `(n_samples,)` where the
          sign is assuming that `classes[1]` is the positive class;
        - for multiclass classification, it is a 2d array of shape
          `(n_samples, n_classes)`;
        - for multilabel classification, it is a 2d array of shape `(n_samples,
          n_outputs)`.

    target_type : {"binary", "multiclass", "multilabel-indicator"}
        Type of the target.

    classes : ndarray of shape (n_classes,) or list of such arrays
        Class labels as reported by `estimator.classes_`.

    pos_label : int, float, bool or str
        Only used with binary and multiclass targets.

    Returns
    -------
    y_pred : ndarray of shape (n_samples,), (n_samples, n_classes) or             (n_samples, n_output)
        Compressed predictions format as requested by the metrics.
    r   r   r   r   ©r   r   r   r   s       r   Ú_process_decision_functionr$   H   s-   € ðF �hÒÐ 9°¸´
Ò#:Ð#:Ø�F‰{ÐØ€Mr   c                 ó
  — ddl m}  || ¦  «        r°t          | |¦  «        }| j        }t	          |¦  «        }|dv r=|�+||                     ¦   «         vrt          d|› d|› �¦  «        ‚|€|dk    r|d         } ||¦  «        }|j        d	k    rt          ||||¬
¦  «        }n^|j        dk    rt          ||||¬
¦  «        }n?|dk    r"t          | j
        j        › d|› d|› d�¦  «        ‚|                      |¦  «        d}}||fS )a�	  Compute the response values of a classifier or a regressor.

    The response values are predictions such that it follows the following shape:

    - for binary classification, it is a 1d array of shape `(n_samples,)`;
    - for multiclass classification, it is a 2d array of shape `(n_samples, n_classes)`;
    - for multilabel classification, it is a 2d array of shape `(n_samples, n_outputs)`;
    - for regression, it is a 1d array of shape `(n_samples,)`.

    If `estimator` is a binary classifier, also return the label for the
    effective positive class.

    This utility is used primarily in the displays and the scikit-learn scorers.

    .. versionadded:: 1.3

    Parameters
    ----------
    estimator : estimator instance
        Fitted classifier or regressor or a fitted :class:`~sklearn.pipeline.Pipeline`
        in which the last estimator is a classifier or a regressor.

    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Input values.

    response_method : {"predict_proba", "decision_function", "predict"} or             list of such str
        Specifies the response method to use get prediction from an estimator
        (i.e. :term:`predict_proba`, :term:`decision_function` or
        :term:`predict`). Possible choices are:

        - if `str`, it corresponds to the name to the method to return;
        - if a list of `str`, it provides the method names in order of
          preference. The method returned corresponds to the first method in
          the list and which is implemented by `estimator`.

    pos_label : int, float, bool or str, default=None
        The class considered as the positive class when computing
        the metrics. By default, `estimators.classes_[1]` is
        considered as the positive class.

    Returns
    -------
    y_pred : ndarray of shape (n_samples,), (n_samples, n_classes) or             (n_samples, n_outputs)
        Target scores calculated from the provided `response_method`
        and `pos_label`.

    pos_label : int, float, bool, str or None
        The class considered as the positive class when computing
        the metrics. Returns `None` if `estimator` is a regressor.

    Raises
    ------
    ValueError
        If `pos_label` is not a valid label.
        If the shape of `y_pred` is not consistent for binary classifier.
        If the response method can be applied to a classifier only and
        `estimator` is a regressor.
    r   r   )r   Ú
multiclassNz
pos_label=z+ is not a valid label: It should be one of r   r   Úpredict_probar#   Údecision_functionÚpredictz? should either be a classifier to be used with response_method=zR or the response_method should be 'predict'. Got a regressor with response_method=ú	 instead.)Úsklearn.baser   r   Úclasses_r   Útolistr   Ú__name__r!   r$   Ú	__class__r)   )	Ú	estimatorÚXÚresponse_methodr   r   Úprediction_methodr   r   r   s	            r   Ú_get_response_valuesr4   p   s¨  € ðD +Ð*Ð*Ð*Ð*Ð*à€}�YÑÔð &7Ý2°9¸oÑNÔNÐØÔ$ˆÝ$ WÑ-Ô-ˆàÐ2Ð2Ð2ØÐ$¨¸'¿.º.Ñ:JÔ:JÐ)JÐ)JÝ ð( ð (ð (Ø%ð(ð (ñô ð ð Ð" {°hÒ'>Ð'>Ø# BœK�	à"Ð" 1Ñ%Ô%ˆàÔ%¨Ò8Ð8Ý+ØØ'ØØ#ð	ñ ô ˆFˆFð Ô'Ð+>Ò>Ð>Ý/ØØ'ØØ#ð	ñ ô ˆFøð ˜iÒ'Ð'ÝØÔ&Ô/ð .ð .Ø-<ð.ð .ð #ð.ð .ð .ñô ð ð &×-Ò-¨aÑ0Ô0°$�	ˆà�9ÐÐr   c                 ó<  — d}t          | ¦  «         t          | ¦  «        s t          |d| j        j        › d�z   ¦  «        ‚t          | j        ¦  «        dk    r(t          |dt          | j        ¦  «        › d�z   ¦  «        ‚|dk    rddg}t          | |||¬	¦  «        S )
a�  Compute the response values of a binary classifier.

    Parameters
    ----------
    estimator : estimator instance
        Fitted classifier or a fitted :class:`~sklearn.pipeline.Pipeline`
        in which the last estimator is a binary classifier.

    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Input values.

    response_method : {'auto', 'predict_proba', 'decision_function'}
        Specifies whether to use :term:`predict_proba` or
        :term:`decision_function` as the target response. If set to 'auto',
        :term:`predict_proba` is tried first and if it does not exist
        :term:`decision_function` is tried next.

    pos_label : int, float, bool or str, default=None
        The class considered as the positive class when computing
        the metrics. By default, `estimators.classes_[1]` is
        considered as the positive class.

    Returns
    -------
    y_pred : ndarray of shape (n_samples,)
        Target scores calculated from the provided response_method
        and pos_label.

    pos_label : int, float, bool or str
        The class considered as the positive class when computing
        the metrics.
    z/Expected 'estimator' to be a binary classifier.z Got r*   r   z classes instead.Úautor'   r(   )r   )r	   r   r   r/   r.   Úlenr,   r4   )r0   r1   r2   r   Úclassification_errors        r   Ú_get_response_values_binaryr9   ß   sÚ   € ðB MÐå�IÑÔÐÝ˜Ñ#Ô#ð 
ÝØ Ð#R¨9Ô+>Ô+GÐ#RÐ#RÐ#RÑRñ
ô 
ð 	
õ 
ˆYÔÑ	 Ô	  AÒ	%Ð	%ÝØ Ð#U­3¨yÔ/AÑ+BÔ+BÐ#UÐ#UÐ#UÑUñ
ô 
ð 	
ð ˜&Ò Ð Ø*Ð,?Ð@ˆåØØ	ØØð	ñ ô ð r   )N)Ú__doc__Únumpyr   Úbaser   r&   r   Ú
validationr   r	   r!   r$   r4   r9   r   r   r   ú<module>r>      sµ   ððð ð Ð Ð Ð à  Ð  Ð  Ð  Ð  Ð  Ø &Ð &Ð &Ð &Ð &Ð &Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?ð9ð 9ð 9ðx%ð %ð %ðX ð	lð lð lð lð^5ð 5ð 5ð 5ð 5ð 5r   