
    0Ph$                         d Z ddlZddlmZmZ 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mZ dd	lmZ dd
lmZ ddlmZmZmZmZ  G d dee          ZddZdS )zGeneric feature selection mixin    N)ABCMetaabstractmethod)
attrgetter)
csc_matrixissparse   )TransformerMixin)_safe_indexingcheck_arraysafe_sqr)_get_output_config)get_tags)_check_feature_names_in_is_pandas_dfcheck_is_fittedvalidate_datac                   J    e Zd ZdZd
dZed             Zd Zd Zd Z	dd	Z
dS )SelectorMixina  
    Transformer mixin that performs feature selection given a support mask

    This mixin provides a feature selector implementation with `transform` and
    `inverse_transform` functionality given an implementation of
    `_get_support_mask`.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.datasets import load_iris
    >>> from sklearn.base import BaseEstimator
    >>> from sklearn.feature_selection import SelectorMixin
    >>> class FeatureSelector(SelectorMixin, BaseEstimator):
    ...    def fit(self, X, y=None):
    ...        self.n_features_in_ = X.shape[1]
    ...        return self
    ...    def _get_support_mask(self):
    ...        mask = np.zeros(self.n_features_in_, dtype=bool)
    ...        mask[:2] = True  # select the first two features
    ...        return mask
    >>> X, y = load_iris(return_X_y=True)
    >>> FeatureSelector().fit_transform(X, y).shape
    (150, 2)
    Fc                 f    |                                  }|s|nt          j        |          d         S )a  
        Get a mask, or integer index, of the features selected.

        Parameters
        ----------
        indices : bool, default=False
            If True, the return value will be an array of integers, rather
            than a boolean mask.

        Returns
        -------
        support : array
            An index that selects the retained features from a feature vector.
            If `indices` is False, this is a boolean array of shape
            [# input features], in which an element is True iff its
            corresponding feature is selected for retention. If `indices` is
            True, this is an integer array of shape [# output features] whose
            values are indices into the input feature vector.
        r   )_get_support_masknpwhere)selfindicesmasks      _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/feature_selection/_base.pyget_supportzSelectorMixin.get_support4   s1    ( %%''"9ttq(99    c                     dS )a  
        Get the boolean mask indicating which features are selected

        Returns
        -------
        support : boolean array of shape [# input features]
            An element is True iff its corresponding feature is selected for
            retention.
        N )r   s    r   r   zSelectorMixin._get_support_maskK   s      r   c           	          t          d|           d         }|dk    ot          |          }t          | |ddt          |           j        j         |d          }|                     |          S )	aB  Reduce X to the selected features.

        Parameters
        ----------
        X : array of shape [n_samples, n_features]
            The input samples.

        Returns
        -------
        X_r : array of shape [n_samples, n_selected_features]
            The input samples with only the selected features.
        	transform)	estimatordensedefaultNcsrF)dtypeaccept_sparseensure_all_finiteskip_check_arrayreset)r   r   r   r   
input_tags	allow_nan
_transform)r   Xoutput_config_dense
preserve_Xs       r   r"   zSelectorMixin.transformW   s     1MMMgV(I5J-:J:J
 "*4..";"EE'
 
 
 q!!!r   c                 f   |                                  }|                                sxt          j        dt                     t          |d          r|j        ddddf         S t          j        d|j	                  
                    |j        d         df          S t          ||d          S )z"Reduce X to the selected features.zYNo features were selected: either the data is too noisy or the selection test too strict.ilocNr   r'      axis)r   anywarningswarnUserWarninghasattrr3   r   emptyr'   reshapeshaper
   )r   r/   r   s      r   r.   zSelectorMixin._transformv   s    !!xxzz 
	GMC    q&!! %vaaa!e}$8AQW---55qwqz1oFFFaA....r   c                    t          |          r|                                }|                     t          j        |j                                      dd                    }|                                }t          j        dgt          j	        |          g          }t          |j        |j        |f|j        d         t          |          dz
  f|j                  }|S |                                 }t#          |d          }|                                |j        d         k    rt'          d          |j        dk    r|dddf         }t          j        |j        d         |j        f|j                  }||dd|f<   |S )a  Reverse the transformation operation.

        Parameters
        ----------
        X : array of shape [n_samples, n_selected_features]
            The input samples.

        Returns
        -------
        X_r : array of shape [n_samples, n_original_features]
            `X` with columns of zeros inserted where features would have
            been removed by :meth:`transform`.
        r5   r   )r?   r'   Nr4   z,X has a different shape than during fitting.)r   tocscinverse_transformr   diffindptrr>   ravelconcatenatecumsumr   datar   r?   lenr'   r   r   sum
