
    ^iST              ,          d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlZddlmZmZmZ ddlmZmZmZmZ ddlmZmZ ddlmZmZm Z m!Z!m"Z"m#Z#m$Z$ erdd	l%m&Z' ed
   Z(dZ)e#	 d7dddddddddd	dee*ef   dede+de
eeee*f         de
e!   de
e,   de
e+   de
ejZ                     de
ee,e+f      de
ee*ej\                  ee/f      de
ee      de
e    defd       Z0 G d d
      Z1 G d d      Z2dejf                  d ejf                  dejf                  fd!Z4d"ddd#d$ed%e+d&edee*   d'e
e   d(e,de	e1   fd)Z5d"dd*ddd+d$ed%e+d&ed,e+dee*   d'e
e   d-e
e,   d.e
e   d(e,de	e1   fd/Z6e#	 d7d0d*dd"dddddddddddd1dedede+d%e+d-e,d.ed2ee*   de
e!   de
e,   de
e+   d'e
e   d3e,de
ee+e,f      d4e,d,e+de
ee      d(e,de
e    deee*e7f   d5f   f&d6       Z8y)8z.Training Library containing training routines.    N)
TYPE_CHECKINGAnyDictIterableListOptionalSequenceTupleUnioncast   )BoosterParamCallableFPreProcCallable)CallbackContainerEarlyStoppingEvaluationMonitorTrainingCallback)SKLEARN_INSTALLEDXGBStratifiedKFold)BoosterDMatrixMetric	ObjectiveXGBoostError_deprecate_positional_args	_RefMixIn)	DataFrameCVPack)zfTraining dataset should be used as a reference when constructing the `QuantileDMatrix` for evaluation.T)	evalsobjmaximizeearly_stopping_roundsevals_resultverbose_eval	xgb_model	callbackscustom_metricparamsdtrainnum_boost_roundr    r!   r"   r#   r$   r%   r&   r'   r(   returnc       	   	         |
g nt        j                   t        |
            }
|rt        |      ng }|D ]f  \  }}t        |t              st	        d      t        |t
              s2|j                  t        j                  |      usT||usYt        t               t        | |g|D cg c]  }|d   	 c}z   |	      }d}|r#|du rdn|}|
j                  t        |             |r|
j                  t        ||             t        |
|t        |            }|j!                  |      }t#        ||      D ]B  }|j%                  ||||      r n,|j'                  |||	       |j)                  ||||      sB n |j+                  |      }||j'                  |j,                         |j/                         S c c}w )
a  Train a booster with given parameters.

    Parameters
    ----------
    params :
        Booster params.
    dtrain :
        Data to be trained.
    num_boost_round :
        Number of boosting iterations.
    evals :
        List of validation sets for which metrics will evaluated during training.
        Validation metrics will help us track the performance of the model.
    obj
        Custom objective function.  See :doc:`Custom Objective
        </tutorials/custom_metric_obj>` for details.
    maximize :
        Whether to maximize custom_metric.

    early_stopping_rounds :

        Activates early stopping. Validation metric needs to improve at least once in
        every **early_stopping_rounds** round(s) to continue training.

        Requires at least one item in **evals**.

        The method returns the model from the last iteration (not the best one).  Use
        custom callback :py:class:`~xgboost.callback.EarlyStopping` or :py:meth:`model
        slicing <xgboost.Booster.__getitem__>` if the best model is desired.  If there's
        more than one item in **evals**, the last entry will be used for early stopping.

        If there's more than one metric in the **eval_metric** parameter given in
        **params**, the last metric will be used for early stopping.

        If early stopping occurs, the model will have two additional fields:
        ``bst.best_score``, ``bst.best_iteration``.

    evals_result :
        This dictionary stores the evaluation results of all the items in watchlist.

        Example: with a watchlist containing
        ``[(dtest,'eval'), (dtrain,'train')]`` and
        a parameter containing ``('eval_metric': 'logloss')``,
        the **evals_result** returns

        .. code-block:: python

            {'train': {'logloss': ['0.48253', '0.35953']},
             'eval': {'logloss': ['0.480385', '0.357756']}}

    verbose_eval :
        Requires at least one item in **evals**.

        If **verbose_eval** is True then the evaluation metric on the validation set is
        printed at each boosting stage.

        If **verbose_eval** is an integer then the evaluation metric on the validation
        set is printed at every given **verbose_eval** boosting stage. The last boosting
        stage / the boosting stage found by using **early_stopping_rounds** is also
        printed.

        Example: with ``verbose_eval=4`` and at least one item in **evals**, an
        evaluation metric is printed every 4 boosting stages, instead of every boosting
        stage.

    xgb_model :
        Xgb model to be loaded before training (allows training continuation).

    callbacks :
        List of callback functions that are applied at end of each iteration.
        It is possible to use predefined callbacks by using
        :ref:`Callback API <callback_api>`.

        .. note::

           States in callback are not preserved during training, which means callback
           objects can not be reused for multiple training sessions without
           reinitialization or deepcopy.

        .. code-block:: python

            for params in parameters_grid:
                # be sure to (re)initialize the callbacks before each run
                callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
                xgboost.train(params, Xy, callbacks=callbacks)

    custom_metric:

        .. versionadded 1.6.0

        Custom metric function.  See :doc:`Custom Metric </tutorials/custom_metric_obj>`
        for details. The metric receives transformed prediction (after applying the
        reverse link function) when using a builtin objective, and raw output when using
        a custom objective.

    Returns
    -------
    Booster : a trained booster model

    zInvalid type for the `evals`.r   )
