Ë
    ÷Q(hð/  ã                   óÌ   — 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y)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j                  «       «      dhz  «      g e ee
«      «      g e e ej                  ej                  «       D � ��cg c]  }t        |   ‘Œ c}}} Ž «      «      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yc c}}} w )Ú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ÚautoÚleftÚbooleanr   )	Ú	bandwidthÚ	algorithmÚkernelÚmetricÚatolÚrtolÚbreadth_firstÚ	leaf_sizeÚmetric_paramsÚ_parameter_constraintsç      ð?r   Ú	euclideanTé(   c       	         ó‚   — || _         || _        || _        || _        || _        || _        || _        || _        |	| _        y ©N)	r&   r%   r'   r(   r)   r*   r+   r,   r-   )
Úselfr%   r&   r'   r(   r)   r*   r+   r,   r-   s
             úT/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sklearn/neighbors/_kde.pyÚ__init__zKernelDensity.__init__—   sE   € ð #ˆŒØ"ˆŒØˆŒØˆŒØˆŒ	ØˆŒ	Ø*ˆÔØ"ˆŒØ*ˆÕó    c                 óÌ   — |dk(  r'|t         j                  v ry|t        j                  v ryy |t        |   j                  vr"t	        dj                  t        |   |«      «      ‚|S )Nr"   r   r   zinvalid metric for {0}: '{1}')r   Úvalid_metricsr   Ú	TREE_DICTÚ
ValueErrorÚformat)r4   r&   r(   s      r5   Ú_choose_algorithmzKernelDensity._choose_algorithm®   ss   € ð ˜Òàœ×-Ñ-Ñ-Ø Øœ8×1Ñ1Ñ1Ø"ð 2ð œY yÑ1×?Ñ?Ñ?Ü Ø3×:Ñ:¼9ÀYÑ;OÐQWÓXóð ð Ðr7   F)Úprefer_skip_nested_validationc                 ó¬  — | j                  | j                  | j                  «      }t        | j                  t
        «      rŠ| j                  dk(  r+|j                  d   d|j                  d   dz   z  z  | _        na| j                  dk(  rR|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ÚdtypeT)rD   Úensure_non_negative)r(   r,   Úsample_weight)r=   r&   r(   Ú
isinstancer%   ÚstrÚshapeÚ
bandwidth_r   ÚnpÚfloat64r   r-   r:   r,   Útree_)r4   ÚXÚyrF   r&   Úkwargss         r5   ÚfitzKernelDensity.fit¾   s7  € ð6 ×*Ñ*¨4¯>©>¸4¿;¹;ÓGˆ	ä�d—n‘n¤cÔ*Ø�~‰~ Ò(Ø"#§'¡'¨!¡*°°q·w±w¸q±zÀA±~Ñ1FÑ"G�•Ø—‘ ;Ò.Ø#$§7¡7¨1¡:°·±¸±¸a±Ñ#@À1Ñ#DØ˜!Ÿ'™' !™* q™.Ñ)ñ#�•ð #Ÿn™nˆDŒOä˜$ ¨´B·J±JÔ?ˆàÐ$Ü0Ø˜q¬¯
©
ÈôˆMð ×#Ñ#ˆØˆ>ØˆFÜ˜yÑ)Øð
à—;‘;Ø—n‘nØ'ñ	
ð
 ñ
ˆŒ
ð ˆr7   c           	      óÚ  — t        | «       t        | |dt        j                  d¬«      }| j                  j
                  €$| j                  j                  j                  d   }n| j                  j                  }| j                  |z  }| j                  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.
        rB   F)rC   rD   Úresetr   T)Úhr'   r)   r*   r+   Ú
