§
    rŠtjÓC  ã                   óp   — d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ  G d„ d	¦  «        ZdS )
é    )ÚproductN)Úis_classifier)Úconfusion_matrix)Úcheck_matplotlib_support)Ú_validate_style_kwargs)Úunique_labelsc                   óœ   — e Zd ZdZddœd„Zdddddddddœd	„Zeddddddddddddd
œd„¦   «         Zeddddddddddddd
œd„¦   «         ZdS )ÚConfusionMatrixDisplaya¾	  Confusion Matrix visualization.

    It is recommended to use
    :func:`~sklearn.metrics.ConfusionMatrixDisplay.from_estimator` or
    :func:`~sklearn.metrics.ConfusionMatrixDisplay.from_predictions` to
    create a :class:`ConfusionMatrixDisplay`. All parameters are stored as
    attributes.

    For general information regarding `scikit-learn` visualization tools, see
    the :ref:`Visualization Guide <visualizations>`.
    For guidance on interpreting these plots, refer to the
    :ref:`Model Evaluation Guide <confusion_matrix>`.

    Parameters
    ----------
    confusion_matrix : ndarray of shape (n_classes, n_classes)
        Confusion matrix.

    display_labels : ndarray of shape (n_classes,), default=None
        Display labels for plot. If None, display labels are set from 0 to
        `n_classes - 1`.

    Attributes
    ----------
    im_ : matplotlib AxesImage
        Image representing the confusion matrix.

    text_ : ndarray of shape (n_classes, n_classes), dtype=matplotlib Text,             or None
        Array of matplotlib axes. `None` if `include_values` is false.

    ax_ : matplotlib Axes
        Axes with confusion matrix.

    figure_ : matplotlib Figure
        Figure containing the confusion matrix.

    See Also
    --------
    confusion_matrix : Compute Confusion Matrix to evaluate the accuracy of a
        classification.
    ConfusionMatrixDisplay.from_estimator : Plot the confusion matrix
        given an estimator, the data, and the label.
    ConfusionMatrixDisplay.from_predictions : Plot the confusion matrix
        given the true and predicted labels.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
    >>> from sklearn.model_selection import train_test_split
    >>> from sklearn.svm import SVC
    >>> X, y = make_classification(random_state=0)
    >>> X_train, X_test, y_train, y_test = train_test_split(X, y,
    ...                                                     random_state=0)
    >>> clf = SVC(random_state=0)
    >>> clf.fit(X_train, y_train)
    SVC(random_state=0)
    >>> predictions = clf.predict(X_test)
    >>> cm = confusion_matrix(y_test, predictions, labels=clf.classes_)
    >>> disp = ConfusionMatrixDisplay(confusion_matrix=cm,
    ...                               display_labels=clf.classes_)
    >>> disp.plot()
    <...>
    >>> plt.show()
    N)Údisplay_labelsc                ó"   — || _         || _        d S )N©r   r   )Úselfr   r   s      úd/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/metrics/_plot/confusion_matrix.pyÚ__init__zConfusionMatrixDisplay.__init__T   s   € Ø 0ˆÔØ,ˆÔÐÐó    TÚviridisÚ
horizontal)Úinclude_valuesÚcmapÚxticks_rotationÚvalues_formatÚaxÚcolorbarÚim_kwÚtext_kwc                ó¾  — t          d¦  «         ddlm}	 |€|	                     ¦   «         \  }
}n|j        }
| j        }|j        d         }t          d|¬¦  «        }|pi }t          ||¦  «        }|pi } |j	        |fi |¤Ž| _
        d| _        | j
                             d¦  «        | j
                             d¦  «        }}|�rGt          j        |t          ¬¦  «        | _        |                     ¦   «         |                     ¦   «         z   dz  }t%          t'          |¦  «        t'          |¦  «        ¦  «        D ]Ð\  }}|||f         |k     r|n|}|€ct)          |||f         d	¦  «        }|j        j        d
k    r:t)          |||f         d¦  «        }t/          |¦  «        t/          |¦  «        k     r|}nt)          |||f         |¦  «        }t          dd|¬¦  «        }t          ||¦  «        } |j        |||fi |¤Ž| j        ||f<   ŒÑ| j        €t          j        |¦  «        }n| j        }|r|
                     | j
        |¬¦  «         |                     t          j        |¦  «        t          j        |¦  «        ||dd¬¦  «         |                     |dz
  df¦  «         |	                     |                     ¦   «         |¬¦  «         |