model_fileTr   )periodroundsr"   )metricoutput_margin)	iterationfobj)copylist
isinstancer   	TypeErrorr   refweakref
ValueError	_RefErrorr   appendr   r   r   callablebefore_trainingrangebefore_iterationupdateafter_iterationafter_traininghistoryreset)r)   r*   r+   r    r!   r"   r#   r$   r%   r&   r'   r(   va_dbststart_iterationcb_containeris                      Q/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/xgboost/training.pytrainrP   3   s   j  'TYYtI-GI DKbE 	(A"g&;<< r9%gkk&11& Y''	( &6(E%:qad%::y
QCO(D0ql*,?@.ChWX$-x}L 
&
&s
+C?O4 ((a?

6QS
1''Q> 
%
%c
*CL00199;5 &;s   )F<c            	           e Zd ZdZdededeeeef      ddfdZ	de
defd	Zd
edee   ddfdZd
edee   dede
fdZy)r   z. "Auxiliary datastruct to hold one fold of CV.r*   dtestparamr,   Nc                 `    || _         || _        |df|dfg| _        t        |||g      | _        y)zInitialize the CVPack.rP   testN)r*   rR   	watchlistr   rK   )selfr*   rR   rS   s       rO   __init__zCVPack.__init__   s9     
!7+eV_=565/2    namec                 <     dt         dt         dt         f fd}|S )Nargskwargsr,   c                  <     t        j                        | i |S N)getattrrK   )r\   r]   rZ   rW   s     rO   _innerz"CVPack.__getattr__.<locals>._inner   s     *7488T*D;F;;rY   )r   )rW   rZ   ra   s   `` rO   __getattr__zCVPack.__getattr__   s$    	<# 	< 	< 	< rY   r4   r5   c                 R    | j                   j                  | j                  ||       y)z' "Update the boosters for one iterationN)rK   rC   r*   )rW   r4   r5   s      rO   rC   zCVPack.update   s    Y5rY   fevalr3   c                 R    | j                   j                  | j                  |||      S )z( "Evaluate the CVPack for one iteration.)rK   eval_setrV   )rW   r4   rd   r3   s       rO   evalzCVPack.eval   s!    xx  E=QQrY   )__name__
__module____qualname____doc__r   r   r   r   r   rX   strr   rb   intr   rC   r   boolrg    rY   rO   r   r      s    833&-36>uT4Z?P6Q3	3  6 68I+> 64 6Rc R(6*: R4 RTW RrY   c            	       T   e Zd ZdeddfdZdedee   ddfdZdedee	   d	e
dee   fd
Zdee   defdZdedee   fdZ	 ddeeeeeef      ef   dee   ddfdZdefdZedefd       Zej2                  deddfd       Zedefd       Zej2                  deddfd       Zy)_PackedBoostercvfoldsr,   Nc                     || _         y r_   )rr   )rW   rr   s     rO   rX   z_PackedBooster.__init__   s	    rY   r4   r!   c                 J    | j                   D ]  }|j                  ||        y)z Iterate through folds for updateN)rr   rC   )rW   r4   r!   folds       rO   rC   z_PackedBooster.update   s#    LL 	(DKK	3'	(rY   rd   r3   c                 d    | j                   D cg c]  }|j                  |||       }}|S c c}w )zIterate through folds for eval)rr   rg   )rW   r4   rd   r3   fresults         rO   rg   z_PackedBooster.eval   s5     DH<<Pa!&&E=9PP Qs   -r]   c                 ^    | j                   D ]  } |j                  j                  di |   y)z,Iterate through folds for setting attributesNro   )rr   rK   set_attr)rW   r]   rw   s      rO   rz   z_PackedBooster.set_attr   s*     	%AAEENN$V$	%rY   keyc                 R    | j                   d   j                  j                  |      S )zRedirect to booster attr.r   )rr   rK   attr)rW   r{   s     rO   r}   z_PackedBooster.attr  s!    ||A""'',,rY   r)   valuec                 ^    | j                   D ]  }|j                  j                  ||         y)z#Iterate through folds for set_paramN)rr   rK   	set_param)rW   r)   r~   rw   s       rO   r   z_PackedBooster.set_param  s)      	+AEEOOFE*	+rY   c                 <    | j                   d   j                         S )zNumber of boosted rounds.r   )rr   num_boosted_roundsrW   s    rO   r   z!_PackedBooster.num_boosted_rounds  s    ||A1133rY   c                     t        t        t         | j                  d   j                  j	                  d                  S )Get best_iterationr   best_iteration)rm   r   rr   rK   r}   r   s    rO   r   z_PackedBooster.best_iteration  s0     4T\\!_00556FGHIIrY   c                 (    | j                  |       y)r   )r   Nrz   )rW   r4   s     rO   r   z_PackedBooster.best_iteration  s     	Y/rY   c                     t        t        t         | j                  d   j                  j	                  d                  S )zGet best_score.r   
best_score)floatr   rr   rK   r}   r   s    rO   r   z_PackedBooster.best_score  s/     T%a!4!4!9!9,!GHIIrY   scorec                 (    | j                  |       y )N)r   r   )rW   r   s     rO   r   z_PackedBooster.best_score#  s    'rY   r_   )rh   ri   rj   _CVFoldsrX   rm   r   r   rC   r   rn   r   rl   rg   r   rz   r}   r   r   r   r
   r   r   propertyr   setterr   r   ro   rY   rO   rq   rq      sk    T ( ((9*= ($ (
%-f%5FJ	c%# %3 %
- - -  $+dHU38_5s:;+ }+ 
	+4C 4 J J J 0 0 0 0 JE J J ( ($ ( (rY   rq   groups
boundariesc                     t        j                  | D cg c]!  }t        j                  ||   ||dz            # c}      S c c}w )z
    Given group row boundaries, convert ground indexes to row indexes
    :param groups: list of groups for testing
    :param boundaries: rows index limits of each group
    :return: row in group
    r   )npconcatenatearange)r   r   gs      rO   groups_to_rowsr   (  s:     >>PVW1299Z]Jq1u4EFWXXWs   &Aro   )r    fpreprocshuffledallnfoldrS   r   r   c                    | j                  d      }t        j                  |      }|du r)t        j                  j	                  t        |            }nt        j                  t        |            }t        j                  ||      }	t        |      D 
