← Back
Editing: _kde.cpython-311.pyc
� d�c�a � � � d dl Z d dlmZmZ d dlmZ d dlmZmZm Z m Z mZmZm Z mZmZmZmZmZmZmZmZmZ d dlZddlmZ ddlmZmZ dgZ G d � d� � Zd � ZdS )� N)�linalg�special)�check_random_state)�asarray� atleast_2d�reshape�zeros�newaxis�exp�pi�sqrt�ravel�power� atleast_1d�squeeze�sum� transpose�ones�cov� )�_mvn)�gaussian_kernel_estimate�gaussian_kernel_estimate_log�gaussian_kdec �� � e Zd ZdZdd�Zd� ZeZd� Zd� Zdd�Z d� Z dd �Zd � Zd� Z eZde_ dd �Zd� Zed� � � Zd� Zd� Zd� Zed� � � Zed� � � ZdS )r a& Representation of a kernel-density estimate using Gaussian kernels. Kernel density estimation is a way to estimate the probability density function (PDF) of a random variable in a non-parametric way. `gaussian_kde` works for both uni-variate and multi-variate data. It includes automatic bandwidth determination. The estimation works best for a unimodal distribution; bimodal or multi-modal distributions tend to be oversmoothed. Parameters ---------- dataset : array_like Datapoints to estimate from. In case of univariate data this is a 1-D array, otherwise a 2-D array with shape (# of dims, # of data). bw_method : str, scalar or callable, optional The method used to calculate the estimator bandwidth. This can be 'scott', 'silverman', a scalar constant or a callable. If a scalar, this will be used directly as `kde.factor`. If a callable, it should take a `gaussian_kde` instance as only parameter and return a scalar. If None (default), 'scott' is used. See Notes for more details. weights : array_like, optional weights of datapoints. This must be the same shape as dataset. If None (default), the samples are assumed to be equally weighted Attributes ---------- dataset : ndarray The dataset with which `gaussian_kde` was initialized. d : int Number of dimensions. n : int Number of datapoints. neff : int Effective number of datapoints. .. versionadded:: 1.2.0 factor : float The bandwidth factor, obtained from `kde.covariance_factor`. The square of `kde.factor` multiplies the covariance matrix of the data in the kde estimation. covariance : ndarray The covariance matrix of `dataset`, scaled by the calculated bandwidth (`kde.factor`). inv_cov : ndarray The inverse of `covariance`. Methods ------- evaluate __call__ integrate_gaussian integrate_box_1d integrate_box integrate_kde pdf logpdf resample set_bandwidth covariance_factor Notes ----- Bandwidth selection strongly influences the estimate obtained from the KDE (much more so than the actual shape of the kernel). Bandwidth selection can be done by a "rule of thumb", by cross-validation, by "plug-in methods" or by other means; see [3]_, [4]_ for reviews. `gaussian_kde` uses a rule of thumb, the default is Scott's Rule. Scott's Rule [1]_, implemented as `scotts_factor`, is:: n**(-1./(d+4)), with ``n`` the number of data points and ``d`` the number of dimensions. In the case of unequally weighted points, `scotts_factor` becomes:: neff**(-1./(d+4)), with ``neff`` the effective number of datapoints. Silverman's Rule [2]_, implemented as `silverman_factor`, is:: (n * (d + 2) / 4.)**(-1. / (d + 4)). or in the case of unequally weighted points:: (neff * (d + 2) / 4.)**(-1. / (d + 4)). Good general descriptions of kernel density estimation can be found in [1]_ and [2]_, the mathematics for this multi-dimensional implementation can be found in [1]_. With a set of weighted samples, the effective number of datapoints ``neff`` is defined by:: neff = sum(weights)^2 / sum(weights^2) as detailed in [5]_. `gaussian_kde` does not currently support data that lies in a lower-dimensional subspace of the space in which it is expressed. For such data, consider performing principle component analysis / dimensionality reduction and using `gaussian_kde` with the transformed data. References ---------- .. [1] D.W. Scott, "Multivariate Density Estimation: Theory, Practice, and Visualization", John Wiley & Sons, New York, Chicester, 1992. .. [2] B.W. Silverman, "Density Estimation for Statistics and Data Analysis", Vol. 26, Monographs on Statistics and Applied Probability, Chapman and Hall, London, 1986. .. [3] B.A. Turlach, "Bandwidth Selection in Kernel Density Estimation: A Review", CORE and Institut de Statistique, Vol. 19, pp. 1-33, 1993. .. [4] D.M. Bashtannyk and R.J. Hyndman, "Bandwidth selection for kernel conditional density estimation", Computational Statistics & Data Analysis, Vol. 36, pp. 279-298, 2001. .. [5] Gray P. G., 1969, Journal of the Royal Statistical Society. Series A (General), 132, 272 Examples -------- Generate some random two-dimensional data: >>> import numpy as np >>> from scipy import stats >>> def measure(n): ... "Measurement model, return two coupled measurements." ... m1 = np.random.normal(size=n) ... m2 = np.random.normal(scale=0.5, size=n) ... return m1+m2, m1-m2 >>> m1, m2 = measure(2000) >>> xmin = m1.min() >>> xmax = m1.max() >>> ymin = m2.min() >>> ymax = m2.max() Perform a kernel density estimate on the data: >>> X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] >>> positions = np.vstack([X.ravel(), Y.ravel()]) >>> values = np.vstack([m1, m2]) >>> kernel = stats.gaussian_kde(values) >>> Z = np.reshape(kernel(positions).T, X.shape) Plot the results: >>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots() >>> ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r, ... extent=[xmin, xmax, ymin, ymax]) >>> ax.plot(m1, m2, 'k.', markersize=2) >>> ax.set_xlim([xmin, xmax]) >>> ax.set_ylim([ymin, ymax]) >>> plt.show() Nc � � t t |� � � � | _ | j j dk st d� � �| j j \ | _ | _ |��t |� � � t � � | _ | xj t | j � � z c_ | j j dk rt d� � �t | j � � | j k rt d� � �dt | j dz � � z | _ | j | j k rd}t |� � � | � |�� � d S # t$ j $ r}d}t% j |� � |�d }~ww xY w) Nr z.`dataset` input should have multiple elements.z*`weights` input should be one-dimensional.z%`weights` input should be of length n� a1 Number of dimensions is greater than number of samples. This results in a singular data covariance matrix, which cannot be treated using the algorithms implemented in `gaussian_kde`. Note that `gaussian_kde` interprets each *column* of `dataset` to be a point; consider transposing the input to `dataset`.�� bw_methodab The data appears to lie in a lower-dimensional subspace of the space in which it is expressed. This has resulted in a singular data covariance matrix, which cannot be treated using the algorithms implemented in `gaussian_kde`. Consider performing principle component analysis / dimensionality reduction and using `gaussian_kde` with the transformed data.)r r �dataset�size� ValueError�shape�d�nr �astype�float�_weightsr �weights�ndim�len�_neff� set_bandwidthr �LinAlgError)�selfr r r) �msg�es �2/usr/lib/python3/dist-packages/scipy/stats/_kde.py�__init__zgaussian_kde.__init__� sk � �!�'�'�"2�"2�3�3����|� �1�$�$��M�N�N�N���+��������&�w�/�/�6�6�u�=�=�D�M��M�M�S���/�/�/�M�M��|� �A�%�%� �!M�N�N�N��4�=�!�!�T�V�+�+� �!H�I�I�I��3�t�}�a�/�0�0�0�D�J� �6�D�F�?�?�-�C� �S�/�/�!� 1������3�3�3�3�3���!� 1� 1� 1�?�C� �$�S�)�)�q�0����� 1���s �6E �E9�E4�4E9c � � t t |� � � � }|j \ }}|| j k rG|dk r%|| j k rt || j df� � }d}nd|�d| j ��}t |� � �t | j |� � \ }}t | | j j | j dd�df |j | j |� � }|dd�df S )a Evaluate the estimated pdf on a set of points. Parameters ---------- points : (# of dimensions, # of points)-array Alternatively, a (# of dimensions,) vector can be passed in and treated as a single point. Returns ------- values : (# of points,)-array The values at each point. Raises ------ ValueError : if the dimensionality of the input points is different than the dimensionality of the KDE. r �points have dimension �, dataset has dimension Nr ) r r r# r$ r r"