ValueErrorndimzerossize)r   r/   itcol_nonzerosrE   Xtsupports          r   rC   zSelectorMixin.inverse_transform   sc    A;; 			A ''(9(9(A(A!R(H(HIIB88::L^aS")L*A*A$BCCFF+wqz3v;;?3g  B
 I""$$&&&;;==AGAJ&&KLLL6Q;;$'
AXqwqz7<0@@@111g:	r   Nc                 t    t          |            t          | |          }||                                          S )a  Mask feature names according to selected features.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

            - If `input_features` is `None`, then `feature_names_in_` is
              used as feature names in. If `feature_names_in_` is not defined,
              then the following input feature names are generated:
              `["x0", "x1", ..., "x(n_features_in_ - 1)"]`.
            - If `input_features` is an array-like, then `input_features` must
              match `feature_names_in_` if `feature_names_in_` is defined.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        )r   r   r   )r   input_featuress     r   get_feature_names_outz#SelectorMixin.get_feature_names_out   s8    ( 	0~FFd..0011r   )F)N)__name__
__module____qualname____doc__r   r   r   r"   r.   rC   rV   r    r   r   r   r      s         4: : : :. 	 	 ^	" " ">/ / / & & &P2 2 2 2 2 2r   r   )	metaclassr5   c                    t          |t                    rs|dk    r]t          | d          rt          d          }nkt          | d          rt          d          }nKt	          d| j        j         d          t          |          }nt          |          st	          d           ||           }||S |dk    rC|j        d	k    rt          j
        |          }nwt          j                            |d
|          }nT|dk    r?|j        d	k    rt          |          }n3t          |                              d
          }nt	          d          |S )a  
    Retrieve and aggregate (ndim > 1)  the feature importances
    from an estimator. Also optionally applies transformation.

    Parameters
    ----------
    estimator : estimator
        A scikit-learn estimator from which we want to get the feature
        importances.

    getter : "auto", str or callable
        An attribute or a callable to get the feature importance. If `"auto"`,
        `estimator` is expected to expose `coef_` or `feature_importances`.

    transform_func : {"norm", "square"}, default=None
        The transform to apply to the feature importances. By default (`None`)
        no transformation is applied.

    norm_order : int, default=1
        The norm order to apply when `transform_func="norm"`. Only applied
        when `importances.ndim > 1`.

    Returns
    -------
    importances : ndarray of shape (n_features,)
        The features importances, optionally transformed.
    autocoef_feature_importances_z;when `importance_getter=='auto'`, the underlying estimator z should have `coef_` or `feature_importances_` attribute. Either pass a fitted estimator to feature selector or call fit before calling transform.z4`importance_getter` has to be a string or `callable`Nnormr5   r   )r7   ordsquarer6   zpValid values for `transform_func` are None, 'norm' and 'square'. Those two transformation are only supported now)
isinstancestrr<   r   rL   	__class__rW   callablerM   r   abslinalgr`   r   rK   )r#   gettertransform_func
norm_orderimportancess        r   _get_feature_importancesrm      s   8 &# QVy'** #G,,$:;; 	#$:;; 0!*!4!=0 0 0    ''FFf QOPPP&##K	6	!	!q  &--KK)..1*.MMKK	8	#	#q  ";//KK";//333;;KK6
 
 	
 r   )Nr5   )rZ   r9   abcr   r   operatorr   numpyr   scipy.sparser   r   baser	   utilsr
   r   r   utils._set_outputr   utils._tagsr   utils.validationr   r   r   r   r   rm   r    r   r   <module>rw      sT   % %
  ' ' ' ' ' ' ' '           - - - - - - - - # # # # # # 9 9 9 9 9 9 9 9 9 9 2 2 2 2 2 2 " " " " " "           k2 k2 k2 k2 k2$ k2 k2 k2 k2\D D D D D Dr   