cg c]7  }
t        j                  t        |      D cg c]  }|
|k7  s	|	|    c}      9 }}
}|D cg c]  }t        ||       }}|	D cg c]  }t        ||       }}g }t        |      D ]  }
| j                  ||
   d      }|j                  |||
             | j                  ||
   d      }|j                  ||	|
             | ||||j                               \  }}}n|}t        |j                               |D cg c]  }d|f c}z   }|j!                  t#        |||              |S c c}w c c}}
w c c}w c c}w c c}w )zb
    Make n folds for cross-validation maintaining groups
    :return: cross-validation folds
    	group_ptrT)allow_groupseval_metric)get_uint_infor   diffrandompermutationlenr   array_splitrA   r   r   slice	set_groupr6   r7   itemsr>   r   )r   r   rS   r    r   r   group_boundariesgroup_sizesidxout_group_idsetkrN   in_group_idset	in_groupsin_idset
out_groups	out_idsetretr*   rR   tparamitmplsts                          rO   mkgroupfoldr   2  s    ))+6''*+K$ii##C$45iiK()nnS%0O u 	E%LKqAF*KLN  FT8Ay"23H  HW9Cz#34I 
 C5\ 0HQKd;^A%678

9Q<d
;OA$678$,VUEJJL$I!FE6FFLLN#u&Ms';&MM

