`L i$dZddlZddlmZmZddlmZddlZddl m Z m Z ddl m Z ddlmZmZmZdd lmZdd lmZdd lmZmZmZmZGd d e eZddZy)zGeneric feature selection mixinN)ABCMetaabstractmethod) attrgetter) csc_matrixissparse)TransformerMixin)_safe_indexing check_arraysafe_sqr)_get_output_config)get_tags)_check_feature_names_in _is_pandas_dfcheck_is_fitted validate_datacBeZdZdZd dZedZdZdZdZ d dZ y) 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) cZ|j}|s|Stj|dS)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_masknpnonzero)selfindicesmasks e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/feature_selection/_base.py get_supportzSelectorMixin.get_support4s-(%%'"t; 4(8(;;cy)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)rs rrzSelectorMixin._get_support_maskKsrc td|d}|dk7xr t|}t||ddt|jj |d}|j |S) aBReduce 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) estimatordensedefaultNcsrF)dtype accept_sparseensure_all_finiteskip_check_arrayreset)r rrr input_tags allow_nan _transform)rXoutput_config_dense preserve_Xs rr"zSelectorMixin.transformWso1MgV(I5J-:J    "*4.";";"E"EE' q!!rcR|j}|jsztjdtt |dr|j ddddfStjd|jj|jddfSt||dS)z"Reduce X to the selected features.zYNo features were selected: either the data is too noisy or the selection test too strict.ilocNrr'axis) ranywarningswarn UserWarninghasattrr3remptyr'reshapeshaper )rr/rs rr.zSelectorMixin._transformvs!xxz MMC  q&!vva!e}$88AQWW-55qwwqz1oF FaA..rc t|r|j}|jtj|j j dd}|j}tjdgtj|g}t|j|j|f|jdt|dz f|j}|S|j!}t#|d}|j%|jdk7r t'd|j(dk(r |dddf}tj*|jd|j,f|j}||dd|f<|S)aReverse the transformation operation. Parameters ---------- X : array of shape [n_samples, n_selected_features] The input samples. Returns ------- X_original : array of shape [n_samples, n_original_features] `X` with columns of zeros inserted where features would have been removed by :meth:`transform`. r5r)r?r'Nr4z,X has a different shape than during fitting.)rtocscinverse_transformrdiffindptrr>ravel concatenatecumsumrdatarr?lenr'rr sum ValueErrorndimzerossize)rr/it col_nonzerosrEXtsupports rrCzSelectorMixin.inverse_transforms8 A; A''(9(A(A!R(HIB88:L^^aS"))L*A$BCFF+wwqz3v;?3ggB I""$  & ;;=AGGAJ &KL L 66Q;$' A XXqwwqz7<<0 @1g: rNcVt|t||}||jS)aMask 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. )rrr)rinput_featuress rget_feature_names_outz#SelectorMixin.get_feature_names_outs,( 0~Fd..011r)F)N) __name__ __module__ __qualname____doc__rrrr"r.rCrVr rrrrs54<.    ">/ &P2rr) metaclassc^t|trd|dk(rSt|dr td}n]t|dr td}nEt d|j j dt|}nt|s t d||}||S|dk(rJ|jdk(rtj|}|Stjj|d | }|S|d k(r9|jdk(r t|}|St|jd }|St d )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. autocoef_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`normr5r)r7ordsquarer6zpValid values for `transform_func` are None, 'norm' and 'square'. Those two transformation are only supported now) isinstancestrr<rrL __class__rWcallablerMrabslinalgr`r rK)r#gettertransform_func norm_order importancess r_get_feature_importancesrmsR8&# V y'*#G,$:;#$:; !!*!4!4!=!= >?00 'F f OPP#K 6 !   q &&-K ))..1*.MK  8 #   q ";/K #;/333;K   4  r)Nr5)rZr9abcrroperatorrnumpyr scipy.sparserrbaser utilsr r r utils._set_outputr utils._tagsrutils.validationrrrrrrmr rrrwsJ% '-#992"k2$k2\Dr