
    0Ph/                         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mZ dd
lmZ ddlmZmZmZ ddlmZ ddl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VALID_METRICS)check_random_state)Interval
StrOptions)	row_norms)_check_sample_weightcheck_is_fittedvalidate_data   )BallTree)KDTree)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     V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/neighbors/_kde.py
<listcomp>zKernelDensity.<listcomp>   s    %U%U%USmC&8%U%U%U    leftbooleanr   )		bandwidth	algorithmkernelmetricatolrtolbreadth_first	leaf_sizemetric_params_parameter_constraints      ?r   	euclideanT(   c       	             || _         || _        || _        || _        || _        || _        || _        || _        |	| _        d S N)	r.   r-   r/   r0   r1   r2   r3   r4   r5   )
selfr-   r.   r/   r0   r1   r2   r3   r4   r5   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.   r0   s      r(   _choose_algorithmzKernelDensity._choose_algorithm   s     --- y8111"{ 21 Yy1??? 3::9Y;OQW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"   r   C)orderdtypeNT)rJ   ensure_non_negative)r0   r4   sample_weight)rC   r.   r0   
isinstancer-   strshape
bandwidth_r   npfloat64r   r5   r@   r4   tree_)r<   XyrL   r.   kwargss         r(   fitzKernelDensity.fit   sD   6 **4>4;GG	dnc** 	-~(("#'!*qwqzA~1F"G;..#$71:a#@1#D!'!*q.)# #nDO$BJ???$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/   r1   r2   r3   
return_log)r   r   rQ   rR   rS   rL   datarO   
sum_weightr1   kernel_densityrP   r/   r2   r3   log)r<   rT   Natol_Nlog_densitys        r(   score_sampleszKernelDensity.score_samples   s      	 $BJeLLL:#+
%a(AA
%AQj//o;, 0 
 
 	rvayy 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"    ( vd((++,,,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   )sizeNrF   r   r   T)squaredg      ?r7   )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.sample4  s   * 	;444%'''z$*/** ..KK19K--:#+TZ]"**2844AAIbj1I&J&JKKM&r*Jq:~>>A;*$$=DGT_!E!EFFF[H$$ *Q-C

C 0
11AQ---DsC$J//C#I>/"'$--  
 7QAAArzM!:::: %$r*   )NNr;   )r   N)__name__
__module____qualname____doc__r   r   r   setr@   keysVALID_KERNELS	itertoolschainr   dictr6   __annotations__r=   rC   r   rW   rc   rf   r~   r%   r*   r(   r   r   &   s        [ [~ HT1d9555J-..
 !jY^^%5%5!6!6&!ABBC:cc-00112JOIO%U%UINNDTDT%U%U%UVWW 

 $47778$47778#hxD@@@A!$ $D   , + + + + +.    \&+  4 4 4	 4l$ $ $L- - - -,3; 3; 3; 3; 3; 3;r*   r   ) r   r   numbersr   r   numpyrQ   scipy.specialr   baser   r   neighbors._baser
   utilsr   utils._param_validationr   r   utils.extmathr   utils.validationr   r   r   
_ball_treer   _kd_treer   r   r@   r   r%   r*   r(   <module>r      sY        " " " " " " " "     " " " " " " . . . . . . . . + + + + + + & & & & & & : : : : : : : : % % % % % % S S S S S S S S S S                     #v66	
A; A; A; A; A;M A; A; A; A; A;r*   