6&%./0 J5 L
" 'Ns0   !G#7
GG		G#G)2G..G3G#F)r    r   
stratifiedfoldsr   seedr   r   c        	            t        |      }t        j                  j                  |       |du r|t	        | j                  d            dkD  rt        | |||||      S |du r.t        j                  j                  | j                               }	n#t        j                  | j                               }	t        j                  |	|      }
t        |      D cg c]7  }t        j                  t        |      D cg c]  }||k7  s	|
|    c}      9 }}}n|1	 |D cg c]  }|d   	 }}|D cg c]  }|d   	 }
}t	        |
      }nut!        |d|      }t        |j                  | j                         | j                                     }|D cg c]  }|d   	 }}|D cg c]  }|d   	 }
}t	        |
      }g }t        |      D ]  }| j#                  ||         }| j#                  |
|         }| ||||j%                               \  }}}n|}t        |j'                               |D cg c]  }d	|f c}z   }|j)                  t+        |||              |S c c}w c c}}w c c}w c c}w # t        $ rl t        |j                  | j                         | j                                     }|D cg c]  }|d   	 nc c}w }}|D cg c]  }|d   	 nc c}w }
}Y w xY wc c}w c c}w c c}w )
z<
    Make an n-fold list of CVPack from random indices.
    Fr   r   )r   r   rS   r    r   r   Tr   )Xy)n_splitsr   random_stater   )r7   r   r   r   r   r   r   r   num_rowr   r   rA   r   r9   split	get_labelr   r   r6   r   r>   r   )r   r   rS   r   r    r   r   r   r   r   r   r   rN   r   xsplitssfkr   r*   rR   r   r   r   s                          rO   mknfoldr   h  s    KEIINN4Uu}t!!+./!3!  d?))''7C))DLLN+CNN3.	 5\
 NN%,IQ!q&IaLIJ
 
 
		/&+,!,H,',-!1-I- I !%DQcii$.."2dnn6FiGH"()QAaD))#)*aQqT*	*I
