
    0Phe                        d Z ddlZddlmZ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mZmZmZmZ dd
lmZmZmZ ddlmZmZ ddlmZm Z  ddl!m"Z" d Z#d Z$ eddgdgdd          d             Z%d Z& eddgdgdd          d             Z' eddgdgdgdgdd          dddd            Z( eddgdgdgdgdd          dddd            Z) G d de"e          Z* G d  d!e*          Z+ G d" d#e*          Z, G d$ d%e*          Z- G d& d'e*          Z. G d( d)e*          Z/ G d* d+e*          Z0dS ),zUnivariate features selection.    N)IntegralReal)specialstats)issparse   )BaseEstimator_fit_context)LabelBinarizer)as_float_arraycheck_array	check_X_y	safe_masksafe_sqr)Interval
StrOptionsvalidate_params)	row_normssafe_sparse_dot)check_is_fittedvalidate_data   )SelectorMixinc                     t          | d          } t          j        | j                  j        | t          j        |           <   | S )z
    Fixes Issue #1240: NaNs can't be properly compared, so change them to the
    smallest value of scores's dtype. -inf seems to be unreliable.
    T)copy)r   npfinfodtypeminisnan)scoress    o/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/feature_selection/_univariate_selection.py_clean_nansr#      s>     F...F!x559F28FM    c                     t          |           }d | D             } t          j        d | D                       }t          j        |          }t          d | D                       }d | D             }t          |          dz  }d |D             }||t	          |          z  z
  }d}	t          |           D ]\  }
}|	||
         ||
         z  z  }	|	|t	          |          z  z  }	||	z
  }|dz
  }||z
  }|	t	          |          z  }|t	          |          z  }t          j        |dk              d	         }t          j        |          d	         j        |j        k    r$|j        rt          j
        d
|z  t                     ||z  }t          j        |                                          }t          j        |||          }||fS )a  Perform a 1-way ANOVA.

    The one-way ANOVA tests the null hypothesis that 2 or more groups have
    the same population mean. The test is applied to samples from two or
    more groups, possibly with differing sizes.

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

    Parameters
    ----------
    *args : {array-like, sparse matrix}
        Sample1, sample2... The sample measurements should be given as
        arguments.

    Returns
    -------
    f_statistic : float
        The computed F-value of the test.
    p_value : float
        The associated p-value from the F-distribution.

    Notes
    -----
    The ANOVA test has important assumptions that must be satisfied in order
    for the associated p-value to be valid.

    1. The samples are independent
    2. Each sample is from a normally distributed population
    3. The population standard deviations of the groups are all equal. This
       property is known as homoscedasticity.

    If these assumptions are not true for a given set of data, it may still be
    possible to use the Kruskal-Wallis H-test (`scipy.stats.kruskal`_) although
    with some loss of power.

    The algorithm is from Heiman[2], pp.394-7.

    See ``scipy.stats.f_oneway`` that should give the same results while
    being less efficient.

    References
    ----------
    .. [1] Lowry, Richard.  "Concepts and Applications of Inferential
           Statistics". Chapter 14.
           http://vassarstats.net/textbook

    .. [2] Heiman, G.W.  Research Methods in Statistics. 2002.
    c                 ,    g | ]}t          |          S  )r   .0as     r"   
<listcomp>zf_oneway.<locals>.<listcomp>\   s     ,,,!N1,,,r$   c                 (    g | ]}|j         d          S )r   )shaper(   s     r"   r+   zf_oneway.<locals>.<listcomp>]   s    #=#=#=1AGAJ#=#=#=r$   c              3   \   K   | ]'}t          |                              d           V  (dS )r   axisN)r   sumr(   s     r"   	<genexpr>zf_oneway.<locals>.<genexpr>_   s6      ;;Xa[[__!_,,;;;;;;r$   c                 ^    g | ]*}t          j        |                    d                     +S )r   r/   )r   asarrayr1   r(   s     r"   r+   zf_oneway.<locals>.<listcomp>`   s.    999qAEEqEMM**999r$   r   c                     g | ]}|d z  S )r   r'   )r)   ss     r"   r+   zf_oneway.<locals>.<listcomp>b   s    333A1a4333r$           r   r   zFeatures %s are constant.)lenr   arrayr1   float	enumeratewherenonzerosizewarningswarnUserWarningr4   ravelr   fdtrc)args	n_classesn_samples_per_class	n_samples
ss_alldata	sums_argssquare_of_sums_alldatasquare_of_sums_argssstotssbnk_sswndfbndfwnmsbmswconstant_features_idxfprobs                       r"   f_onewayrX   *   s   b D		I,,t,,,D(#=#=#=#=#=>>*++I;;d;;;;;J99D999I ^^q033333/%	2B2BBBED$ @ @1#A&)<Q)???"U9%5%555D4<Dq=Dy D
t
C
t
CHSCZ003	z#q#(**/D/I*14II;WWWc	A

