§
    ¨éUg=-  ã                   óÄ   — d Z ddlmZmZ ddlZddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ  G d„ deeee¦  «        ZdS )z
Sequential feature selection
é    )ÚIntegralÚRealNé   )ÚBaseEstimatorÚMetaEstimatorMixinÚ_fit_contextÚcloneÚis_classifier)Úget_scorer_names)Úcheck_cvÚcross_val_score)Ú
HasMethodsÚIntervalÚ
RealNotIntÚ
StrOptions)Ú
_safe_tags)Ú_RoutingNotSupportedMixin)Úcheck_is_fittedé   )ÚSelectorMixinc                   ó`  — e Zd ZU dZ edg¦  «        g edh¦  «         eeddd¬¦  «         eeddd	¬¦  «        gd ee	ddd	¬¦  «        g ed
dh¦  «        gd e e
 e¦   «         ¦  «        ¦  «        egdgdegdœZeed<   ddd
ddddœd„Z ed¬¦  «        dd„¦   «         Zd„ Zd„ Zd„ ZdS )ÚSequentialFeatureSelectora®  Transformer that performs Sequential Feature Selection.

    This Sequential Feature Selector adds (forward selection) or
    removes (backward selection) features to form a feature subset in a
    greedy fashion. At each stage, this estimator chooses the best feature to
    add or remove based on the cross-validation score of an estimator. In
    the case of unsupervised learning, this Sequential Feature Selector
    looks only at the features (X), not the desired outputs (y).

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

    .. versionadded:: 0.24

    Parameters
    ----------
    estimator : estimator instance
        An unfitted estimator.

    n_features_to_select : "auto", int or float, default="auto"
        If `"auto"`, the behaviour depends on the `tol` parameter:

        - if `tol` is not `None`, then features are selected while the score
          change does not exceed `tol`.
        - otherwise, half of the features are selected.

        If integer, the parameter is the absolute number of features to select.
        If float between 0 and 1, it is the fraction of features to select.

        .. versionadded:: 1.1
           The option `"auto"` was added in version 1.1.

        .. versionchanged:: 1.3
           The default changed from `"warn"` to `"auto"` in 1.3.

    tol : float, default=None
        If the score is not incremented by at least `tol` between two
        consecutive feature additions or removals, stop adding or removing.

        `tol` can be negative when removing features using `direction="backward"`.
        `tol` is required to be strictly positive when doing forward selection.
        It can be useful to reduce the number of features at the cost of a small
        decrease in the score.

        `tol` is enabled only when `n_features_to_select` is `"auto"`.

        .. versionadded:: 1.1

    direction : {'forward', 'backward'}, default='forward'
        Whether to perform forward selection or backward selection.

    scoring : str or callable, default=None
        A single str (see :ref:`scoring_parameter`) or a callable
        (see :ref:`scoring`) to evaluate the predictions on the test set.

        NOTE that when using a custom scorer, it should return a single
        value.

        If None, the estimator's score method is used.

    cv : int, cross-validation generator or an iterable, default=None
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:

        - None, to use the default 5-fold cross validation,
        - integer, to specify the number of folds in a `(Stratified)KFold`,
        - :term:`CV splitter`,
        - An iterable yielding (train, test) splits as arrays of indices.

        For integer/None inputs, if the estimator is a classifier and ``y`` is
        either binary or multiclass,
        :class:`~sklearn.model_selection.StratifiedKFold` is used. In all other
        cases, :class:`~sklearn.model_selection.KFold` is used. These splitters
        are instantiated with `shuffle=False` so the splits will be the same
        across calls.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validation strategies that can be used here.

    n_jobs : int, default=None
        Number of jobs to run in parallel. When evaluating a new feature to
        add or remove, the cross-validation procedure is parallel over the
        folds.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    Attributes
    ----------
    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying estimator exposes such an attribute when 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

    n_features_to_select_ : int
        The number of features that were selected.

    support_ : ndarray of shape (n_features,), dtype=bool
        The mask of selected features.

    See Also
    --------
    GenericUnivariateSelect : Univariate feature selector with configurable
        strategy.
    RFE : Recursive feature elimination based on importance weights.
    RFECV : Recursive feature elimination based on importance weights, with
        automatic selection of the number of features.
    SelectFromModel : Feature selection based on thresholds of importance
        weights.

    Examples
    --------
    >>> from sklearn.feature_selection import SequentialFeatureSelector
    >>> from sklearn.neighbors import KNeighborsClassifier
    >>> from sklearn.datasets import load_iris
    >>> X, y = load_iris(return_X_y=True)
    >>> knn = KNeighborsClassifier(n_neighbors=3)
    >>> sfs = SequentialFeatureSelector(knn, n_features_to_select=3)
    >>> sfs.fit(X, y)
    SequentialFeatureSelector(estimator=KNeighborsClassifier(n_neighbors=3),
                              n_features_to_select=3)
    >>> sfs.get_support()
    array([ True, False,  True,  True])
    >>> sfs.transform(X).shape
    (150, 3)
    ÚfitÚautor   r   Úright)ÚclosedNÚneitherÚforwardÚbackwardÚ	cv_object©Ú	estimatorÚn_features_to_selectÚtolÚ	directionÚscoringÚcvÚn_jobsÚ_parameter_constraintsé   )r#   r$   r%   r&   r'   r(   c                óh   — || _         || _        || _        || _        || _        || _        || _        d S ©Nr!   )Úselfr"   r#   r$   r%   r&   r'   r(   s           úd/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/feature_selection/_sequential.pyÚ__init__z"SequentialFeatureSelector.__init__©   s:   € ð #ˆŒØ$8ˆÔ!ØˆŒØ"ˆŒØˆŒØˆŒØˆŒˆˆó    F)Úprefer_skip_nested_validationc           	      ón  — |                       ¦   «         }|                      |dd|                     dd¦  «         ¬¦  «        }|j        d         }| j        dk    r| j        �|dz
  | _        n‚|dz  | _        nwt          | j        t          ¦  «        r'| j        |k    rt          d	¦  «        ‚| j        | _        n6t          | j        t          ¦  «        rt          || j        z  ¦  «        | _        | j        �%| j        d
k     r| j        dk    rt          d¦  «        ‚t          | j        |t          | j        ¦  «        ¬¦  «        }t#          | j        ¦  «        }t%          j        |t(          ¬¦  «        }| j        dk    s| j        dk    r| j        n	|| j        z
  }t$          j         }	| j        duo
| j        dk    }
t-          |¦  «        D ]7}|                      |||||¦  «        \  }}|
r||	z
  | j        k     r n|}	d||<   Œ8| j        dk    r| }|| _        | j                             ¦   «         | _        | S )a  Learn the features to select from X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training vectors, where `n_samples` is the number of samples and
            `n_features` is the number of predictors.

        y : array-like of shape (n_samples,), default=None
            Target values. This parameter may be ignored for
            unsupervised learning.

        Returns
        -------
        self : object
            Returns the instance itself.
        Úcscr   Ú	allow_nanT)Úaccept_sparseÚensure_min_featuresÚforce_all_finiter   r   Nz*n_features_to_select must be < n_features.r   r   z:tol must be strictly positive when doing forward selection)Ú
classifier)ÚshapeÚdtyper   )Ú	_get_tagsÚ_validate_dataÚgetr9   r#   r$   Ún_features_to_select_Ú
isinstancer   Ú
ValueErrorr   Úintr%   r   r'   r
   r"   r	   ÚnpÚzerosÚboolÚinfÚrangeÚ_get_best_new_feature_scoreÚsupport_Úsum)r-   ÚXÚyÚtagsÚ
n_featuresr'   Úcloned_estimatorÚcurrent_maskÚn_iterationsÚ	old_scoreÚis_auto_selectÚ_Únew_feature_idxÚ	new_scores                 r.   r   zSequentialFeatureSelector.fit¼   sv  € ð, �~Š~ÑÔˆØ×ÒØØØ !Ø!%§¢¨+°tÑ!<Ô!<Ð<ð	  ñ 
ô 
ˆð ”W˜Q”Zˆ
àÔ$¨Ò.Ð.ØŒxÐ#ð .8¸!©^�Ô*Ð*à-7¸1©_�Ô*Ð*Ý˜Ô1µ8Ñ<Ô<ð 	UØÔ(¨JÒ6Ð6Ý Ð!MÑNÔNÐNØ)-Ô)BˆDÔ&Ð&Ý˜Ô1µ4Ñ8Ô8ð 	UÝ),¨Z¸$Ô:SÑ-SÑ)TÔ)TˆDÔ&àŒ8Ð D¤H¨q¢L L°T´^ÀyÒ5PÐ5PÝØLñô ð õ �d”g˜q­]¸4¼>Ñ-JÔ-JÐKÑKÔKˆå  ¤Ñ0Ô0Ðõ
 ”x j½Ð=Ñ=Ô=ˆð Ô(¨FÒ2Ð2°d´nÈ	Ò6QÐ6Qð Ô&Ð&à˜dÔ8Ñ8ð 	õ ”V�Gˆ	Øœ¨Ð-ÐU°$Ô2KÈvÒ2UˆÝ�|Ñ$Ô$ð 	1ð 	1ˆAØ)-×)IÒ)IØ  ! Q¨¨Lñ*ô *Ñ&ˆO˜Yð ð  I°	Ñ$9¸T¼XÒ#EÐ#EØ�à!ˆIØ,0ˆL˜Ñ)Ð)àŒ>˜ZÒ'Ð'Ø(˜=ˆLà$ˆŒØ%)¤]×%6Ò%6Ñ%8Ô%8ˆÔ"àˆr0   c           	      óF  ‡— t          j        | ¦  «        }i Š|D ]i}|                     ¦   «         }d||<   | j        dk    r| }|d d …|f         }	t	          ||	||| j        | j        ¬¦  «                             ¦   «         ‰|<   Œjt          ‰ˆfd„¬¦  «        }
|
‰|
         fS )NTr   )r'   r&   r(   c                 ó   •— ‰|          S r,   © )Úfeature_idxÚscoress    €r.   ú<lambda>zGSequentialFeatureSelector._get_best_new_feature_score.<locals>.<lambda>&  s   ø€ ¸fÀ[Ô>Q€ r0   ©Úkey)	rB   ÚflatnonzeroÚcopyr%   r   r&   r(   ÚmeanÚmax)r-   r"   rJ   rK   r'   rO   Úcandidate_feature_indicesrY   Úcandidate_maskÚX_newrT   rZ   s              @r.   rG   z5SequentialFeatureSelector._get_best_new_feature_score  sÛ   ø€ õ %'¤N°L°=Ñ$AÔ$AÐ!ØˆØ4ð 	ð 	ˆKØ)×.Ò.Ñ0Ô0ˆNØ*.ˆN˜;Ñ'ØŒ~ Ò+Ð+Ø"0 �Ø�a�a�a˜Ð'Ô(ˆEÝ"1ØØØØØœØ”{ð#ñ #ô #÷ Šd‰fŒfð �;ÑÐõ ˜fÐ*QÐ*QÐ*QÐ*QÐRÑRÔRˆØ  Ô 7Ð7Ð7r0   c                 ó.   — t          | ¦  «         | j        S r,   )r   rH   ©r-   s    r.   Ú_get_support_maskz+SequentialFeatureSelector._get_support_mask)  s   € Ý˜ÑÔÐØŒ}Ðr0   c                 ó2   — dt          | j        d¬¦  «        iS )Nr4   r\   )r   r"   rf   s    r.   Ú
_more_tagsz$SequentialFeatureSelector._more_tags-  s    € à� D¤N¸ÐDÑDÔDð
ð 	
r0   r,   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   Úsetr   Úcallabler)   ÚdictÚ__annotations__r/   r   r   rG   rg   ri   rX   r0   r.   r   r      s’  € € € € € € ðCð CðL !�j % Ñ)Ô)Ð*àˆJ˜�xÑ Ô ØˆH�Z  A¨gÐ6Ñ6Ô6ØˆH�X˜q $¨yÐ9Ñ9Ô9ð!
ð
 �h�h˜t T¨4¸	ÐBÑBÔBÐCØ �j )¨ZÐ!8Ñ9Ô9Ð:Ø˜*˜* S SÐ)9Ð)9Ñ);Ô);Ñ%<Ô%<Ñ=Ô=¸xÐHØˆmØ˜Ð"ð$ð $Ð˜Dð ð ñ ð$ $ØØØØØðð ð ð ð ð& €\à&+ðñ ô ðNð Nð Nñ	ô ðNð`8ð 8ð 8ð2ð ð ð
ð 
ð 
ð 
ð 
r0   r   )rm   Únumbersr   r   ÚnumpyrB   Úbaser   r   r   r	   r
   Úmetricsr   Úmodel_selectionr   r   Úutils._param_validationr   r   r   r   Úutils._tagsr   Úutils.metadata_routingr   Úutils.validationr   Ú_baser   r   rX   r0   r.   ú<module>r|      s;  ððð ð #Ð "Ð "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð à XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ &Ð &Ð &Ð &Ð &Ð &Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RØ $Ð $Ð $Ð $Ð $Ð $Ø >Ð >Ð >Ð >Ð >Ð >Ø .Ð .Ð .Ð .Ð .Ð .Ø  Ð  Ð  Ð  Ð  Ð  ð]
ð ]
ð ]
ð ]
ð ]
Ø˜}Ð.@À-ñ]
ô ]
ð ]
ð ]
ð ]
r0   