C5\ 
0HQK(

9Q<($,VUEJJL$I!FE6FFLLN#u&Ms';&MM

6&%./
0 JE J
 -- 	/%++(8DNN<L+MNF&,-!--H-'-.!1..I.		/ ** 'Nsy   !I*9
I%I%	I*I: I0+I: 1I5=I: K2'K75K<%I*0
I: :AK/?KK/K$#K/.K/   )r   r   r   metricsr!   r"   r#   r   	as_pandasr%   	show_stdvr   r'   r   r(   r   r   r   PdDataFramec                   |du rt         st        d      t        |t              r|g}t        |t              rt        d      | j                         } t        | t              r/| D cg c]  }|d   dk(  s|d    }}t        |       } d| v r|| d<   |s#d| v rt        | d   t              r| d   }n| d   g}| j                  dd       i }t        ||| |||
|||	      }|g nt        j                  t        |            }|r$|du rdn|}|j                  t        ||	             |	r|j                  t        |	|
             t        ||dt        |            }t!        |      }|j#                  |       t%        |      D ]  }|j'                  |||d      r n|j)                  ||       |j+                  |||d      }|j,                  }t/        t0        t2        t        t4        t4        f      |      D ]R  \  }}}|dz   |vrg ||dz   <   |dz   |vrg ||dz   <   ||dz      j                  |       ||dz      j                  |       T |s|j7                         D ]  }||   d|j8                  dz    ||<     n |r 	 ddl}|j<                  j?                  |      }|jC                  |       |S c c}w # t@        $ r Y #w xY w)a  Cross-validation with given parameters.

    Parameters
    ----------
    params : dict
        Booster params.
    dtrain :
        Data to be trained. Only the :py:class:`DMatrix` without external memory is
        supported.
    num_boost_round :
        Number of boosting iterations.
    nfold : int
        Number of folds in CV.
    stratified : bool
        Perform stratified sampling.
    folds : a KFold or StratifiedKFold instance or list of fold indices
        Sklearn KFolds or StratifiedKFolds object.
        Alternatively may explicitly pass sample indices for each fold.
        For ``n`` folds, **folds** should be a length ``n`` list of tuples.
        Each tuple is ``(in,out)`` where ``in`` is a list of indices to be used
        as the training samples for the ``n`` th fold and ``out`` is a list of
        indices to be used as the testing samples for the ``n`` th fold.
    metrics : string or list of strings
        Evaluation metrics to be watched in CV.
    obj :

        Custom objective function.  See :doc:`Custom Objective
        </tutorials/custom_metric_obj>` for details.

    maximize : bool
        Whether to maximize the evaluataion metric (score or error).

    early_stopping_rounds: int
        Activates early stopping. Cross-Validation metric (average of validation
        metric computed over CV folds) needs to improve at least once in
        every **early_stopping_rounds** round(s) to continue training.
        The last entry in the evaluation history will represent the best iteration.
        If there's more than one metric in the **eval_metric** parameter given in
        **params**, the last metric will be used for early stopping.
    fpreproc : function
        Preprocessing function that takes (dtrain, dtest, param) and returns
        transformed versions of those.
    as_pandas : bool, default True
        Return pd.DataFrame when pandas is installed.
        If False or pandas is not installed, return np.ndarray
    verbose_eval : bool, int, or None, default None
        Whether to display the progress. If None, progress will be displayed
        when np.ndarray is returned. If True, progress will be displayed at
        boosting stage. If an integer is given, progress will be displayed
        at every given `verbose_eval` boosting stage.
    show_stdv : bool, default True
        Whether to display the standard deviation in progress.
        Results are not affected, and always contains std.
    seed : int
        Seed used to generate the folds (passed to numpy.random.seed).
    callbacks :
        List of callback functions that are applied at end of each iteration.
        It is possible to use predefined callbacks by using
        :ref:`Callback API <callback_api>`.

        .. note::

           States in callback are not preserved during training, which means callback
           objects can not be reused for multiple training sessions without
           reinitialization or deepcopy.

        .. code-block:: python

            for params in parameters_grid:
                # be sure to (re)initialize the callbacks before each run
                callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
                xgboost.train(params, Xy, callbacks=callbacks)

    shuffle : bool
        Shuffle data before creating folds.
    custom_metric :

        .. versionadded 1.6.0

        Custom metric function.  See :doc:`Custom Metric </tutorials/custom_metric_obj>`
        for details.

    Returns
    -------
    evaluation history : list(string)
    Tz;sklearn needs to be installed in order to use stratified cvz'`QuantileDMatrix` is not yet supported.r   r   r   N)	r   r   rS   r   r    r   r   r   r   )r/   r   r0   )r2   is_cvr3   z-meanz-std)"r   r   r8   rl   r   r<   r6   r7   dictpopr   r>   r   r   r   r?   rq   r@   rA   rB   rC   rD   aggregated_cvr   r   r
   r   keysr   pandasr   	from_dictImportErrorrE   ) r)   r*   r+   r   r   r   r   r!   r"   r#   r   r   r%   r   r   r'   r   r(   r   _metricsresultsrr   callbacks_containerboosterrN   should_breakresr{   meanstdr   pds                                    rO   cvr     s
   Z T"3I
 	
 '3)&)$BCC[[]F&$"(BQAaDM,AAaDBBfF"$,F=!&0f]+T2]+Gm,-G
JJ}d#&(G
G  'TYYtI-GI(D0ql*,)TU.ChWX+-t8C= W%G''0?# //FDIq#*::7AvtT!//"4c5%.?(@#A3G 	.NCsW}G+)+g&V|7*(*f%C'M"))$/C&L!((-	. \\^ H$QZ(F7+A+AA+EG
H%& 	ll,,W5G &&w/NM CB  		s   (J66J6J; ;	KK)
   )9rk   r6   osr;   typingr   r   r   r   r   r   r	   r
   r   r   numpyr   _typingr   r   r   callbackr   r   r   r   compatr   r   corer   r   r   r   r   r   r   r   r   r   r   r=   rl   rm   rn   EvalsLogPathLike	bytearrayrP   r   rq   ndarrayr   r   r   r   r   ro   rY   rO   <module>r      s<   5  	     = =  :   /H	  \
 6:##+/8</3GK6:&*\cN\\ \
 HU7C<012\ 
)	\ tn\ $C=\ +445\ 5s+,\ c2;;BCD\ !123\ F#\ \ \~R R67( 7(tY2:: Y2:: Y"** Y +/3
3 3 	3
 C=3 '(3 3 
&\3x +/!&*.F
F F 	F
 F C=F '(F F &'F F 
&\FR  }
  $##+/+//36:&*'}}} }
 } } } c]} 
)	} tn} $C=} '(} } 5d+,} }  !}" !123#}$ %}& F#'}( 4U
]*+)} }rY   