1A=tQ''Dd7Nr$   z
array-likezsparse matrix)XyTprefer_skip_nested_validationc                      t           g d          \    fdt          j                  D             }t          | S )a  Compute the ANOVA F-value for the provided sample.

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

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        The set of regressors that will be tested sequentially.

    y : array-like of shape (n_samples,)
        The target vector.

    Returns
    -------
    f_statistic : ndarray of shape (n_features,)
        F-statistic for each feature.

    p_values : ndarray of shape (n_features,)
        P-values associated with the F-statistic.

    See Also
    --------
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    f_regression : F-value between label/feature for regression tasks.

    Examples
    --------
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.feature_selection import f_classif
    >>> X, y = make_classification(
    ...     n_samples=100, n_features=10, n_informative=2, n_clusters_per_class=1,
    ...     shuffle=False, random_state=42
    ... )
    >>> f_statistic, p_values = f_classif(X, y)
    >>> f_statistic
    array([2.2...e+02, 7.0...e-01, 1.6...e+00, 9.3...e-01,
           5.4...e+00, 3.2...e-01, 4.7...e-02, 5.7...e-01,
           7.5...e-01, 8.9...e-02])
    >>> p_values
    array([7.1...e-27, 4.0...e-01, 1.9...e-01, 3.3...e-01,
           2.2...e-02, 5.7...e-01, 8.2...e-01, 4.5...e-01,
           3.8...e-01, 7.6...e-01])
    csrcsccooaccept_sparsec                 D    g | ]}t          |k                       S r'   )r   )r)   rN   rY   rZ   s     r"   r+   zf_classif.<locals>.<listcomp>   s,    :::Ai16""#:::r$   )r   r   uniquerX   )rY   rZ   rD   s   `` r"   	f_classifrf   w   sT    f Q)>)>)>???DAq:::::RYq\\:::DT?r$   c                 <   t          j        | t           j                  } t          |           }| }||z  }|dz  }t          j        d          5  ||z  }ddd           n# 1 swxY w Y   |                    d          }|t          j        |dz
  |          fS )	zFast replacement for scipy.stats.chisquare.

    Version from https://github.com/scipy/scipy/pull/2525 with additional
    optimizations.
    r   r   ignore)invalidNr   r/   r   )r   r4   float64r8   errstater1   r   chdtrc)f_obsf_exprN   chisqs       r"   
_chisquarerq      s     JuBJ///EE

AE	UNE	aKE	X	&	&	&                II1IE'.Q....s   A##A'*A'c                    t          | dt          j        t          j        f          } t          j        t          |           r| j        n| dk               rt          d          t          d          	                    |          }|j
        d         dk    r.|                                }t          j        d|z
  |d          }t          |j        |           }t          |          r|                                }|                     d                              dd	          }|                    d                              dd	          }t          j        |j        |          }t'          ||          S )
a  Compute chi-squared stats between each non-negative feature and class.

    This score can be used to select the `n_features` features with the
    highest values for the test chi-squared statistic from X, which must
    contain only **non-negative integer feature values** such as booleans or frequencies
    (e.g., term counts in document classification), relative to the classes.

    If some of your features are continuous, you need to bin them, for
    example by using :class:`~sklearn.preprocessing.KBinsDiscretizer`.

    Recall that the chi-square test measures dependence between stochastic
    variables, so using this function "weeds out" the features that are the
    most likely to be independent of class and therefore irrelevant for
    classification.

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

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Sample vectors.

    y : array-like of shape (n_samples,)
        Target vector (class labels).

    Returns
    -------
    chi2 : ndarray of shape (n_features,)
        Chi2 statistics for each feature.

    p_values : ndarray of shape (n_features,)
        P-values for each feature.

    See Also
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    f_regression : F-value between label/feature for regression tasks.

    Notes
    -----
    Complexity of this algorithm is O(n_classes * n_features).

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.feature_selection import chi2
    >>> X = np.array([[1, 1, 3],
    ...               [0, 1, 5],
    ...               [5, 4, 1],
    ...               [6, 6, 2],
    ...               [1, 4, 0],
    ...               [0, 0, 0]])
    >>> y = np.array([1, 1, 0, 0, 2, 2])
    >>> chi2_stats, p_values = chi2(X, y)
    >>> chi2_stats
    array([15.3...,  6.5       ,  8.9...])
    >>> p_values
    array([0.0004..., 0.0387..., 0.0116... ])
    r_   rc   r   r   zInput X must be non-negative.T)sparse_outputr   r/   )r   r   rk   float32anyr   data
