
    ה9il)                         d Z ddlmZmZ ddlZddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ 	 ddZddZ G d de
e	e      Zd Z G d de
ee      Zy)z)Base class for ensemble-based estimators.    )ABCMetaabstractmethodN)effective_n_jobs)BaseEstimatorMetaEstimatorMixincloneis_classifieris_regressor)Bunchcheck_random_state)get_tags)_print_elapsed_time)_routing_enabled)_BaseCompositionc                    t               s3d|v r/	 t        ||      5  | j                  |||d          ddd       | S t        ||      5   | j                  ||fi | ddd       | S # 1 sw Y   6xY w# t        $ rB}dt	        |      v r/t        dj                  | j                  j                              | d}~ww xY w# 1 sw Y   | S xY w)z7Private function used to fit an estimator within a job.sample_weight)r   Nz+unexpected keyword argument 'sample_weight'z8Underlying estimator {} does not support sample weights.)r   r   fit	TypeErrorstrformat	__class____name__)	estimatorXy
fit_paramsmessage_clsnamemessageexcs          `/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/sklearn/ensemble/_base.py_fit_single_estimatorr!      s     /Z"?
	$_g> Oa*_2MNO  !': 	.IMM!Q-*-	.O O 	<CHNUU!++44 	
 		.s9   A5 A)A5 
C)A2.A5 5	C >=B;;C Cc                 4   t        |      }i }t        | j                  d            D ]X  }|dk(  s|j                  d      s|j	                  t        j                  t
        j                        j                        ||<   Z |r | j                  di | yy)a  Set fixed random_state parameters for an estimator.

    Finds all parameters ending ``random_state`` and sets them to integers
    derived from ``random_state``.

    Parameters
    ----------
    estimator : estimator supporting get/set_params
        Estimator with potential randomness managed by random_state
        parameters.

    random_state : int, RandomState instance or None, default=None
        Pseudo-random number generator to control the generation of the random
        integers. Pass an int for reproducible output across multiple function
        calls.
        See :term:`Glossary <random_state>`.

    Notes
    -----
    This does not necessarily set *all* ``random_state`` attributes that
    control an estimator's randomness, only those accessible through
    ``estimator.get_params()``.  ``random_state``s not controlled include
    those belonging to:

        * cross-validation splitters
        * ``scipy.stats`` rvs
    Tdeeprandom_state__random_stateN )
r   sorted
get_paramsendswithrandintnpiinfoint32max
set_params)r   r%   to_setkeys       r    _set_random_statesr3   1   s    8 &l3LFi***56 G. CLL1A$B&..rxx/A/E/EFF3KG 	&v&     c                   X    e Zd ZdZe	 dd e       dd       ZddZddZd Z	d	 Z
d
 Zy)BaseEnsemblea  Base class for all ensemble classes.

    Warning: This class should not be used directly. Use derived classes
    instead.

    Parameters
    ----------
    estimator : object
        The base estimator from which the ensemble is built.

    n_estimators : int, default=10
        The number of estimators in the ensemble.

    estimator_params : list of str, default=tuple()
        The list of attributes to use as parameters when instantiating a
        new base estimator. If none are given, default parameters are used.

    Attributes
    ----------
    estimator_ : estimator
        The base estimator from which the ensemble is grown.

    estimators_ : list of estimators
        The collection of fitted base estimators.
    N
   )n_estimatorsestimator_paramsc                .    || _         || _        || _        y N)r   r8   r9   )selfr   r8   r9   s       r    __init__zBaseEnsemble.__init__r   s     #( 0r4   c                 N    | j                   | j                   | _        y|| _        y)zMCheck the base estimator.

        Sets the `estimator_` attributes.
        N)r   
estimator_)r<   defaults     r    _validate_estimatorz BaseEnsemble._validate_estimator   s     
 >>%"nnDO%DOr4   c                     t        | j                        } |j                  di | j                  D ci c]  }|t	        | |       c} |t        ||       |r| j                  j                  |       |S c c}w )zMake and configure a copy of the `estimator_` attribute.

        Warning: This method should be used to properly instantiate new
        sub-estimators.
        r'   )r   r?   r0   r9   getattrr3   estimators_append)r<   rE   r%   r   ps        r    _make_estimatorzBaseEnsemble._make_estimator   su     $//*		TT=R=RS74#3 3ST#y,7##I.  Ts   A8c                 ,    t        | j                        S )z0Return the number of estimators in the ensemble.)lenrD   r<   s    r    __len__zBaseEnsemble.__len__   s    4##$$r4   c                      | j                   |   S )z.Return the index'th estimator in the ensemble.)rD   )r<   indexs     r    __getitem__zBaseEnsemble.__getitem__   s    &&r4   c                 ,    t        | j                        S )z0Return iterator over estimators in the ensemble.)iterrD   rJ   s    r    __iter__zBaseEnsemble.__iter__   s    D$$%%r4   r;   )TN)r   
__module____qualname____doc__r   tupler=   rA   rG   rK   rN   rQ   r'   r4   r    r6   r6   W   sH    4  
1 
1 
1 &"%'&r4   r6   )	metaclassc                     t        t        |      |       }t        j                  || |z  t              }|d| |z  xxx dz  ccc t        j
                  |      }||j                         dg|j                         z   fS )z;Private function used to partition estimators between jobs.)dtypeN   r   )minr   r,   fullintcumsumtolist)r8   n_jobsn_estimators_per_jobstartss       r    _partition_estimatorsrb      s{     !&)<8F 776<6+AM0<&01Q61YY+,F'..01#2GGGr4   c                   ^     e Zd ZdZed        Zed        Zd Z fdZ	d fd	Z
 fdZ xZS )	_BaseHeterogeneousEnsemblea  Base class for heterogeneous ensemble of learners.

    Parameters
    ----------
    estimators : list of (str, estimator) tuples
        The ensemble of estimators to use in the ensemble. Each element of the
        list is defined as a tuple of string (i.e. name of the estimator) and
        an estimator instance. An estimator can be set to `'drop'` using
        `set_params`.

    Attributes
    ----------
    estimators_ : list of estimators
        The elements of the estimators parameter, having been fitted on the
        training data. If an estimator has been set to `'drop'`, it will not
        appear in `estimators_`.
    c                 >    t        di t        | j                        S )zDictionary to access any fitted sub-estimators by name.

        Returns
        -------
        :class:`~sklearn.utils.Bunch`
        r'   )r   dict
estimatorsrJ   s    r    named_estimatorsz+_BaseHeterogeneousEnsemble.named_estimators   s     -tDOO,--r4   c                     || _         y r;   rg   )r<   rg   s     r    r=   z#_BaseHeterogeneousEnsemble.__init__   s	    $r4   c           	         t        | j                        dk(  st        d | j                  D              st        d      t	        | j                   \  }}| j                  |       t        d |D              }|st        d      t        |       rt        nt        }|D ]L  }|dk7  s	 ||      rt        dj                  |j                  j                  |j                  dd               ||fS )	Nr   c              3   r   K   | ]/  }t        |t        t        f      xr t        |d    t               1 yw)r   N)
isinstancerU   listr   ).0items     r    	<genexpr>zB_BaseHeterogeneousEnsemble._validate_estimators.<locals>.<genexpr>   s6      0
 teT]+H
47C0HH0
s   57zfInvalid 'estimators' attribute, 'estimators' should be a non-empty list of (string, estimator) tuples.c              3   &   K   | ]	  }|d k7    yw)dropNr'   ro   ests     r    rq   zB_BaseHeterogeneousEnsemble._validate_estimators.<locals>.<genexpr>   s     @cC6M@s   zHAll estimators are dropped. At least one is required to be an estimator.rs   z The estimator {} should be a {}.   )rI   rg   all
ValueErrorzip_validate_namesanyr	   r
   r   r   r   )r<   namesrg   has_estimatoris_estimator_typeru   s         r    _validate_estimatorsz/_BaseHeterogeneousEnsemble._validate_estimators   s    t1$C 0
0
 -
 @   1zU#@Z@@& 
 .;4-@Ml 	Cf}%6s%; 6==..0A0J0J120N 	 j  r4   c                 &    t        |   di | | S )a  
        Set the parameters of an estimator from the ensemble.

        Valid parameter keys can be listed with `get_params()`. Note that you
        can directly set the parameters of the estimators contained in
        `estimators`.

        Parameters
        ----------
        **params : keyword arguments
            Specific parameters using e.g.
            `set_params(parameter_name=new_value)`. In addition, to setting the
            parameters of the estimator, the individual estimator of the
            estimators can also be set, or can be removed by setting them to
            'drop'.

        Returns
        -------
        self : object
            Estimator instance.
        rj   )super_set_params)r<   paramsr   s     r    r0   z%_BaseHeterogeneousEnsemble.set_params   s    , 	3F3r4   c                 &    t         |   d|      S )a<  
        Get the parameters of an estimator from the ensemble.

        Returns the parameters given in the constructor as well as the
        estimators contained within the `estimators` parameter.

        Parameters
        ----------
        deep : bool, default=True
            Setting it to True gets the various estimators and the parameters
            of the estimators as well.

        Returns
        -------
        params : dict
            Parameter and estimator names mapped to their values or parameter
            names mapped to their values.
        rg   r#   )r   _get_params)r<   r$   r   s     r    r)   z%_BaseHeterogeneousEnsemble.get_params  s    & w"<d";;r4   c                     t         |          }	 t        d | j                  D              |j                  _        t        d | j                  D              |j                  _        |S # t        $ r Y |S w xY w)Nc              3   t   K   | ]0  }|d    dk7  r"t        |d          j                  j                  nd 2 ywrY   rs   TN)r   
input_tags	allow_nanrt   s     r    rq   z>_BaseHeterogeneousEnsemble.__sklearn_tags__.<locals>.<genexpr>,  s=      , :=Q69IQ ++55tS,   68c              3   t   K   | ]0  }|d    dk7  r"t        |d          j                  j                  nd 2 ywr   )r   r   sparsert   s     r    rq   z>_BaseHeterogeneousEnsemble.__sklearn_tags__.<locals>.<genexpr>0  s=      ) 7:!f6FQ ++22DP)r   )r   __sklearn_tags__rw   rg   r   r   r   	Exception)r<   tagsr   s     r    r   z+_BaseHeterogeneousEnsemble.__sklearn_tags__)  s}    w')	(+ ,??, )DOO% &) )??) &DOO"   	 	s   AA) )	A65A6)T)r   rR   rS   rT   propertyrh   r   r=   r   r0   r)   r   __classcell__)r   s   @r    rd   rd      sJ    $ . . % %!@2<* r4   rd   )NNr;   )rT   abcr   r   numpyr,   joblibr   sklearn.baser   r   r   r	   r
   sklearn.utilsr   r   sklearn.utils._tagsr   sklearn.utils._user_interfacer   sklearn.utils.metadata_routingr   sklearn.utils.metaestimatorsr   r!   r3   r6   rb   rd   r'   r4   r    <module>r      sn    /
 (  #  4 ( = ; 9 @D0#'LQ&%} Q&h
HA(GAr4   