o
    tBh*                     @   s   d 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
mZ ddlmZ d	d
lmZmZ d	dlmZ g dZeedZG dd deZdS )z5
Kernel Density Estimation
-------------------------
    N)gammainc   )BaseEstimator)check_random_state)_check_sample_weightcheck_is_fitted)	row_norms   )BallTreeDTYPE)KDTree)gaussiantophatepanechnikovexponentiallinearcosine)	ball_treekd_treec                
   @   sd   e Zd ZdZddddddddd	d
	ddZdd ZdddZdd ZdddZdddZ	dd Z
d	S )KernelDensitya
  Kernel Density Estimation.

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

    Parameters
    ----------
    bandwidth : float, default=1.0
        The bandwidth of the kernel.

    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'
        The distance metric to use.  Note that not all metrics are
        valid with all algorithms.  Refer to the documentation of
        :class:`BallTree` and :class:`KDTree` for a description of
        available algorithms.  Note that the normalization of the density
        output is correct only for the Euclidean distance metric. Default
        is 'euclidean'.

    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.

        .. 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])
          ?autor   	euclideanr   T(   N)		bandwidth	algorithmkernelmetricatolrtolbreadth_first	leaf_sizemetric_paramsc       	   
      C   s:   || _ || _|| _|| _|| _|| _|| _|| _|	| _d S N)	r   r   r   r   r   r   r    r!   r"   )
selfr   r   r   r   r   r   r    r!   r"    r%   m/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/neighbors/_kde.py__init__s   s   
zKernelDensity.__init__c                 C   sn   |dkr|t jv rdS |tjv rdS td||tv r0|t| jvr.tdt| ||S td|)Nr   r   r   zinvalid metric: '{0}'zinvalid metric for {0}: '{1}'zinvalid algorithm: '{0}')r   valid_metricsr
   
ValueErrorformat	TREE_DICT)r$   r   r   r%   r%   r&   _choose_algorithm   s   

zKernelDensity._choose_algorithmc                 C   s   |  | j| j}| jdkrtd| jtvrtd| j| j|dt	d}|dur2t
||t	dd}| j}|du r;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   zbandwidth must be positivezinvalid kernel: '{0}'C)orderdtypeNT)only_non_negative)r   r!   sample_weight)r,   r   r   r   r)   r   VALID_KERNELSr*   _validate_datar   r   r"   r+   r!   tree_)r$   Xyr1   r   kwargsr%   r%   r&   fit   s.   

zKernelDensity.fitc              	   C   s|   t |  | j|dtdd}| jjdu r| jjjd }n| jj}| j| }| jj	|| j
| j|| j| jdd}|t|8 }|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.
        r-   F)r.   r/   resetNr   T)hr   r   r   r    
return_log)r   r3   r   r4   r1   datashape
sum_weightr   kernel_densityr   r   r   r    nplog)r$   r5   Natol_Nlog_densityr%   r%   r&   score_samples   s"   
	zKernelDensity.score_samplesc                 C   s   t | |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.
        )r@   sumrE   )r$   r5   r6   r%   r%   r&   score   s   zKernelDensity.scorer	   c                 C   s,  t |  | jdvrt t| jj}t|}|jdd|d}| jj	du r1||j
d  tj}ntt| jj	}|d }t||| }| jdkrXt||| | jS | jdkr|j
d }	|j||	fd}
t|
d	d
}td|	 d| d|	  | j t| }|| |
|ddtjf   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	   )sizeNr   r   T)squaredg      ?r   )r   r   NotImplementedErrorr@   asarrayr4   r<   r   uniformr1   r=   astypeint64cumsumsearchsorted
atleast_2dnormalr   r   r   sqrtnewaxis)r$   	n_samplesrandom_stater<   rnguicumsum_weightr>   dimr5   s_sq
correctionr%   r%   r&   sample  s2   



zKernelDensity.samplec                 C   s   dddiiS )N_xfail_checkscheck_sample_weights_invariancez'sample_weight must have positive valuesr%   )r$   r%   r%   r&   
_more_tagsA  s
   zKernelDensity._more_tags)NNr#   )r	   N)__name__
__module____qualname____doc__r'   r,   r8   rE   rG   r_   rb   r%   r%   r%   r&   r       s$    U
2
&
5r   )rf   numpyr@   scipy.specialr   baser   utilsr   utils.validationr   r   utils.extmathr   
_ball_treer
   r   _kd_treer   r2   r+   r   r%   r%   r%   r&   <module>   s    