return_log)r   r   rK   rL   rM   rF   ÚdatarI   Ú
sum_weightr)   Úkernel_densityrJ   r'   r*   r+   Úlog)r4   rN   ÚNÚatol_NÚlog_densitys        r5   Úscore_sampleszKernelDensity.score_samplesø   sÂ   € ô  	˜Ôô ˜$ ¨´B·J±JÀeÔLˆØ�:‰:×#Ñ#Ð+Ø—
‘
—‘×%Ñ% aÑ(‰Aà—
‘
×%Ñ%ˆAØ—‘˜Q‘ˆØ—j‘j×/Ñ/ØØ�o‰oØ—;‘;ØØ—‘Ø×,Ñ,Øð 0ó 
ˆð 	”r—v‘v˜a“yÑ ˆØÐr7   c                 óJ   — t        j                  | 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.
        )rK   Úsumr]   )r4   rN   rO   s      r5   ÚscorezKernelDensity.score  s   € ô( �v‰v�d×(Ñ(¨Ó+Ó,Ð,r7   c                 óÐ  — t        | «       | j                  dvr
t        «       ‚t        j                  | j
                  j                  «      }t        |«      }|j                  dd|¬«      }| j
                  j                  €0||j                  d   z  j                  t        j                  «      }nZt        j                  t        j                  | j
                  j                  «      «      }|d   }t        j                  |||z  «      }| j                  dk(  r2t        j                  |j!                  ||   | j"                  «      «      S | j                  dk(  r‹|j                  d   }	|j!                  ||	f¬«      }
t%        |
d	¬
«      }t'        d|	z  d|z  «      d|	z  z  | j"                  z  t        j(                  |«      z  }||   |
|dd…t        j*                  f   z  z   S y)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   )ÚsizeNr@   r   r   T)Úsquaredg      à?r/   )r   r'   ÚNotImplementedErrorrK   ÚasarrayrM   rV   r
   ÚuniformrF   rI   ÚastypeÚint64ÚcumsumÚsearchsortedÚ
atleast_2dÚnormalrJ   r   r   ÚsqrtÚnewaxis)r4   Ú	n_samplesÚrandom_staterV   ÚrngÚuÚiÚcumsum_weightrW   ÚdimrN   Ús_sqÚ
corrections                r5   ÚsamplezKernelDensity.sample4  s”  € ô* 	˜Ôà�;‰;Ð4Ñ4Ü%Ó'Ð'ä�z‰z˜$Ÿ*™*Ÿ/™/Ó*ˆä  Ó.ˆØ�K‰K˜˜1 9ˆKÓ-ˆØ�:‰:×#Ñ#Ð+Ø�T—Z‘Z ‘]Ñ"×*Ñ*¬2¯8©8Ó4‰AäŸI™I¤b§j¡j°·±×1IÑ1IÓ&JÓKˆMØ& rÑ*ˆJÜ—‘ ¨q°:©~Ó>ˆAØ�;‰;˜*Ò$Ü—=‘= §¡¨D°©G°T·_±_Ó!EÓFÐFà�[‰[˜HÒ$ð —*‘*˜Q‘-ˆCØ—
‘
 ¨CÐ 0�
Ó1ˆAÜ˜Q¨Ô-ˆDä˜˜s™ C¨$¡JÓ/°C¸#±IÑ>Ø—/‘/ñ"ä—'‘'˜$“-ñ ð ð
 ˜‘7˜Q ªA¬r¯z©z¨MÑ!:Ñ:Ñ:Ð:ð %r7   )NNr3   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úsetr:   ÚkeysÚVALID_KERNELSÚ	itertoolsÚchainr	   r   Údictr.   Ú__annotations__r6   r=   r   rQ   r]   r`   rx   )Ú.0Úalgr	   s   000r5   r   r   &   s@  … ñ[ñ~ �T˜1˜d¨9Ô5Ù˜ Ð-Ó.ð
ñ !¡ Y§^¡^Ó%5Ó!6¸&¸Ñ!AÓBÐCÙ™c -Ó0Ó1Ð2áÙ�O�I—O‘OÀIÇNÁNÓDT×%UÐ%U¸S¤m°CÓ&8Ô%UÐVÓWóð
ñ
 ˜$  4°Ô7Ð8Ù˜$  4°Ô7Ð8Ø#˜Ù˜x¨¨D¸Ô@ÐAØ ˜ñ!$Ð˜Dó ð, ØØØØØØØØô+ò.ñ  à&+ôò4ó	ð4òl$óL-ô,3;ùôO &Vs   Á/C+r   ) r|   r€   Únumbersr   r   ÚnumpyrK   Úscipy.specialr   Úbaser   r   Úneighbors._baser	   Úutilsr
   Úutils._param_validationr   r   Úutils.extmathr   Úutils.validationr   r   r   Ú
_ball_treer   Ú_kd_treer   r   r:   r   © r7   r5   ú<module>r’      sT   ðñó ß "ã Ý "ç .Ý +Ý &ß :Ý %ß SÑ SÝ  Ý ò€ð #¨vÑ6€	ô
A;�Mõ A;r7   