§
    rŠtjG0  ã                   óÔ   — d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
m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mZ ddlmZ ddlmZmZmZ g d¢ZeedœZ G d„ de
¦  «        ZdS )z5
Kernel Density Estimation
-------------------------
é    N)ÚIntegralÚReal)Úgammainc)ÚBaseEstimatorÚ_fit_context)ÚBallTree©ÚVALID_METRICS)ÚKDTree)Úcheck_random_state)ÚIntervalÚ
StrOptions)Ú	row_norms)Ú_check_sample_weightÚcheck_is_fittedÚvalidate_data)ÚgaussianÚtophatÚepanechnikovÚexponentialÚlinearÚcosine)Ú	ball_treeÚkd_treec                   ó  — e Zd ZU dZ eeddd¬¦  «         eddh¦  «        g e ee 	                    ¦   «         ¦  «        dhz  ¦  «        g e ee
¦  «        ¦  «        g e e ej        d	„ e 	                    ¦   «         D ¦   «         Ž ¦  «        ¦  «        g eeddd
¬¦  «        g eeddd
¬¦  «        gdg eeddd
¬¦  «        gdegdœ	Zeed<   ddddddddddœ	d„Zd„ Z ed¬¦  «        dd„¦   «         Zd„ Zdd„Zdd„ZdS )ÚKernelDensitya  Kernel Density Estimation.

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

    Parameters
    ----------
    bandwidth : float or {"scott", "silverman"}, default=1.0
        The bandwidth of the kernel. If bandwidth is a float, it defines the
        bandwidth of the kernel. If bandwidth is a string, one of the estimation
        methods is implemented.

    algorithm : {'kd_tree', 'ball_tree', 'auto'}, default='auto'
        The tree algorithm to use.

    kernel : {'gaussian', 'tophat', 'epanechnikov', 'exponential', 'linear',                  'cosine'}, default='gaussian'
        The kernel to use.

    metric : str, default='euclidean'
        Metric to use for distance computation. See the
        documentation of `scipy.spatial.distance
        <https://docs.scipy.org/doc/scipy/reference/spatial.distance.html>`_ and
        the metrics listed in
        :class:`~sklearn.metrics.pairwise.distance_metrics` for valid metric
        values.

        Not all metrics are valid with all algorithms: refer to the
        documentation of :class:`BallTree` and :class:`KDTree`. Note that the
        normalization of the density output is correct only for the Euclidean
        distance metric.

    atol : float, default=0
        The desired absolute tolerance of the result.  A larger tolerance will
        generally lead to faster execution.

    rtol : float, default=0
        The desired relative tolerance of the result.  A larger tolerance will
        generally lead to faster execution.

    breadth_first : bool, default=True
        If true (default), use a breadth-first approach to the problem.
        Otherwise use a depth-first approach.

    leaf_size : int, default=40
        Specify the leaf size of the underlying tree.  See :class:`BallTree`
        or :class:`KDTree` for details.

    metric_params : dict, default=None
        Additional parameters to be passed to the tree for use with the
        metric.  For more information, see the documentation of
        :class:`BallTree` or :class:`KDTree`.

    Attributes
    ----------
    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    tree_ : ``BinaryTree`` instance
        The tree algorithm for fast generalized N-point problems.

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

    bandwidth_ : float
        Value of the bandwidth, given directly by the bandwidth parameter or
        estimated using the 'scott' or 'silverman' method.

        .. versionadded:: 1.0

    See Also
    --------
    sklearn.neighbors.KDTree : K-dimensional tree for fast generalized N-point
        problems.
    sklearn.neighbors.BallTree : Ball tree for fast generalized N-point
        problems.

    Examples
    --------
    Compute a gaussian kernel density estimate with a fixed bandwidth.

    >>> from sklearn.neighbors import KernelDensity
    >>> import numpy as np
    >>> rng = np.random.RandomState(42)
    >>> X = rng.random_sample((100, 3))
    >>> kde = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(X)
    >>> log_density = kde.score_samples(X[:3])
    >>> log_density
    array([-1.52955942, -1.51462041, -1.60244657])
    r   NÚneither)ÚclosedÚscottÚ	silvermanÚautoc                 ó(   — g | ]}t           |         ‘ŒS © r	   )Ú.0Úalgs     úT/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/neighbors/_kde.pyú