| _         || _!        | S )aL  Plot visualization.

        Parameters
        ----------
        include_values : bool, default=True
            Includes values in confusion matrix.

        cmap : str or matplotlib Colormap, default='viridis'
            Colormap recognized by matplotlib.

        xticks_rotation : {'vertical', 'horizontal'} or float,                          default='horizontal'
            Rotation of xtick labels.

        values_format : str, default=None
            Format specification for values in confusion matrix. If `None`,
            the format specification is 'd' or '.2g' whichever is shorter.

        ax : matplotlib axes, default=None
            Axes object to plot on. If `None`, a new figure and axes is
            created.

        colorbar : bool, default=True
            Whether or not to add a colorbar to the plot.

        im_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.imshow` call.

        text_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.text` call.

            .. versionadded:: 1.2

        Returns
        -------
        display : :class:`~sklearn.metrics.ConfusionMatrixDisplay`
            Returns a :class:`~sklearn.metrics.ConfusionMatrixDisplay` instance
            that contains all the information to plot the confusion matrix.
        zConfusionMatrixDisplay.plotr   NÚnearest)Úinterpolationr   g      ð?)Údtypeg       @z.2gÚfÚdÚcenter)ÚhaÚvaÚcolor)r   z
True labelzPredicted label)ÚxticksÚyticksÚxticklabelsÚyticklabelsÚylabelÚxlabelg      à?g      à¿)Úrotation)"r   Úmatplotlib.pyplotÚpyplotÚsubplotsÚfigurer   ÚshapeÚdictr   ÚimshowÚim_Útext_r   ÚnpÚ
empty_likeÚobjectÚmaxÚminr   ÚrangeÚformatr   ÚkindÚlenÚtextr   Úaranger   ÚsetÚset_ylimÚsetpÚget_xticklabelsÚfigure_Úax_)r   r   r   r   r   r   r   r   r   ÚpltÚfigÚcmÚ	n_classesÚdefault_im_kwÚcmap_minÚcmap_maxÚthreshÚiÚjr%   Útext_cmÚtext_dÚdefault_text_kwargsÚtext_kwargsr   s                            r   ÚplotzConfusionMatrixDisplay.plotX   sø  € õf 	!Ð!>Ñ?Ô?Ð?Ø'Ð'Ð'Ð'Ð'Ð'àˆ:Ø—l’l‘n”n‰GˆC��à”)ˆCàÔ"ˆØ”H˜Q”Kˆ	å¨9¸4Ð@Ñ@Ô@ˆØ�˜ˆÝ& }°eÑ<Ô<ˆØ�-˜Rˆà�2”9˜RÐ)Ð) 5Ð)Ð)ˆŒØˆŒ
Ø!œXŸ]š]¨1Ñ-Ô-¨t¬x¯}ª}¸SÑ/AÔ/A�(ˆàñ 	IÝœ rµÐ8Ñ8Ô8ˆDŒJð —f’f‘h”h §¢¡¤Ñ)¨SÑ0ˆFå¥ iÑ 0Ô 0µ%¸	Ñ2BÔ2BÑCÔCð Ið I‘��1Ø$& q¨! t¤H¨vÒ$5Ð$5˜˜¸8�à Ð(Ý$ R¨¨1¨¤X¨uÑ5Ô5�GØ”x”}¨Ò+Ð+Ý!'¨¨1¨a¨4¬°#Ñ!6Ô!6˜Ý˜v™;œ;­¨W©¬Ò5Ð5Ø&,˜Gøå$ R¨¨1¨¤X¨}Ñ=Ô=�Gå&*¨h¸8È5Ð&QÑ&QÔ&QÐ#Ý4Ð5HÈ'ÑRÔR�à#* 2¤7¨1¨a°Ð#HÐ#H¸KÐ#HÐ#H�”
˜1˜a˜4Ñ Ð àÔÐ&ÝœY yÑ1Ô1ˆNˆNà!Ô0ˆNØð 	*Ø�LŠL˜œ bˆLÑ)Ô)Ð)Ø
�ŠÝ”9˜YÑ'Ô'Ý”9˜YÑ'Ô'Ø&Ø&ØØ$ð 	ñ 	
ô 	
ð 	
ð 	�Š�Y ‘_ dÐ+Ñ,Ô,Ð,Ø�Š�×#Ò#Ñ%Ô%°ˆÑ@Ô@Ð@àˆŒØˆŒØˆr   )ÚlabelsÚsample_weightÚ	normalizer   r   r   r   r   r   r   r   r   c                óæ   — | j         › d�}t          |¦  «         t          |¦  «        st          |› d�¦  «        ‚|                     |¦  «        }|                      ||||||||||	|
|||¬¦  «        S )a9  Plot Confusion Matrix given an estimator and some data.

        For general information regarding `scikit-learn` visualization tools, see
        the :ref:`Visualization Guide <visualizations>`.
        For guidance on interpreting these plots, refer to the
        :ref:`Model Evaluation Guide <confusion_matrix>`.

        .. versionadded:: 1.0

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

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

        y : array-like of shape (n_samples,)
            Target values.

        labels : array-like of shape (n_classes,), default=None
            List of labels to index the confusion matrix. This may be used to
            reorder or select a subset of labels. If `None` is given, those
            that appear at least once in `y_true` or `y_pred` are used in
            sorted order.

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

        normalize : {'true', 'pred', 'all'}, default=None
            Either to normalize the counts display in the matrix:

            - if `'true'`, the confusion matrix is normalized over the true
              conditions (e.g. rows);
            - if `'pred'`, the confusion matrix is normalized over the
              predicted conditions (e.g. columns);
            - if `'all'`, the confusion matrix is normalized by the total
              number of samples;
            - if `None` (default), the confusion matrix will not be normalized.

        display_labels : array-like of shape (n_classes,), default=None
            Target names used for plotting. By default, `labels` will be used
            if it is defined, otherwise the unique labels of `y_true` and
            `y_pred` will be used.

        include_values : bool, default=True
            Includes values in confusion matrix.

        xticks_rotation : {'vertical', 'horizontal'} or float,                 default='horizontal'
            Rotation of xtick labels.

        values_format : str, default=None
            Format specification for values in confusion matrix. If `None`, the
            format specification is 'd' or '.2g' whichever is shorter.

        cmap : str or matplotlib Colormap, default='viridis'
            Colormap recognized by matplotlib.

        ax : matplotlib Axes, default=None
            Axes object to plot on. If `None`, a new figure and axes is
            created.

        colorbar : bool, default=True
            Whether or not to add a colorbar to the plot.

        im_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.imshow` call.

        text_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.text` call.

            .. versionadded:: 1.2

        Returns
        -------
        display : :class:`~sklearn.metrics.ConfusionMatrixDisplay`

        See Also
        --------
        ConfusionMatrixDisplay.from_predictions : Plot the confusion matrix
            given the true and predicted labels.

        Examples
        --------
        >>> import matplotlib.pyplot as plt
        >>> from sklearn.datasets import make_classification
        >>> from sklearn.metrics import ConfusionMatrixDisplay
        >>> from sklearn.model_selection import train_test_split
        >>> from sklearn.svm import SVC
        >>> X, y = make_classification(random_state=0)
        >>> X_train, X_test, y_train, y_test = train_test_split(
        ...         X, y, random_state=0)
        >>> clf = SVC(random_state=0)
        >>> clf.fit(X_train, y_train)
        SVC(random_state=0)
        >>> ConfusionMatrixDisplay.from_estimator(
        ...     clf, X_test, y_test)
        <...>
        >>> plt.show()

        For a detailed example of using a confusion matrix to evaluate a
        Support Vector Classifier, please see
        :ref:`sphx_glr_auto_examples_model_selection_plot_confusion_matrix.py`
        z.from_estimatorz only supports classifiers)rW   rV   rX   r   r   r   r   r   r   r   r   r   )Ú__name__r   r   Ú