ValueErrorr   fit_transformr-   toarrayappendr   Tr1   reshapemeandotrq   )rY   rZ   Yobservedfeature_count
class_probexpecteds          r"   chi2r      sH   P 	AU2:rz2JKKKA	v!+qvv!q011 :8999 	T***88;;AwqzQIIKKIa!eQQ'''qsA&&H &
 ##%%EEqEMM))!R00MQ''2..JvjlM22Hh)))r$   boolean)rY   rZ   centerforce_finiter   r   c                   t          | |g dt          j                  \  } }| j        d         }|r|t          j        |          z
  }|                     d          }t          |t          j                  r|                                n|}t          j        t          | j
        d          ||dz  z  z
            }nt          | j
                  }t          ||           }t          j        dd	          5  ||z  }|t          j                            |          z  }d
d
d
           n# 1 swxY w Y   |r?t          j        |                                          st          j        |          }d||<   |S )a  Compute Pearson's r for each features and the target.

    Pearson's r is also known as the Pearson correlation coefficient.

    Linear model for testing the individual effect of each of many regressors.
    This is a scoring function to be used in a feature selection procedure, not
    a free standing feature selection procedure.

    The cross correlation between each regressor and the target is computed
    as::

        E[(X[:, i] - mean(X[:, i])) * (y - mean(y))] / (std(X[:, i]) * std(y))

    For more on usage see the :ref:`User Guide <univariate_feature_selection>`.

    .. versionadded:: 1.0

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        The data matrix.

    y : array-like of shape (n_samples,)
        The target vector.

    center : bool, default=True
        Whether or not to center the data matrix `X` and the target vector `y`.
        By default, `X` and `y` will be centered.

    force_finite : bool, default=True
        Whether or not to force the Pearson's R correlation to be finite.
        In the particular case where some features in `X` or the target `y`
        are constant, the Pearson's R correlation is not defined. When
        `force_finite=False`, a correlation of `np.nan` is returned to
        acknowledge this case. When `force_finite=True`, this value will be
        forced to a minimal correlation of `0.0`.

        .. versionadded:: 1.1

    Returns
    -------
    correlation_coefficient : ndarray of shape (n_features,)
        Pearson's R correlation coefficients of features.

    See Also
    --------
    f_regression: Univariate linear regression tests returning f-statistic
        and p-values.
    mutual_info_regression: Mutual information for a continuous target.
    f_classif: ANOVA F-value between label/feature for classification tasks.
    chi2: Chi-squared stats of non-negative features for classification tasks.

    Examples
    --------
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.feature_selection import r_regression
    >>> X, y = make_regression(
    ...     n_samples=50, n_features=3, n_informative=1, noise=1e-4, random_state=42
    ... )
    >>> r_regression(X, y)
    array([-0.15...,  1.        , -0.22...])
    r^   rs   r   r/   T)squaredr   ri   dividerj   Nr7   )r   r   rk   r-   r   
isinstancematrixgetA1sqrtr   r}   r   rl   linalgnormisfiniteallr    )	rY   rZ   r   r   rG   X_meansX_normscorrelation_coefficientnan_masks	            r"   r_regressionr   %  s   P Q)>)>)>bjQQQDAq
I
  !

N &&a&..%/%C%CP'--///')AC666WaZ9OOPPAC..-a33	Hh	7	7	7 5 57*29>>!#4#445 5 5 5 5 5 5 5 5 5 5 5 5 5 5  0BK(?@@DDFF 0 8344,/)""s   ;(D//D36D3c                    t          | |||          }|j        |rdndz
  }|dz  }t          j        dd          5  |d|z
  z  |z  }t          j                            |d|          }ddd           n# 1 swxY w Y   |ryt          j        |                                          sSt          j	        |          }	t          j
        |j                  j        ||	<   t          j        |          }
