B
    0d+                 @   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 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)gammainc   )BaseEstimator)check_random_state)_check_sample_weightcheck_is_fitted)	row_norms   )BallTreeDTYPE)KDTreegaussiantophatZepanechnikovZexponentialZlinearZ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])
    g      ?autor   Z	euclideanr   T(   N)		bandwidth	algorithmkernelmetricatolrtolbreadth_first	leaf_sizemetric_paramsc   	   
      C   sp   || _ || _|| _|| _|| _|| _|| _|| _|	| _| 	| j | j |dkrVt
d|tkrlt
d|d S )Nr   zbandwidth must be positivezinvalid kernel: '{0}')r   r   r   r   r   r   r   r   r   _choose_algorithm
ValueErrorVALID_KERNELSformat)
selfr   r   r   r   r   r   r   r   r    r"   H/var/www/html/venv/lib/python3.7/site-packages/sklearn/neighbors/_kde.py__init__s   s    zKernelDensity.__init__c             C   st   |dkr4|t jkrdS |tjkr$dS td|n<|tkrb|t| jkr^tdt| ||S td|d S )Nr   r   r   zinvalid metric: '{0}'zinvalid metric for {0}: '{1}'zinvalid algorithm: '{0}')r   Zvalid_metricsr
   r   r    	TREE_DICT)r!   r   r   r"   r"   r#   r      s    

zKernelDensity._choose_algorithmc             C   s   |  | j| j}| j|dtd}|dk	rHt||t}| dkrHtd| j}|dkrZi }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.
        C)orderdtypeNr   z'sample_weight must have positive values)r   r   sample_weight)r   r   r   _validate_datar   r   minr   r   r%   r   tree_)r!   Xyr)   r   kwargsr"   r"   r#   fit   s     
zKernelDensity.fitc          	   C   s|   t |  | j|dtdd}| jjdkr6| 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   Z
return_log)r   r*   r   r,   r)   datashape
sum_weightr   Zkernel_densityr   r   r   r   nplog)r!   r-   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.
        )r6   sumr9   )r!   r-   r.   r"   r"   r#   score   s    zKernelDensity.scorer	   c             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	   )sizeNr   r   T)Zsquaredg      ?g      ?)r   r   NotImplementedErrorr6   Zasarrayr,   r3   r   uniformr)   r4   ZastypeZint64ZcumsumZsearchsortedZ
atleast_2dnormalr   r   r   sqrtZnewaxis)r!   Z	n_samplesZrandom_stater3   rnguiZcumsum_weightr5   dimr-   Zs_sqZ
correctionr"   r"   r#   sample  s&    


*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_tagsD  s    zKernelDensity._more_tags)NN)N)r	   N)__name__
__module____qualname____doc__r$   r   r0   r9   r;   rF   rG   r"   r"   r"   r#   r       s    Q
+&

5r   )rK   numpyr6   Zscipy.specialr   baser   utilsr   Zutils.validationr   r   Zutils.extmathr   Z
_ball_treer
   r   Z_kd_treer   r   r%   r   r"   r"   r"   r#   <module>   s    