ValueErrorÚpredictÚfrom_predictions)ÚclsÚ	estimatorÚXÚyrV   rW   rX   r   r   r   r   r   r   r   r   r   Úmethod_nameÚy_preds                     r   Úfrom_estimatorz%ConfusionMatrixDisplay.from_estimatorÌ   s¥   € ð| œÐ6Ð6Ð6ˆÝ  Ñ-Ô-Ð-Ý˜YÑ'Ô'ð 	IÝ ÐGÐGÐGÑHÔHÐHØ×"Ò" 1Ñ%Ô%ˆà×#Ò#ØØØ'ØØØ)Ø)ØØØ+Ø'ØØØð $ñ 
ô 
ð 	
r   c          
      óÚ   — t          | j        › d�¦  «         |€|€t          ||¦  «        }n|}t          |||||¬¦  «        } | ||¬¦  «        }|                     ||
|||	|||¬¦  «        S )a  Plot Confusion Matrix given true and predicted labels.

        For general information regarding `scikit-learn` visualization tools, see
        the :ref:`Visualization Guide <visualizations>`.
        For guidance on interpreting these plots, refer to the
        :ref:`Model Evaluation Guide <confusion_matrix>`.

        .. versionadded:: 1.0

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

        y_pred : array-like of shape (n_samples,)
            The predicted labels given by the method `predict` of an
            classifier.

        labels : array-like of shape (n_classes,), default=None
            List of labels to index the confusion matrix. This may be used to
            reorder or select a subset of labels. If `None` is given, those
            that appear at least once in `y_true` or `y_pred` are used in
            sorted order.

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

        normalize : {'true', 'pred', 'all'}, default=None
            Either to normalize the counts display in the matrix:

            - if `'true'`, the confusion matrix is normalized over the true
              conditions (e.g. rows);
            - if `'pred'`, the confusion matrix is normalized over the
              predicted conditions (e.g. columns);
            - if `'all'`, the confusion matrix is normalized by the total
              number of samples;
            - if `None` (default), the confusion matrix will not be normalized.

        display_labels : array-like of shape (n_classes,), default=None
            Target names used for plotting. By default, `labels` will be used
            if it is defined, otherwise the unique labels of `y_true` and
            `y_pred` will be used.

        include_values : bool, default=True
            Includes values in confusion matrix.

        xticks_rotation : {'vertical', 'horizontal'} or float,                 default='horizontal'
            Rotation of xtick labels.

        values_format : str, default=None
            Format specification for values in confusion matrix. If `None`, the
            format specification is 'd' or '.2g' whichever is shorter.

        cmap : str or matplotlib Colormap, default='viridis'
            Colormap recognized by matplotlib.

        ax : matplotlib Axes, default=None
            Axes object to plot on. If `None`, a new figure and axes is
            created.

        colorbar : bool, default=True
            Whether or not to add a colorbar to the plot.

        im_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.imshow` call.

        text_kw : dict, default=None
            Dict with keywords passed to `matplotlib.pyplot.text` call.

            .. versionadded:: 1.2

        Returns
        -------
        display : :class:`~sklearn.metrics.ConfusionMatrixDisplay`

        See Also
        --------
        ConfusionMatrixDisplay.from_estimator : Plot the confusion matrix
            given an estimator, the data, and the label.

        Examples
        --------
        >>> import matplotlib.pyplot as plt
        >>> from sklearn.datasets import make_classification
        >>> from sklearn.metrics import ConfusionMatrixDisplay
        >>> from sklearn.model_selection import train_test_split
        >>> from sklearn.svm import SVC
        >>> X, y = make_classification(random_state=0)
        >>> X_train, X_test, y_train, y_test = train_test_split(
        ...         X, y, random_state=0)
        >>> clf = SVC(random_state=0)
        >>> clf.fit(X_train, y_train)
        SVC(random_state=0)
        >>> y_pred = clf.predict(X_test)
        >>> ConfusionMatrixDisplay.from_predictions(
        ...    y_test, y_pred)
        <...>
        >>> plt.show()
        z.from_predictionsN)rW   rV   rX   r   )r   r   r   r   r   r   r   r   )r   rZ   r   r   rU   )r^   Úy_truerc   rV   rW   rX   r   r   r   r   r   r   r   r   r   rI   Údisps                    r   r]   z'ConfusionMatrixDisplay.from_predictionsa  s«   € õn 	! C¤LÐ!CÐ!CÐ!CÑDÔDÐDàÐ!Øˆ~Ý!.¨v°vÑ!>Ô!>��à!'�åØØØ'ØØð
ñ 
ô 
ˆð ˆs B°~ÐFÑFÔFˆà�yŠyØ)ØØØ+Ø'ØØØð ñ 	
ô 	
ð 		
r   )	rZ   Ú
__module__Ú__qualname__Ú__doc__r   rU   Úclassmethodrd   r]   © r   r   r
   r
      s  € € € € € ðBð BðH <@ð -ð -ð -ð -ð -ð ØØ$ØØØØØðrð rð rð rð rðh ð ØØØØØ$ØØØØØØð#R
ð R
ð R
ð R
ñ „[ðR
ðh ð ØØØØØ$ØØØØØØð!Q
ð Q
ð Q
ð Q
ñ „[ðQ
ð Q
ð Q
r   r
   )Ú	itertoolsr   Únumpyr6   Úsklearn.baser   Úsklearn.metricsr   Ú$sklearn.utils._optional_dependenciesr   Úsklearn.utils._plottingr   Úsklearn.utils.multiclassr   r
   rl   r   r   ú<module>rt      s»   ðð Ð Ð Ð Ð Ð à Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø IÐ IÐ IÐ IÐ IÐ IØ :Ð :Ð :Ð :Ð :Ð :Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2ðd
ð d
ð d
ð d
ð d
ñ d
ô d
ð d
ð d
ð d
r   