d||
<   d||
<   ||fS )	am  Univariate linear regression tests returning F-statistic and p-values.

    Quick linear model for testing the effect of a single regressor,
    sequentially for many regressors.

    This is done in 2 steps:

    1. The cross correlation between each regressor and the target is computed
       using :func:`r_regression` as::

           E[(X[:, i] - mean(X[:, i])) * (y - mean(y))] / (std(X[:, i]) * std(y))

    2. It is converted to an F score and then to a p-value.

    :func:`f_regression` is derived from :func:`r_regression` and will rank
    features in the same order if all the features are positively correlated
    with the target.

    Note however that contrary to :func:`f_regression`, :func:`r_regression`
    values lie in [-1, 1] and can thus be negative. :func:`f_regression` is
    therefore recommended as a feature selection criterion to identify
    potentially predictive feature for a downstream classifier, irrespective of
    the sign of the association with the target variable.

    Furthermore :func:`f_regression` returns p-values while
    :func:`r_regression` does not.

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

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        The data matrix.

    y : array-like of shape (n_samples,)
        The target vector.

    center : bool, default=True
        Whether or not to center the data matrix `X` and the target vector `y`.
        By default, `X` and `y` will be centered.

    force_finite : bool, default=True
        Whether or not to force the F-statistics and associated p-values to
        be finite. There are two cases where the F-statistic is expected to not
        be finite:

        - when the target `y` or some features in `X` are constant. In this
          case, the Pearson's R correlation is not defined leading to obtain
          `np.nan` values in the F-statistic and p-value. When
          `force_finite=True`, the F-statistic is set to `0.0` and the
          associated p-value is set to `1.0`.
        - when a feature in `X` is perfectly correlated (or
          anti-correlated) with the target `y`. In this case, the F-statistic
          is expected to be `np.inf`. When `force_finite=True`, the F-statistic
          is set to `np.finfo(dtype).max` and the associated p-value is set to
          `0.0`.

        .. versionadded:: 1.1

    Returns
    -------
    f_statistic : ndarray of shape (n_features,)
        F-statistic for each feature.

    p_values : ndarray of shape (n_features,)
        P-values associated with the F-statistic.

    See Also
    --------
    r_regression: Pearson's R between label/feature for regression tasks.
    f_classif: ANOVA F-value between label/feature for classification tasks.
    chi2: Chi-squared stats of non-negative features for classification tasks.
    SelectKBest: Select features based on the k highest scores.
    SelectFpr: Select features based on a false positive rate test.
    SelectFdr: Select features based on an estimated false discovery rate.
    SelectFwe: Select features based on family-wise error rate.
    SelectPercentile: Select features based on percentile of the highest
        scores.

    Examples
    --------
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.feature_selection import f_regression
    >>> X, y = make_regression(
    ...     n_samples=50, n_features=3, n_informative=1, noise=1e-4, random_state=42
    ... )
    >>> f_statistic, p_values = f_regression(X, y)
    >>> f_statistic
    array([1.2...+00, 2.6...+13, 2.6...+00])
    >>> p_values
    array([2.7..., 1.5..., 1.0...])
    r   r   r   ri   r   Nr7   g      ?)r   r>   r   rl   r   rV   sfr   r   isinfr   r   maxr    )rY   rZ   r   r   r   deg_of_freedomcorr_coef_squaredf_statisticp_valuesmask_infmask_nans              r"   f_regressionr     s]   L +	1V,   VF1qq2N/2	Hh	7	7	7 > >'1/@+@ANR7::k1n==> > > > > > > > > > > > > > >  	!BK4488:: 	! 8K(( "): ; ; ?H 8K(( #H   s   -A66A:=A:c                   p     e Zd ZU dZdegiZeed<   d Z e	d          dd            Z
d	 Z fd
Z xZS )_BaseFilterzInitialize the univariate feature selection.

    Parameters
    ----------
    score_func : callable
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues) or a single array with scores.
    
