
    -iH                         S SK Jr  S SKJrJr  S SKrSSKJrJ	r	J
r
Jr  SSKJr  SSKJrJrJr  SSKJr  SS	KJrJrJrJr  SS
KJr  SSKJrJrJrJrJ r   SSK!J"r"J#r#  S r$ " S S\	\"\5      r%g)    )deepcopy)IntegralRealN   )BaseEstimatorMetaEstimatorMixin_fit_contextclone)NotFittedError)
HasMethodsIntervalOptions)get_tags)MetadataRouterMethodMapping_routing_enabledprocess_routing)available_if)_check_feature_names_estimator_has_num_featurescheck_is_fittedcheck_scalar   )SelectorMixin_get_feature_importancesc                    Uc  U R                   R                  n[        U S5      =(       a    U R                  S:H  nSU;   nUS:H  =(       a4    [        U S5      =(       a!    [        R
                  " U R                  S5      nUS:H  =(       a4    [        U S5      =(       a!    [        R
                  " U R                  S5      nU(       d  U(       d  U(       d  U(       a  S	nOS
n[        U[        5      (       a  SU;   a  UR                  S5      u  p[        UR                  5       5      nU	R                  5       n	U	S:X  a  [        R                  " U5      n	O+U	S
:X  a  [        R                  " U5      n	O[        SU	-   5      eX-  nU$ US:X  a  [        R                  " U5      nU$ US
:X  a  [        R                  " U5      nU$ [        SU-  5      e[        U5      nU$ )zInterpret the threshold valuepenaltyl1Lasso
ElasticNetl1_ratiog      ?ElasticNetCV	l1_ratio_gh㈵>mean*medianzUnknown reference: z6Expected threshold='mean' or threshold='median' got %s)	__class____name__hasattrr   npiscloser"   r$   
isinstancestrsplitfloatstripr'   r%   
ValueError)
	estimatorimportances	thresholdest_nameis_l1_penalizedis_lassois_elasticnet_l1_penalizedis_elasticnetcv_l1_penalizedscale	references
             X/var/www/html/venv/lib/python3.13/site-packages/sklearn/feature_selection/_from_model.py_calculate_thresholdr>      s    &&//!)Y7UI<M<MQU<Uh&%-%= &
Iz*Rrzz):L:Lc/R 	# (0>'A (
I{+T

9;N;NPS0T 	% )+ II)S!!)(s3E%++-(E!)IH$IIk2	f$GGK0	 !6!BCC)I   ("		+.I  & ,I  H9T 
 )$	    c                     ^  \ rS rSr% Sr\" S5      /\" \SSSS9\S/S/\" \	SSS	S9\" \	S
SSS9\
" \\R                  \R                  * 15      /\" \	SSSS9\S/\\/S.r\\S'   SSS
SSS.S jrS rS r\" SS9SS j5       r\S 5       r\" \" S5      5      \" SS9SS j5       5       r\S 5       rS rU 4S jrSrU =r$ )SelectFromModelW   a  Meta-transformer for selecting features based on importance weights.

.. versionadded:: 0.17

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

Parameters
----------
estimator : object
    The base estimator from which the transformer is built.
    This can be both a fitted (if ``prefit`` is set to True)
    or a non-fitted estimator. The estimator should have a
    ``feature_importances_`` or ``coef_`` attribute after fitting.
    Otherwise, the ``importance_getter`` parameter should be used.

threshold : str or float, default=None
    The threshold value to use for feature selection. Features whose
    absolute importance value is greater or equal are kept while the others
    are discarded. If "median" (resp. "mean"), then the ``threshold`` value
    is the median (resp. the mean) of the feature importances. A scaling
    factor (e.g., "1.25*mean") may also be used. If None and if the
    estimator has a parameter penalty set to l1, either explicitly
    or implicitly (e.g, Lasso), the threshold used is 1e-5.
    Otherwise, "mean" is used by default.

prefit : bool, default=False
    Whether a prefit model is expected to be passed into the constructor
    directly or not.
    If `True`, `estimator` must be a fitted estimator.
    If `False`, `estimator` is fitted and updated by calling
    `fit` and `partial_fit`, respectively.

norm_order : non-zero int, inf, -inf, default=1
    Order of the norm used to filter the vectors of coefficients below
    ``threshold`` in the case where the ``coef_`` attribute of the
    estimator is of dimension 2.

max_features : int, callable, default=None
    The maximum number of features to select.

    - If an integer, then it specifies the maximum number of features to
      allow.
    - If a callable, then it specifies how to calculate the maximum number of
      features allowed by using the output of `max_features(X)`.
    - If `None`, then all features are kept.

    To only select based on ``max_features``, set ``threshold=-np.inf``.

    .. versionadded:: 0.20
    .. versionchanged:: 1.1
       `max_features` accepts a callable.

importance_getter : str or callable, default='auto'
    If 'auto', uses the feature importance either through a ``coef_``
    attribute or ``feature_importances_`` attribute of estimator.

    Also accepts a string that specifies an attribute name/path
    for extracting feature importance (implemented with `attrgetter`).
    For example, give `regressor_.coef_` in case of
    :class:`~sklearn.compose.TransformedTargetRegressor`  or
    `named_steps.clf.feature_importances_` in case of
    :class:`~sklearn.pipeline.Pipeline` with its last step named `clf`.

    If `callable`, overrides the default feature importance getter.
    The callable is passed with the fitted estimator and it should
    return importance for each feature.

    .. versionadded:: 0.24

Attributes
----------
estimator_ : estimator
    The base estimator from which the transformer is built. This attribute
    exist only when `fit` has been called.

    - If `prefit=True`, it is a deep copy of `estimator`.
    - If `prefit=False`, it is a clone of `estimator` and fit on the data
      passed to `fit` or `partial_fit`.

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

max_features_ : int
    Maximum number of features calculated during :term:`fit`. Only defined
    if the ``max_features`` is not `None`.

    - If `max_features` is an `int`, then `max_features_ = max_features`.
    - If `max_features` is a callable, then `max_features_ = max_features(X)`.

    .. versionadded:: 1.1

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

threshold_ : float
    The threshold value used for feature selection.

See Also
--------
RFE : Recursive feature elimination based on importance weights.
RFECV : Recursive feature elimination with built-in cross-validated
    selection of the best number of features.
SequentialFeatureSelector : Sequential cross-validation based feature
    selection. Does not rely on importance weights.

Notes
-----
Allows NaN/Inf in the input if the underlying estimator does as well.

Examples
--------
>>> from sklearn.feature_selection import SelectFromModel
>>> from sklearn.linear_model import LogisticRegression
>>> X = [[ 0.87, -1.34,  0.31 ],
...      [-2.79, -0.02, -0.85 ],
...      [-1.34, -0.48, -2.55 ],
...      [ 1.92,  1.48,  0.65 ]]
>>> y = [0, 1, 0, 1]
>>> selector = SelectFromModel(estimator=LogisticRegression()).fit(X, y)
>>> selector.estimator_.coef_
array([[-0.3252,  0.8345,  0.4976]])
>>> selector.threshold_
np.float64(0.55249)
>>> selector.get_support()
array([False,  True, False])
>>> selector.transform(X)
array([[-1.34],
       [-0.02],
       [-0.48],
       [ 1.48]])

Using a callable to create a selector that can use no more than half
of the input features.

>>> def half_callable(X):
...     return round(len(X[0]) / 2)
>>> half_selector = SelectFromModel(estimator=LogisticRegression(),
...                                 max_features=half_callable)
>>> _ = half_selector.fit(X, y)
>>> half_selector.max_features_
2
fitNboth)closedbooleanrightr   leftr   )r3   r5   prefit
norm_ordermax_featuresimportance_getter_parameter_constraintsFauto)r5   rJ   rK   rL   rM   c                L    Xl         X l        X0l        X`l        X@l        XPl        g N)r3   r5   rJ   rM   rK   rL   )selfr3   r5   rJ   rK   rL   rM   s          r=   __init__SelectFromModel.__init__   s$     #"!2$(r?   c                    [        U SU R                  5      n[        U SU R                  5      nU R                  (       a   [	        U R                  5        [        U5      (       a  [        S5      eUb$  [        U[        5      (       d  [        SU S35      e[        UU R                  SU R                  S9n[        XU R                  5      nU R                  b7  [        R                   " U["        S	9n[        R$                  " U* S
S9S U nSXg'   O[        R&                  " U["        S	9nSXdU:  '   U$ ! [
         a  n[        S5      UeS nAff = f)N
estimator_max_features_EWhen `prefit=True`, `estimator` is expected to be a fitted estimator.z[When `prefit=True` and `max_features` is a callable, call `fit` before calling `transform`.z5`max_features` must be an integer. Got `max_features=z
` instead.normr3   gettertransform_funcrK   )dtype	mergesort)kindTF)getattrr3   rL   rJ   r   r   callabler-   r   r2   r   rM   rK   r>   r5   r+   
zeros_likeboolargsort	ones_like)rR   r3   rL   excscoresr5   maskcandidate_indicess           r=   _get_support_mask!SelectFromModel._get_support_mask  sR   D,?	t_d6G6GH;;/ L!! !.  %jx.P.PG~ V  
 *))!	
 )DNNK	(==t4D "

F7 Em| T&*D#<<d3D#(i C " $! s   D4 4
E>E

Ec                     U R                   b[  [        U5      n[        U R                   5      (       a  U R                  U5      nOU R                   n[        US[        SUS9  X0l        g g )NrL   r   )min_valmax_val)rL   r   ra   r   r   rW   )rR   X
n_featuresrL   s       r=   _check_max_features#SelectFromModel._check_max_features5  sh    (&q)J))**#003#00" ". )r?   )prefer_skip_nested_validationc                    U R                  U5        U R                  (       a1   [        U R                  5        [        U R                  5      U l        O[        5       (       aY  [        U S40 UD6n[        U R                  5      U l        U R                  R                  " X40 UR                  R                  D6  O7[        U R                  5      U l        U R                  R                  " X40 UD6  [        U R                  S5      (       a  U R                  R                  U l        U $ [        XSS9  U $ ! [         a  n[	        S5      UeSnAff = f)a5  Fit the SelectFromModel meta-transformer.

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

y : array-like of shape (n_samples,), default=None
    The target values (integers that correspond to classes in
    classification, real numbers in regression).

**fit_params : dict
    - If `enable_metadata_routing=False` (default): Parameters directly passed
      to the `fit` method of the sub-estimator. They are ignored if
      `prefit=True`.

    - If `enable_metadata_routing=True`: Parameters safely routed to the `fit`
      method of the sub-estimator. They are ignored if `prefit=True`.

    .. versionchanged:: 1.4
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
self : object
    Fitted estimator.
rX   NrC   feature_names_in_Treset)rq   rJ   r   r3   r   r   rV   r   r   r
   rC   r*   ru   r   )rR   ro   y
fit_paramsrf   routed_paramss         r=   rC   SelectFromModel.fitG  s   B 	  #;;/ 't~~6DO!! /e Jz J"'"7##AHM,C,C,G,GH #("7##A7J74??$788%)__%F%FD"  !5+ " $! s   D7 7
EEEc                     [        U R                  U R                  SU R                  S9n[	        U R
                  XR                  5      $ )z+Threshold value used for feature selection.rY   rZ   )r   rV   rM   rK   r>   r3   r5   )rR   rg   s     r=   
threshold_SelectFromModel.threshold_  s?     *oo))!	
 $DNNFNNKKr?   partial_fitc                    [        U S5      (       + nU(       a  U R                  U5        U R                  (       a9  U(       a0   [        U R                  5        [        U R                  5      U l        U $ U(       a  [        U R                  5      U l        [        5       (       aY  [        U S40 UD6n[        U R                  5      U l        U R                  R                  " X40 UR                  R                  D6  OU R                  R                  " X40 UD6  [        U R                  S5      (       a  U R                  R                  U l        U $ [        XUS9  U $ ! [
         a  n[        S5      UeSnAff = f)a  Fit the SelectFromModel meta-transformer only once.

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

y : array-like of shape (n_samples,), default=None
    The target values (integers that correspond to classes in
    classification, real numbers in regression).

**partial_fit_params : dict
    - If `enable_metadata_routing=False` (default): Parameters directly passed
      to the `partial_fit` method of the sub-estimator.

    - If `enable_metadata_routing=True`: Parameters passed to the `partial_fit`
      method of the sub-estimator. They are ignored if `prefit=True`.

    .. versionchanged:: 1.4

        `**partial_fit_params` are routed to the sub-estimator, if
        `enable_metadata_routing=True` is set via
        :func:`~sklearn.set_config`, which allows for aliasing.

        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
self : object
    Fitted estimator.
rV   rX   Nr   ru   rv   )r*   rq   rJ   r   r3   r   r   rV   r
   r   r   r   ru   r   )rR   ro   rx   partial_fit_params
first_callrf   rz   s          r=   r   SelectFromModel.partial_fit  s7   L !|44
$$Q';;#DNN3 #+4>>":K#DNN3DO+D-VCUVM#DNN3DOOO''T0G0G0S0ST OO''C0BC4??$788%)__%F%FD"  !
;/ & (% s   E 
E9(E44E9c                      [        U 5        U R                  R                  $ ! [         a4  n[        SR                  U R                  R
                  5      5      UeSnAff = f)z%Number of features seen during `fit`.z*{} object has no n_features_in_ attribute.N)r   r   AttributeErrorformatr(   r)   rV   n_features_in_)rR   nfes     r=   r   SelectFromModel.n_features_in_  s`    
	D! ---  	 <CCNN++ 		s   # 
A!/AA!c                     [        U R                  R                  S9R                  U R                  [        5       R                  SSS9R                  SSS9S9nU$ )a"  Get metadata routing of this object.

Please check :ref:`User Guide <metadata_routing>` on how the routing
mechanism works.

.. versionadded:: 1.4

Returns
-------
routing : MetadataRouter
    A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
    routing information.
)ownerr   )callercalleerC   )r3   method_mapping)r   r(   r)   addr3   r   )rR   routers     r=   get_metadata_routing$SelectFromModel.get_metadata_routing  sX      dnn&=&=>BBnn(?SmS<SeS,	 C 
 r?   c                   > [         TU ]  5       n[        U R                  5      R                  R
                  UR                  l        [        U R                  5      R                  R                  UR                  l        U$ rQ   )super__sklearn_tags__r   r3   
input_tagssparse	allow_nan)rR   tagsr(   s     r=   r    SelectFromModel.__sklearn_tags__  sW    w')!)$..!9!D!D!K!K$,T^^$<$G$G$Q$Q!r?   )	r3   rV   ru   rM   rL   rW   rK   rJ   r5   rQ   ) r)   
__module____qualname____firstlineno____doc__r   r   r   r.   r   r   r+   infra   rN   dict__annotations__rS   rj   rq   r	   rC   propertyr}   r   r   r   r   r   r   __static_attributes____classcell__)r(   s   @r=   rA   rA   W   s@   Sl !'(tT4?dK+XtR8Xq$v6D266BFF7+,

 "(AtFCXtT!8_$D "  )"(T.$ &+7	7r L L ./0&+A	 1
AF . ., r?   rA   )&copyr   numbersr   r   numpyr+   baser   r   r	   r
   
exceptionsr   utils._param_validationr   r   r   utils._tagsr   utils.metadata_routingr   r   r   r   utils.metaestimatorsr   utils.validationr   r   r   r   r   _baser   r   r>   rA    r?   r=   <module>r      sY     "  I I ' C C "  0  ;6rj(- jr?   