<listcomp>zKernelDensity.<listcomp>‘   s   € Ð%UÐ%UÐ%U¸S¥m°CÔ&8Ð%UÐ%UÐ%Uó    ÚleftÚbooleané   )	Ú	bandwidthÚ	algorithmÚkernelÚmetricÚatolÚrtolÚbreadth_firstÚ	leaf_sizeÚmetric_paramsÚ_parameter_constraintsç      ð?r   Ú	euclideanTé(   c       	         ó„   — || _         || _        || _        || _        || _        || _        || _        || _        |	| _        d S ©N)	r-   r,   r.   r/   r0   r1   r2   r3   r4   )
Úselfr,   r-   r.   r/   r0   r1   r2   r3   r4   s
             r&   Ú__init__zKernelDensity.__init__›   sK   € ð #ˆŒØ"ˆŒØˆŒØˆŒØˆŒ	ØˆŒ	Ø*ˆÔØ"ˆŒØ*ˆÔÐÐr(   c                 óÚ   — |dk    r"|t           j        v rdS |t          j        v rdS d S |t          |         j        vr.t	          d                     t          |         |¦  «        ¦  «        ‚|S )Nr!   r   r   zinvalid metric for {0}: '{1}')r   Úvalid_metricsr   Ú	TREE_DICTÚ
ValueErrorÚformat)r;   r-   r/   s      r&   Ú_choose_algorithmzKernelDensity._choose_algorithm²   s…   € ð ˜ÒÐà�Ô-Ð-Ð-Ø �yØ�8Ô1Ð1Ð1Ø"�{ð 2Ð1ð �Y yÔ1Ô?Ð?Ð?Ý Ø3×:Ò:½9ÀYÔ;OÐQWÑXÔXñô ð ð Ðr(   F)Úprefer_skip_nested_validationc                 ó\  — |                       | j        | j        ¦  «        }t          | j        t
          ¦  «        rx| j        dk    r'|j        d         d|j        d         dz   z  z  | _        nR| j        dk    r:|j        d         |j        d         dz   z  dz  d|j        d         dz   z  z  | _        n| j        | _        t          | |dt          j
        ¬	¦  «        }|�t          ||t          j
        d¬¦  «        }| j        }|€i }t          |         |f| j        | j        |dœ|¤Ž| _        | S )a”  Fit the Kernel Density model on the data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points.  Each row
            corresponds to a single data point.

        y : None
            Ignored. This parameter exists only for compatibility with
            :class:`~sklearn.pipeline.Pipeline`.

        sample_weight : array-like of shape (n_samples,), default=None
            List of sample weights attached to the data X.

            .. versionadded:: 0.20

        Returns
        -------
        self : object
            Returns the instance itself.
        r   r   éÿÿÿÿr+   é   r    é   ÚC)ÚorderÚdtypeNT)rJ   Úensure_non_negative)r/   r3   Úsample_weight)rB   r-   r/   Ú