score_func_parameter_constraintsc                     || _         d S Nr   )selfr   s     r"   __init__z_BaseFilter.__init__  s    $r$   Tr[   Nc                    |t          | |ddg          }nt          | ||ddgd          \  }}|                     ||           |                     ||          }t          |t          t
          f          r.|\  | _        | _        t          j	        | j                  | _        n|| _        d| _        t          j	        | j                  | _        | S )a  Run score function on (X, y) and get the appropriate features.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The training input samples.

        y : array-like of shape (n_samples,) or None
            The target values (class labels in classification, real numbers in
            regression). If the selector is unsupervised then `y` can be set to `None`.

        Returns
        -------
        self : object
            Returns the instance itself.
        Nr_   r`   rb   T)rc   multi_output)
r   _check_paramsr   r   listtuplescores_pvalues_r   r4   )r   rY   rZ   score_func_rets       r"   fitz_BaseFilter.fit  s    $ 9dAeU^DDDAA a5%.t  DAq 	1a   A..ntUm44 	!*8'DL$-Jt}55DMM)DL DMz$,//r$   c                     d S r   r'   r   rY   rZ   s      r"   r   z_BaseFilter._check_paramsE  s    r$   c                 x    t                                                      }d|j        _        d|j        _        |S )NT)super__sklearn_tags__target_tagsrequired
input_tagssparser   tags	__class__s     r"   r   z_BaseFilter.__sklearn_tags__H  s2    ww''))$(!!%r$   r   )__name__
__module____qualname____doc__callabler   dict__annotations__r   r
   r   r   r   __classcell__r   s   @r"   r   r     s           %18*#=D===% % % \555# # # 65#J          r$   r   c                   ~     e Zd ZU dZi ej        d eeddd          giZee	d<   e
fdd	 fd
Zd Z fdZ xZS )SelectPercentilea	  Select features according to a percentile of the highest scores.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues) or a single array with scores.
        Default is f_classif (see below "See Also"). The default function only
        works with classification tasks.

        .. versionadded:: 0.18

    percentile : int, default=10
        Percent of features to keep.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores, None if `score_func` returned only scores.

    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
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    mutual_info_classif : Mutual information for a discrete target.
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    f_regression : F-value between label/feature for regression tasks.
    mutual_info_regression : Mutual information for a continuous target.
    SelectKBest : Select features based on the k highest scores.
    SelectFpr : Select features based on a false positive rate test.
    SelectFdr : Select features based on an estimated false discovery rate.
    SelectFwe : Select features based on family-wise error rate.
    GenericUnivariateSelect : Univariate feature selector with configurable
        mode.

    Notes
    -----
    Ties between features with equal scores will be broken in an unspecified
    way.

    This filter supports unsupervised feature selection that only requests `X` for
    computing the scores.

    Examples
    --------
    >>> from sklearn.datasets import load_digits
    >>> from sklearn.feature_selection import SelectPercentile, chi2
    >>> X, y = load_digits(return_X_y=True)
    >>> X.shape
    (1797, 64)
    >>> X_new = SelectPercentile(chi2, percentile=10).fit_transform(X, y)
    >>> X_new.shape
    (1797, 7)
    
percentiler   d   bothclosedr   
   )r   c                Z    t                                          |           || _        d S Nr   )r   r   r   )r   r   r   r   s      r"   r   zSelectPercentile.__init__  s)    J///$r$   c                 b   t          |            | j        dk    r-t          j        t	          | j                  t                    S | j        dk    r-t          j        t	          | j                  t                    S t          | j                  }t          j        |d| j        z
            }||k    }t          j	        ||k              d         }t	          |          rKt          t	          |          | j        z  dz            }|d ||                                z
           }d||<   |S )Nr   rh   r   T)r   r   r   onesr8   r   boolzerosr#   r<   intr1   )r   r!   	thresholdmaskties	max_feats	kept_tiess          r"   _get_support_maskz"SelectPercentile._get_support_mask  s    ?c!!73t|,,D9999_!!8C--T::::T\**M&#*?@@		!x)+,,Q/t99 	#CKK$/9C?@@I5y488::556I"DOr$   c                 `    t                                                      }d|j        _        |S NFr   r   r   r   r   s     r"   r   z!SelectPercentile.__sklearn_tags__  (    ww''))$)!r$   )r   r   r   r   r   r   r   r   r   r   rf   r   r   r   r   r   s   @r"   r   r   R  s         D DL$

,$xxaV<<<=$ $D   
 #, %2 % % % % % % %  &        r$   r   c            	            e Zd ZU dZi ej        d edh           eeddd          giZe	e
