U
    9%e0                     @   s   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 ddlmZ ddlmZ ddddddgZeedZG d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   )BallTree)KDTreegaussiantophatZepanechnikovZexponentialZlinearZcosine)	ball_treekd_treec                   @   s  e Zd ZU dZeeddddeddhgeee	 dhB geee
geeejd	d
 e	 D  geeddddgeeddddgdgeeddddgdegd	Zeed< dddddddddd	ddZdd Zedd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 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   NZneither)closedscott	silvermanautoc                 C   s   g | ]}t | qS  r   ).0algr   r   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sklearn/neighbors/_kde.py
<listcomp>   s     zKernelDensity.<listcomp>leftbooleanr   )		bandwidth	algorithmkernelmetricatolrtolbreadth_first	leaf_sizemetric_params_parameter_constraints      ?r   Z	euclideanT(   c       	   
      C   s:   || _ || _|| _|| _|| _|| _|| _|| _|	| _d S )N)	r$   r#   r%   r&   r'   r(   r)   r*   r+   )
selfr#   r$   r%   r&   r'   r(   r)   r*   r+   r   r   r   __init__   s    zKernelDensity.__init__c                 C   sP   |dkr&|t jkrdS |tjkrLdS n&|t| jkrHtdt| ||S d S )Nr   r   r   zinvalid metric for {0}: '{1}')r   Zvalid_metricsr   	TREE_DICT
ValueErrorformat)r/   r$   r&   r   r   r   _choose_algorithm   s    

zKernelDensity._choose_algorithmF)Zprefer_skip_nested_validationc                 C   s   |  | j| j}t| jtr| jdkrF|jd d|jd d   | _q| jdkr|jd |jd d  d d|jd d   | _n| j| _| j|dt	j
d	}|d
k	rt||t	j
dd}| j}|d
kri }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)r9   Zonly_non_negative)r&   r*   sample_weight)r4   r$   r&   
isinstancer#   strshape
bandwidth__validate_datanpfloat64r   r+   r1   r*   tree_)r/   Xyr:   r$   kwargsr   r   r   fit   s<    
 
   zKernelDensity.fitc              	   C   s~   t |  | j|dtjdd}| jjdkr8| 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.
        r7   F)r8   r9   resetNr   T)hr%   r'   r(   r)   Z
return_log)r   r?   r@   rA   rB   r:   datar=   
sum_weightr'   Zkernel_densityr>   r%   r(   r)   log)r/   rC   NZatol_NZ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@   sumrM   )r/   rC   rD   r   r   r   score  s    zKernelDensity.scorec                 C   s.  t |  | jdkrt t| jj}t|}|jdd|d}| jj	dkrb||j
d  tj}n,tt| jj	}|d }t||| }| jdkrt||| | 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   )sizeNr5   r   r   T)Zsquaredg      ?r-   )r   r%   NotImplementedErrorr@   ZasarrayrB   rI   r
   uniformr:   r=   ZastypeZint64ZcumsumZsearchsortedZ
atleast_2dnormalr>   r   r   sqrtZnewaxis)r/   Z	n_samplesZrandom_staterI   rnguiZcumsum_weightrJ   dimrC   Zs_sqZ
correctionr   r   r   sample1  s0    


zKernelDensity.samplec                 C   s   dddiiS )NZ_xfail_checksZcheck_sample_weights_invariancez'sample_weight must have positive valuesr   )r/   r   r   r   
_more_tagsf  s
    zKernelDensity._more_tags)NN)N)r   N)__name__
__module____qualname____doc__r   r   r   setr1   keysVALID_KERNELS	itertoolschainr   dictr,   __annotations__r0   r4   r   rF   rM   rO   rY   rZ   r   r   r   r   r   #   sF   
_
6&

5r   )r^   rb   numbersr   r   numpyr@   Zscipy.specialr   baser   r   Zneighbors._baser	   utilsr
   Zutils._param_validationr   r   Zutils.extmathr   Zutils.validationr   r   Z
_ball_treer   Z_kd_treer   ra   r1   r   r   r   r   r   <module>   s*   	