isinstancer,   ÚstrÚshapeÚ
bandwidth_r   ÚnpÚfloat64r   r4   r?   r3   Útree_)r;   ÚXÚyrL   r-   Úkwargss         r&   ÚfitzKernelDensity.fitÂ   sD  € ð6 ×*Ò*¨4¬>¸4¼;ÑGÔGˆ	å�d”n¥cÑ*Ô*ð 	-ØŒ~ Ò(Ð(Ø"#¤'¨!¤*°°q´w¸q´zÀA±~Ñ1FÑ"G�”�Ø” ;Ò.Ð.Ø#$¤7¨1¤:°´¸´¸a±Ñ#@À1Ñ#DØ˜!œ' !œ* q™.Ñ)ñ#�”øð #œnˆDŒOå˜$ ¨µB´JÐ?Ñ?Ô?ˆàÐ$Ý0Ø˜q­¬
Èðñ ô ˆMð Ô#ˆØˆ>ØˆFÝ˜yÔ)Øð
à”;Ø”nØ'ð	
ð 
ð
 ð
ð 
ˆŒ
ð ˆr(   c           	      ól  — t          | ¦  «         t          | |dt          j        d¬¦  «        }| j        j        €| j        j        j        d         }n| j        j        }| j	        |z  }| j         
                    || j        | j        || j        | j        d¬¦  «        }|t          j        |¦  «        z  }|S )a  Compute the log-likelihood of each sample under the model.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            An array of points to query.  Last dimension should match dimension
            of training data (n_features).

        Returns
        -------
        density : ndarray of shape (n_samples,)
            Log-likelihood of each sample in `X`. These are normalized to be
            probability densities, so values will be low for high-dimensional
            data.
        rH   F)rI   rJ   ÚresetNr   T)Úhr.   r0   r1   r2   Ú
return_log)r   r   rQ   rR   rS   rL   ÚdatarO   Ú
sum_weightr0   Úkernel_densityrP   r.   r1   r2   Úlog)r;   rT   ÚNÚatol_NÚlog_densitys        r&   Úscore_sampleszKernelDensity.score_samplesü   sµ   € õ  	˜ÑÔÐõ ˜$ ¨µB´JÀeÐLÑLÔLˆØŒ:Ô#Ð+Ø”
”Ô% aÔ(ˆAˆAà”
Ô%ˆAØ”˜Q‘ˆØ”j×/Ò/ØØŒoØ”;ØØ”ØÔ,Øð 0ñ 
ô 
ˆð 	•r”v˜a‘y”yÑ ˆØÐr(   c                 óP   — t          j        |                      |¦  «        ¦  «        S )a}  Compute the total log-likelihood under the model.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points.  Each row
            corresponds to a single data point.

        y : None
            Ignored. This parameter exists only for compatibility with
            :class:`~sklearn.pipeline.Pipeline`.

        Returns
        -------
        logprob : float
            Total log-likelihood of the data in X. This is normalized to be a
            probability density, so the value will be low for high-dimensional
            data.
        )rQ   Úsumrc   )r;   rT   rU   s      r&   ÚscorezKernelDensity.score"  s"   € õ( Œv�d×(Ò(¨Ñ+Ô+Ñ,Ô,Ð,r(   c                 ó¤  — t          | ¦  «         | j        dvrt          ¦   «         ‚t          j        | j        j        ¦  «        }t          |¦  «        }|                     dd|¬¦  «        }| j        j	        €.||j
        d         z                       t          j        ¦  «        }nPt          j        t          j        | j        j	        ¦  «        ¦  «        }|d         }t          j        |||z  ¦  «        }| j        dk    r3t          j        |                     ||         | j        ¦  «        ¦  «        S | j        dk    r‘|j
        d         }	|                     ||	f¬¦  «        }
t%          |
d	¬
¦  «        }t'          d|	z  d|z  ¦  «        d|	z  z  | j        z  t          j        |¦  «        z  }||         |
|dd…t          j        f         z  z   S dS )a�  Generate random samples from the model.

        Currently, this is implemented only for gaussian and tophat kernels.

        Parameters
        ----------
        n_samples : int, default=1
            Number of samples to generate.

        random_state : int, RandomState instance or None, default=None
            Determines random number generation used to generate
            random samples. Pass an int for reproducible results
            across multiple function calls.
            See :term:`Glossary <random_state>`.

        Returns
        -------
        X : array-like of shape (n_samples, n_features)
            List of samples.
        )r   r   r   r+   )ÚsizeNrE   r   r   T)Úsquaredg      à?r6   )r   r.   ÚNotImplementedErrorrQ   ÚasarrayrS   r\   r   ÚuniformrL   rO   ÚastypeÚint64ÚcumsumÚsearchsortedÚ