d<   efd	d
 fdZd Zd Z fdZ xZS )SelectKBesta	  Select features according to the k highest scores.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues) or a single array with scores.
        Default is f_classif (see below "See Also"). The default function only
        works with classification tasks.

        .. versionadded:: 0.18

    k : int or "all", default=10
        Number of top features to select.
        The "all" option bypasses selection, for use in a parameter search.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores, None if `score_func` returned only scores.

    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
    --------
    f_classif: ANOVA F-value between label/feature for classification tasks.
    mutual_info_classif: Mutual information for a discrete target.
    chi2: Chi-squared stats of non-negative features for classification tasks.
    f_regression: F-value between label/feature for regression tasks.
    mutual_info_regression: Mutual information for a continuous target.
    SelectPercentile: Select features based on percentile of the highest
        scores.
    SelectFpr : Select features based on a false positive rate test.
    SelectFdr : Select features based on an estimated false discovery rate.
    SelectFwe : Select features based on family-wise error rate.
    GenericUnivariateSelect : Univariate feature selector with configurable
        mode.

    Notes
    -----
    Ties between features with equal scores will be broken in an unspecified
    way.

    This filter supports unsupervised feature selection that only requests `X` for
    computing the scores.

    Examples
    --------
    >>> from sklearn.datasets import load_digits
    >>> from sklearn.feature_selection import SelectKBest, chi2
    >>> X, y = load_digits(return_X_y=True)
    >>> X.shape
    (1797, 64)
    >>> X_new = SelectKBest(chi2, k=20).fit_transform(X, y)
    >>> X_new.shape
    (1797, 20)
    rN   r   r   Nleftr   r   r   )rN   c                Z    t                                          |           || _        d S r   )r   r   rN   )r   r   rN   r   s      r"   r   zSelectKBest.__init__	  s)    J///r$   c                     t          | j        t                    sC| j        |j        d         k    r/t	          j        d| j         d|j        d          d           d S d S d S )Nr   zk=z is greater than n_features=z$. All the features will be returned.)r   rN   strr-   r?   r@   r   s      r"   r   zSelectKBest._check_params  s    $&#&& 	46AGAJ+>+>M5TV 5 5 5 5 5    	 	+>+>r$   c                    t          |            | j        dk    r%t          j        | j        j        t                    S | j        dk    r%t          j        | j        j        t                    S t          | j                  }t          j        |j        t                    }d|t          j	        |d          | j         d          <   |S )Nr   rh   r   r   	mergesort)kind)
r   rN   r   r   r   r-   r   r   r#   argsort)r   r!   r   s      r"   r   zSelectKBest._get_support_mask  s    6U??74<-T::::Vq[[8DL.d;;;; ..F8FL555D EFDF555tvgii@AKr$   c                 `    t                                                      }d|j        _        |S r   r   r   s     r"   r   zSelectKBest.__sklearn_tags__$  r   r$   )r   r   r   r   r   r   r   r   r   r   r   rf   r   r   r   r   r   r   s   @r"   r   r     s         F FP$

,$jj%!!88Haf#M#M#MN$ $D   
 #, "                    r$   r   c                   t     e Zd ZU dZi ej        d eeddd          giZee	d<   e
fdd	 fd
Zd Z xZS )	SelectFpra  Filter: Select the pvalues below alpha based on a FPR test.

    FPR test stands for False Positive Rate test. It controls the total
    amount of false detections.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues).
        Default is f_classif (see below "See Also"). The default function only
        works with classification tasks.

    alpha : float, default=5e-2
        Features with p-values less than `alpha` are selected.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores.

    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
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    mutual_info_classif: Mutual information for a discrete target.
    f_regression : F-value between label/feature for regression tasks.
    mutual_info_regression : Mutual information for a continuous target.
    SelectPercentile : Select features based on percentile of the highest
        scores.
    SelectKBest : Select features based on the k highest scores.
    SelectFdr : Select features based on an estimated false discovery rate.
    SelectFwe : Select features based on family-wise error rate.
    GenericUnivariateSelect : Univariate feature selector with configurable
        mode.

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.feature_selection import SelectFpr, chi2
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> X.shape
    (569, 30)
    >>> X_new = SelectFpr(chi2, alpha=0.01).fit_transform(X, y)
    >>> X_new.shape
    (569, 16)
    alphar   r   r   r   r   皙?r   c                Z    t                                          |           || _        d S r   r   r   r   r   r   r   r   s      r"   r   zSelectFpr.__init__p  )    J///


