o
    tBh"<                     @   s   d dl mZ d dlZd dlZddl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mZmZ dd
lmZ ddlmZ dd Zdd ZG dd deee	ZdS )    )deepcopyN   )SelectorMixin)_get_feature_importances   )BaseEstimatorcloneMetaEstimatorMixin)
_safe_tags)check_is_fittedcheck_scalar_num_features)NotFittedError)available_ifc                 C   s   |du r| j j}t| dr| jdksd|v rd}nd}t|trqd|v rU|d\}}t| }| }|dkr?t	
|}n|dkrIt	|}ntd	| || }|S |dkr`t	
|}|S |dkrkt	|}|S td
| t|}|S )zInterpret the threshold valueNpenaltyl1Lassogh㈵>mean*medianzUnknown reference: z6Expected threshold='mean' or threshold='median' got %s)	__class____name__hasattrr   
isinstancestrsplitfloatstripnpr   r   
ValueError)	estimatorimportances	thresholdest_namescale	reference r&   |/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/feature_selection/_from_model.py_calculate_threshold   s>   




r(   c                    s    fddS )zCheck if we can delegate a method to the underlying estimator.

    First, we check the fitted estimator if available, otherwise we
    check the unfitted estimator.
    c                    s"   t | drt | j S t | j S )N
estimator_)r   r)   r    selfattrr&   r'   <lambda>G   s   z _estimator_has.<locals>.<lambda>r&   r,   r&   r,   r'   _estimator_hasA   s   r/   c                   @   sv   e Zd ZdZddddddddZd	d
 Zdd ZdddZedd Z	e
eddddZedd Zdd ZdS )SelectFromModela  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
        importance 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_feaures(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.3252302 ,  0.83462377,  0.49750423]])
    >>> selector.threshold_
    0.55245...
    >>> 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
    NFr   auto)r"   prefit
norm_ordermax_featuresimportance_getterc                C   s(   || _ || _|| _|| _|| _|| _d S N)r    r"   r2   r5   r3   r4   )r+   r    r"   r2   r3   r4   r5   r&   r&   r'   __init__   s   

zSelectFromModel.__init__c              
   C   s  t | d| j}t | d| j}| jr*zt| j W n ty) } ztd|d }~ww t|r2td|d urDt|tj	sDt
d| dt|| jd| jd}t||| j}| jd urrtj|td	}tj| d
dd | }d||< ntj|td	}d|||k < |S )Nr)   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r    gettertransform_funcr3   )dtype	mergesort)kindTF)getattrr    r4   r2   r   r   callabler   numbersIntegralr   r   r5   r3   r(   r"   r   
zeros_likeboolargsort	ones_like)r+   r    r4   excscoresr"   maskcandidate_indicesr&   r&   r'   _get_support_mask   sJ   



z!SelectFromModel._get_support_maskc                 C   s   | j d urCt|}t| j tjr!t| j dtjd|d | j | _d S t| j r:|  |}t|dtjd|d || _d S td| j  dd S )Nr4   r   )min_valmax_valzmax_features(X)zP'max_features' must be either an int or a callable that takes 'X' as input. Got z	 instead.)	r4   r   r   rC   rD   r   r8   rB   	TypeError)r+   X
n_featuresr4   r&   r&   r'   _check_max_features!  s6   



z#SelectFromModel._check_max_featuresc              
   K   s   |  | | jr(zt| j W n ty  } ztd|d}~ww t| j| _nt| j| _| jj||fi | t	| jdrF| jj
| _
| S | j|dd | S )a  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
            Other estimator specific parameters.

        Returns
        -------
        self : object
            Fitted estimator.
        r9   Nfeature_names_in_Treset)rS   r2   r   r    r   r   r)   r   fitr   rT   _check_feature_names)r+   rQ   y
fit_paramsrI   r&   r&   r'   rW   >  s(   

zSelectFromModel.fitc                 C   s&   t | j| jd| jd}t| j|| jS )z+Threshold value used for feature selection.r:   r;   )r   r)   r5   r3   r(   r    r"   )r+   rJ   r&   r&   r'   
threshold_h  s   zSelectFromModel.threshold_partial_fitc              
   K   s   |  | | jr.t| ds,zt| j W n ty% } ztd|d}~ww t| j| _| S t| d }|r<t| j| _| jj	||fi | t| jdrT| jj
| _
| S | j||d | S )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).

        **fit_params : dict
            Other estimator specific parameters.

        Returns
        -------
        self : object
            Fitted estimator.
        r)   r9   NrT   rU   )rS   r2   r   r   r    r   r   r)   r   r\   rT   rX   )r+   rQ   rY   rZ   rI   
first_callr&   r&   r'   r\   s  s0   


zSelectFromModel.partial_fitc              
   C   s@   z	t |  W | jjS  ty } z
td| jj|d}~ww )z%Number of features seen during `fit`.z*{} object has no n_features_in_ attribute.N)r   r   AttributeErrorformatr   r   r)   n_features_in_)r+   nfer&   r&   r'   r`     s   
zSelectFromModel.n_features_in_c                 C   s   dt | jddiS )N	allow_nan)key)r
   r    r*   r&   r&   r'   
_more_tags  s   zSelectFromModel._more_tagsr6   )r   
__module____qualname____doc__r7   rM   rS   rW   propertyr[   r   r/   r\   r`   rd   r&   r&   r&   r'   r0   N   s&     ,
*


.
r0   )copyr   numpyr   rC   _baser   r   baser   r   r	   utils._tagsr
   utils.validationr   r   r   
exceptionsr   utils.metaestimatorsr   r(   r/   r0   r&   r&   r&   r'   <module>   s   .