atleast_2dÚnormalrP   r   r   ÚsqrtÚnewaxis)r;   Ú	n_samplesÚrandom_stater\   ÚrngÚuÚiÚcumsum_weightr]   ÚdimrT   Ús_sqÚ
corrections                r&   ÚsamplezKernelDensity.sample8  s¬  € õ* 	˜ÑÔÐàŒ;Ð4Ð4Ð4Ý%Ñ'Ô'Ð'åŒz˜$œ*œ/Ñ*Ô*ˆå  Ñ.Ô.ˆØ�KŠK˜˜1 9ˆKÑ-Ô-ˆØŒ:Ô#Ð+Ø�T”Z ”]Ñ"×*Ò*­2¬8Ñ4Ô4ˆAˆAåœI¥b¤j°´Ô1IÑ&JÔ&JÑKÔKˆMØ& rÔ*ˆJÝ” ¨q°:©~Ñ>Ô>ˆAØŒ;˜*Ò$Ð$Ý”= §¢¨D°¬G°T´_Ñ!EÔ!EÑFÔFÐFàŒ[˜HÒ$Ð$ð ”*˜Q”-ˆCØ—
’
 ¨CÐ 0�
Ñ1Ô1ˆAÝ˜Q¨Ð-Ñ-Ô-ˆDå˜˜s™ C¨$¡JÑ/Ô/°C¸#±IÑ>Ø”/ñ"å”'˜$‘-”-ñ ð ð
 ˜”7˜Q ¨A¨A¨A­r¬z¨MÔ!:Ñ:Ñ:Ð:ð %Ð$r(   )NNr:   )r+   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úsetr?   ÚkeysÚVALID_KERNELSÚ	itertoolsÚchainr   Údictr5   Ú__annotations__r<   rB   r   rW   rc   rf   r~   r#   r(   r&   r   r   *   s  € € € € € € ð[ð [ð~ ˆH�T˜1˜d¨9Ð5Ñ5Ô5ØˆJ˜ Ð-Ñ.Ô.ð
ð !�j   Y§^¢^Ñ%5Ô%5Ñ!6Ô!6¸&¸Ñ!AÑBÔBÐCØ�:˜c˜c -Ñ0Ô0Ñ1Ô1Ð2àˆJØ��O�I”OÐ%UÐ%UÀIÇNÂNÑDTÔDTÐ%UÑ%UÔ%UÐVÑWÔWñô ð
ð
 �˜$  4°Ð7Ñ7Ô7Ð8Ø�˜$  4°Ð7Ñ7Ô7Ð8Ø#˜Ø�h˜x¨¨D¸Ð@Ñ@Ô@ÐAØ ˜ð!$ð $Ð˜Dð ð ñ ð, ØØØØØØØØð+ð +ð +ð +ð +ð.ð ð ð  €\à&+ðñ ô ð4ð 4ð 4ñ	ô ð4ðl$ð $ð $ðL-ð -ð -ð -ð,3;ð 3;ð 3;ð 3;ð 3;ð 3;r(   r   ) r‚   r†   Únumbersr   r   ÚnumpyrQ   Úscipy.specialr   Úsklearn.baser   r   Úsklearn.neighbors._ball_treer   Úsklearn.neighbors._baser
   Úsklearn.neighbors._kd_treer   Úsklearn.utilsr   Úsklearn.utils._param_validationr   r   Úsklearn.utils.extmathr   Úsklearn.utils.validationr   r   r   r…   r?   r   r#   r(   r&   ú<module>r•      sm  ððð ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø -Ð -Ð -Ð -Ð -Ð -Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ø +Ð +Ð +Ð +Ð +Ð +ðð ð ð ð ð ð ð ð ð ðð ð €ð #¨vÐ6Ð6€	ð
A;ð A;ð A;ð A;ð A;�Mñ A;ô A;ð A;ð A;ð A;r(   