r$   c                 @    t          |            | j        | j        k     S r   )r   r   r   r   s    r"   r   zSelectFpr._get_support_maskt  s    }tz))r$   r   r   r   r   r   r   r   r   r   r   rf   r   r   r   r   s   @r"   r   r   *  s         > >@$

,$((4Af5556$ $D   
 #, d       * * * * * * *r$   r   c                   t     e Zd ZU dZi ej        d eeddd          giZee	d<   e
fdd	 fd
Zd Z xZS )	SelectFdra6	  Filter: Select the p-values for an estimated false discovery rate.

    This uses the Benjamini-Hochberg procedure. ``alpha`` is an upper bound
    on the expected false discovery rate.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues).
        Default is f_classif (see below "See Also"). The default function only
        works with classification tasks.

    alpha : float, default=5e-2
        The highest uncorrected p-value for features to keep.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores.

    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
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    mutual_info_classif : Mutual information for a discrete target.
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    f_regression : F-value between label/feature for regression tasks.
    mutual_info_regression : Mutual information for a continuous target.
    SelectPercentile : Select features based on percentile of the highest
        scores.
    SelectKBest : Select features based on the k highest scores.
    SelectFpr : Select features based on a false positive rate test.
    SelectFwe : Select features based on family-wise error rate.
    GenericUnivariateSelect : Univariate feature selector with configurable
        mode.

    References
    ----------
    https://en.wikipedia.org/wiki/False_discovery_rate

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.feature_selection import SelectFdr, chi2
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> X.shape
    (569, 30)
    >>> X_new = SelectFdr(chi2, alpha=0.01).fit_transform(X, y)
    >>> X_new.shape
    (569, 16)
    r   r   r   r   r   r   r   r   c                Z    t                                          |           || _        d S r   r   r   s      r"   r   zSelectFdr.__init__  r   r$   c                 ~   t          |            t          | j                  }t          j        | j                  }||t          | j                  |z  t          j        d|dz             z  k             }|j        dk    r t          j	        | j        t                    S | j        |                                k    S )Nr   r   rh   )r   r8   r   r   sortr:   r   aranger>   
zeros_liker   r   )r   
n_featuressvselecteds       r"   r   zSelectFdr._get_support_mask  s    ''
WT]##%
##j029Q
Q3O3OOO
 =A=d;;;;}..r$   r   r   s   @r"   r   r   z  s         B BH$

,$((4Af5556$ $D   
 #, d       
/ 
/ 
/ 
/ 
/ 
/ 
/r$   r   c                   t     e Zd ZU dZi ej        d eeddd          giZee	d<   e
fdd	 fd
Zd Z xZS )	SelectFwea  Filter: Select the p-values corresponding to Family-wise error rate.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues).
        Default is f_classif (see below "See Also"). The default function only
        works with classification tasks.

    alpha : float, default=5e-2
        The highest uncorrected p-value for features to keep.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores.

    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
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    f_regression : F-value between label/feature for regression tasks.
    SelectPercentile : Select features based on percentile of the highest
        scores.
    SelectKBest : Select features based on the k highest scores.
    SelectFpr : Select features based on a false positive rate test.
    SelectFdr : Select features based on an estimated false discovery rate.
    GenericUnivariateSelect : Univariate feature selector with configurable
        mode.

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.feature_selection import SelectFwe, chi2
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> X.shape
    (569, 30)
    >>> X_new = SelectFwe(chi2, alpha=0.01).fit_transform(X, y)
    >>> X_new.shape
    (569, 15)
    r   r   r   r   r   r   r   r   c                Z    t                                          |           || _        d S r   r   r   s      r"   r   zSelectFwe.__init__  r   r$   c                 j    t          |            | j        | j        t          | j                  z  k     S r   )r   r   r   r8   r   s    r"   r   zSelectFwe._get_support_mask  s.    }tzC,>,>>>>r$   r   r   s   @r"   r	  r	    s         9 9v$

