U
    _»{js  ã                   @   sn   d dl mZ d dl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 G d
d„ deeƒZdS )é    )ÚRealNé   )ÚBaseEstimatorÚ_fit_context)ÚInterval)Úmean_variance_axisÚmin_max_axis)Úcheck_is_fittedé   )ÚSelectorMixinc                   @   s^   e Zd ZU dZdeedddd�giZeed< dd	d
„Z	e
dd�ddd„ƒZdd„ Zdd„ ZdS )ÚVarianceThresholdat  Feature selector that removes all low-variance features.

    This feature selection algorithm looks only at the features (X), not the
    desired outputs (y), and can thus be used for unsupervised learning.

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

    Parameters
    ----------
    threshold : float, default=0
        Features with a training-set variance lower than this threshold will
        be removed. The default is to keep all features with non-zero variance,
        i.e. remove the features that have the same value in all samples.

    Attributes
    ----------
    variances_ : array, shape (n_features,)
        Variances of individual features.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    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
    --------
    SelectFromModel: Meta-transformer for selecting features based on
        importance weights.
    SelectPercentile : Select features according to a percentile of the highest
        scores.
    SequentialFeatureSelector : Transformer that performs Sequential Feature
        Selection.

    Notes
    -----
    Allows NaN in the input.
    Raises ValueError if no feature in X meets the variance threshold.

    Examples
    --------
    The following dataset has integer features, two of which are the same
    in every sample. These are removed with the default setting for threshold::

        >>> from sklearn.feature_selection import VarianceThreshold
        >>> X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]]
        >>> selector = VarianceThreshold()
        >>> selector.fit_transform(X)
        array([[2, 0],
               [1, 4],
               [1, 1]])
    Ú	thresholdr   NÚleft)ÚclosedÚ_parameter_constraintsç        c                 C   s
   || _ d S ©N)r   )Úselfr   © r   úl/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sklearn/feature_selection/_variance_threshold.pyÚ__init__M   s    zVarianceThreshold.__init__T)Zprefer_skip_nested_validationc           	      C   sô   | j |dtjdd�}t|dƒrTt|dd�\}| _| jdkr|t|dd�\}}|| }n(tj|dd�| _| jdkr|tj	|dd�}| jdkr¦t 
| j|g¡}tj|dd�| _t t | j¡ | j| jkB ¡rðd}|jd dkrà|d	7 }t| | j¡ƒ‚| S )
a  Learn empirical variances from X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Data from which to compute variances, where `n_samples` is
            the number of samples and `n_features` is the number of features.

        y : any, default=None
            Ignored. This parameter exists only for compatibility with
            sklearn.pipeline.Pipeline.

        Returns
        -------
        self : object
            Returns the instance itself.
        )ZcsrZcscz	allow-nan)Zaccept_sparseZdtypeZforce_all_finiteZtoarrayr   )Zaxisz4No feature in X meets the variance threshold {0:.5f}r
   z (X contains only one sample))Z_validate_dataÚnpZfloat64Úhasattrr   Ú
variances_r   r   ZnanvarZptpÚarrayZnanminÚallÚisfiniteÚshapeÚ
ValueErrorÚformat)	r   ÚXÚyÚ_ZminsZmaxesZpeak_to_peaksZcompare_arrÚmsgr   r   r   ÚfitP   s.    ü




 zVarianceThreshold.fitc                 C   s   t | ƒ | j| jkS r   )r	   r   r   ©r   r   r   r   Ú_get_support_mask‚   s    z#VarianceThreshold._get_support_maskc                 C   s   ddiS )NÚ	allow_nanTr   r%   r   r   r   Ú
_more_tags‡   s    zVarianceThreshold._more_tags)r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚdictÚ__annotations__r   r   r$   r&   r(   r   r   r   r   r      s   
; ÿ
1r   )Únumbersr   Únumpyr   Úbaser   r   Zutils._param_validationr   Zutils.sparsefuncsr   r   Zutils.validationr	   Z_baser   r   r   r   r   r   Ú<module>   s   