,$((4Af5556$ $D   
 #, d       ? ? ? ? ? ? ?r$   r	  c                       e Zd ZU dZeeeeedZ	e
ed<   i ej         e ee	                                                    g eeddd           edh          gd	Ze
ed
<   efddd	 fdZd Z fdZd Zd Z xZS )GenericUnivariateSelecta	  Univariate feature selector with configurable strategy.

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

    Parameters
    ----------
    score_func : callable, default=f_classif
        Function taking two arrays X and y, and returning a pair of arrays
        (scores, pvalues). For modes 'percentile' or 'kbest' it can return
        a single array scores.

    mode : {'percentile', 'k_best', 'fpr', 'fdr', 'fwe'}, default='percentile'
        Feature selection mode. Note that the `'percentile'` and `'kbest'`
        modes are supporting unsupervised feature selection (when `y` is `None`).

    param : "all", float or int, default=1e-5
        Parameter of the corresponding mode.

    Attributes
    ----------
    scores_ : array-like of shape (n_features,)
        Scores of features.

    pvalues_ : array-like of shape (n_features,)
        p-values of feature scores, None if `score_func` returned scores only.

    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
    --------
    f_classif : ANOVA F-value between label/feature for classification tasks.
    mutual_info_classif : Mutual information for a discrete target.
    chi2 : Chi-squared stats of non-negative features for classification tasks.
    f_regression : F-value between label/feature for regression tasks.
    mutual_info_regression : Mutual information for a continuous target.
    SelectPercentile : Select features based on percentile of the highest
        scores.
    SelectKBest : Select features based on the k highest scores.
    SelectFpr : Select features based on a false positive rate test.
    SelectFdr : Select features based on an estimated false discovery rate.
    SelectFwe : Select features based on family-wise error rate.

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.feature_selection import GenericUnivariateSelect, chi2
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> X.shape
    (569, 30)
    >>> transformer = GenericUnivariateSelect(chi2, mode='k_best', param=20)
    >>> X_new = transformer.fit_transform(X, y)
    >>> X_new.shape
    (569, 20)
    )r   k_bestfprfdrfwe_selection_modesr   Nr   r   r   )modeparamr   r   gh㈵>c                h    t                                          |           || _        || _        d S r   )r   r   r  r  )r   r   r  r  r   s       r"   r   z GenericUnivariateSelect.__init__v  s0    J///	


r$   c                      | j         | j                 | j                  }|                                }|                    d            |j        di |d         | j        i |S )Nr   r   r   r'   )r  r  r   _get_param_namesremove
set_paramsr  )r   selectorpossible_paramss      r"   _make_selectorz&GenericUnivariateSelect._make_selector{  sq    34(3tOOO #3355|,,,??q14:>???r$   c                 d    t                                                      }ddg|j        _        |S )Nrk   rv   )r   r   transformer_tagspreserves_dtyper   s     r"   r   z(GenericUnivariateSelect.__sklearn_tags__  s-    ww''))1:I0F-r$   c                 V    |                                                      ||           d S r   )r  r   r   s      r"   r   z%GenericUnivariateSelect._check_params  s*    ++Aq11111r$   c                     t          |            |                                 }| j        |_        | j        |_        |                                S r   )r   r  r   r   r   )r   r  s     r"   r   z)GenericUnivariateSelect._get_support_mask  sG    &&(( M<))+++r$   )r   r   r   r   r   r   r   r   r	  r  r   r   r   r   r   setkeysr   r   rf   r   r  r   r   r   r   r   s   @r"   r  r  '  sS        > >B ' d   $

,$CC 0 5 5 7 78899:(4D888**eW:M:MN$ $ $D    #, \       
	 	 	    
2 2 2, , , , , , ,r$   r  )1r   r?   numbersr   r   numpyr   scipyr   r   scipy.sparser   baser	   r
   preprocessingr   utilsr   r   r   r   r   utils._param_validationr   r   r   utils.extmathr   r   utils.validationr   r   _baser   r#   rX   rf   rq   r   r   r   r   r   r   r   r   r	  r  r'   r$   r"   <module>r/     s&   $ $  " " " " " " " "                     ! ! ! ! ! ! . . . . . . . . * * * * * * O O O O O O O O O O O O O O K K K K K K K K K K 6 6 6 6 6 6 6 6 = = = = = = = =            	 	 	&J J JZ O,^  #'  . . .b/ / /& O,^  #'  Y* Y* Y*x O,^+"	  #'   "&D ]# ]# ]# ]# ]#@ O,^+"	  #'   "&D r! r! r! r! r!r< < < < <- < < <Df f f f f{ f f fRl l l l l+ l l l^M* M* M* M* M* M* M* M*`X/ X/ X/ X/ X/ X/ X/ X/vH? H? H? H? H? H? H? H?dm, m, m, m, m,k m, m, m, m, m,r$   