Ë
    »Ó^jÐ‹  ã            %       óô  — d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
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 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"m#Z#m$Z$m%Z%m&Z& g d¢Z'eeejP                  ege f   eejP                  egee    f   f   Z)eeeee*ef   geeeee*ef   f   f   Z+de,dee*ef   dee*ef   fd„Z-	 	 	 	 	 	 	 d)dee*ef   dede,deee      deee*      deee)ee)   f      deee*e	ef      de.deee      defd„Z/ G d„ d«      Z0dedeeeeejP                  ejP                  f      e$f      de,dee*ef   de,dee+   d e.d!e.d"e.de0fd#„Z1d$eee      dee   fd%„Z2	 	 	 	 	 	 	 	 	 	 	 	 	 d*dee*ef   dede,deeeeejP                  ejP                  f      e$f      de,d e.d!e.d&eee*ee*   f      deee)ee)   f      deee*e	ef      dee+   de,deee      d"e.d'e.dee*eee3   e0f   f   f d(„Z4y)+z+Library with training routines of LightGBM.é    N)ÚOrderedDictÚdefaultdict)Ú
attrgetter)ÚPath)ÚAnyÚCallableÚDictÚIterableÚListÚOptionalÚTupleÚUnioné   )Úcallback)
ÚBoosterÚDatasetÚ
EvalResultÚLightGBMErrorÚ_choose_param_valueÚ_ConfigAliasesÚ_InnerPredictorÚ_LGBM_CustomObjectiveFunctionÚ_LGBM_EvalFunctionResultTypeÚ_log_warning)ÚSKLEARN_INSTALLEDÚ_LGBMBaseCrossValidatorÚ_LGBMGroupKFoldÚ_LGBMStratifiedKFold)ÚcvÚ	CVBoosterÚtrainÚnum_boost_round_kwargÚparamsÚreturnc                 óf  — t        j                  d«      D �ci c]  }||v sŒ|||   “Œ }}t        d|| ¬«      }t        |«      dk  r|S t        t	        |j                  «       «      «      dk  r|S dj                  d„ |j                  «       D «       «      }t        d|› d|d   › d�«       |S c c}w )	a‚  Choose number of boosting rounds.

    In ``train()`` and ``cv()``, there are multiple ways to provide configuration for
    the number of boosting rounds to perform:

      * the ``num_boost_round`` keyword argument
      * any of the ``num_iterations`` or its aliases via the ``params`` dictionary

    These should be preferred in the following order (first one found wins):

      1. ``num_iterations`` provided via ``params`` (because it's the main parameter name)
      2. any other aliases of ``num_iterations`` provided via ``params``
      3. the ``num_boost_round`` keyword argument

    This function handles that choice, and issuing helpful warnings in the cases where the
    result might be surprising.

    Returns
    -------
    params : dict
        Parameters, with ``"num_iterations"`` set to the preferred value and all other
        aliases of ``num_iterations`` removed.
    Únum_iterations©Úmain_param_namer#   Údefault_valuer   z, c              3   ó0   K  — | ]  \  }}|› d |› �–— Œ y­w)ú=N© )Ú.0ÚaliasÚvals      úZ/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/lightgbm/engine.pyú	<genexpr>z)_choose_num_iterations.<locals>.<genexpr>b   s   è ø€ Òg±*°%¸ ˜w a¨ uÔ-Ñgùs   ‚zCFound conflicting values for num_iterations provided via 'params': z. LightGBM will perform up to z´ boosting rounds. To be confident in the maximum number of boosting rounds LightGBM will perform and to suppress this warning, modify 'params' so that only one of those is present.)	r   Úgetr   ÚlenÚsetÚvaluesÚjoinÚitemsr   )r"   r#   r.   Únum_iteration_configs_providedÚvalue_strings        r0   Ú_choose_num_iterationsr:   4   sä   € ô2 +9×*<Ñ*<Ð=MÓ*Nö&Ø!&ÐRWÐ[aÒRaˆˆv�e‰}Ñð&Ð"ð &ô !Ø(ØØ+ô€Fô Ð)Ó*¨aÒ/Øˆô Œ3Ð-×4Ñ4Ó6Ó7Ó8¸AÒ=Øˆð —9‘9ÑgÐ@^×@dÑ@dÓ@fÔgÓg€LÜØ
MÈlÈ^ð \'Ø'-Ð.>Ñ'?Ð&@ð AWð	Wôð €Mùò;&s
   ˜	B.¢B.Ú	train_setÚnum_boost_roundÚ
valid_setsÚvalid_namesÚfevalÚ
init_modelÚkeep_training_boosterÚ	callbacksc	                 óŒ  — t        |t        «      s"t        dt        |«      j                  › d�«      ‚t        |t
        «      rHt        |«      D ]:  \  }	}
t        |
t        «      rŒt        d|	› dt        |
«      j                  › d�«      ‚ t        j                  | «      } t        d| d¬«      } d}t        | d   «      r
| d   }d| d<   t        || ¬	«      } | d
   }|dk  rt        d|› d�«      ‚t        d| d¬«      } | d   €| j                  d«       | j                  dd«      }d}t        |t        t         f«      rt#        j$                  || ¬«      }n;t        |t&        «      r+t#        j(                  |t+        |j,                  fi | ¤Ž¬«      }|�|j/                  «       }nd}|j1                  | «      j3                  |«       d}d}g }g }|�¯t        |t        «      r|g}t        |t        «      r|g}t        |«      D ]{  \  }	}||u r
d}|�||	   }Œ|j5                  |j1                  | «      j7                  |«      «       |�#t9        |«      |	kD  r|j5                  ||	   «       Œh|j5                  d|	› �«       Œ} |€t;        «       }nFt        |«      D ]-  \  }	}|j<                  j?                  d|	t9        |«      z
  «       Œ/ t;        |«      }tA        jB                  | j                  dd«      «      rY|jE                  tA        jF                  | d   || j                  dd«      t        d| d¬«      j                  d«      dkD  ¬«      «       |D �ch c]  }tI        |dd«      sŒ|’Œ }}||z
  }tK        |tM        d«      ¬«      }tK        |tM        d«      ¬«      }	 t'        | |¬«      }|r|jO                  |«       tQ        ||d¬«      D ]  \  }}|jS                  ||«       Œ 	 |jU                  «        |D ]  }|jU                  «        Œ 	 d|_+        tY        |||z   «      D ]±  }	|D ]&  } |tA        jZ                  || |	|||z   d¬ «      «       Œ( |j]                  |¬!«       g }|�B|r |j_                  |ja                  |«      «       |j_                  |jc                  |«      «       	 |D ]&  } |tA        jZ                  || |	|||z   |¬ «      «       Œ( Œ³ ti        tj        «      |_3        D ]2  }|jl                  |jf                  |jn                     |jp                  <   Œ4 |s-|js                  |ju                  «       «      jw                  «        |S c c}w # |jU                  «        |D ]  }|jU                  «        Œ w xY w# t@        jd                  $ r+}|jV                  dz   |_+        |jf                  }Y d}~ Œæd}~ww xY w)"a·  Perform the training with given parameters.

    Parameters
    ----------
    params : dict
        Parameters for training. Values passed through ``params`` take precedence over those
        supplied via arguments.
    train_set : Dataset
        Data to be trained on.
    num_boost_round : int, optional (default=100)
        Number of boosting iterations.
    valid_sets : list of Dataset, or None, optional (default=None)
        List of data to be evaluated on during training.
    valid_names : list of str, or None, optional (default=None)
        Names of ``valid_sets``.
    feval : callable, list of callable, or None, optional (default=None)
        Customized evaluation function.
        Each evaluation function should accept two parameters: preds, eval_data,
        and return (metric_name, metric_value, maximize) or list of such tuples.

            preds : numpy 1-D array or numpy 2-D array (for multi-class task)
                The predicted values.
                For multi-class task, preds are numpy 2-D array of shape = [n_samples, n_classes].
                If custom objective function is used, predicted values are returned before any transformation,
                e.g. they are raw margin instead of probability of positive class for binary task in this case.
            eval_data : Dataset
                A ``Dataset`` to evaluate.
            metric_name : str
                Unique identifier for the metric (e.g. "custom_adjusted_mse").
            metric_value : float
                Value of the evaluation metric.
            maximize : bool
                Are higher values better? e.g. ``True`` for AUC and ``False`` for binary error.

        To ignore the default metric corresponding to the used objective,
        set the ``metric`` parameter to the string ``"None"`` in ``params``.
    init_model : str, pathlib.Path, Booster or None, optional (default=None)
        Filename of LightGBM model or Booster instance used for continue training.
    keep_training_booster : bool, optional (default=False)
        Whether the returned Booster will be used to keep training.
        If False, the returned value will be converted into _InnerPredictor before returning.
        This means you won't be able to use ``eval``, ``eval_train`` or ``eval_valid`` methods of the returned Booster.
        When your model is very large and cause the memory error,
        you can try to set this param to ``True`` to avoid the model conversion performed during the internal call of ``model_to_string``.
        You can still use _InnerPredictor as ``init_model`` for future continue training.
    callbacks : list of callable, or None, optional (default=None)
        List of callback functions that are applied at each iteration.
        See Callbacks in Python API for more information.

    Note
    ----
    A custom objective function can be provided for the ``objective`` parameter.
    It should accept two parameters: preds, train_data and return (grad, hess).

        preds : numpy 1-D array or numpy 2-D array (for multi-class task)
            The predicted values.
            Predicted values are returned before any transformation,
            e.g. they are raw margin instead of probability of positive class for binary task.
        train_data : Dataset
            The training dataset.
        grad : numpy 1-D array or numpy 2-D array (for multi-class task)
            The value of the first order derivative (gradient) of the loss
            with respect to the elements of preds for each sample point.
        hess : numpy 1-D array or numpy 2-D array (for multi-class task)
            The value of the second order derivative (Hessian) of the loss
            with respect to the elements of preds for each sample point.

    For multi-class task, preds are numpy 2-D array of shape = [n_samples, n_classes],
    and grad and hess should be returned in the same format.

    Returns
    -------
    booster : Booster
        The trained Booster model.
    z9train() only accepts Dataset object, train_set has type 'ú'.z8Every item in valid_sets must be a Dataset object. Item z has type 'Ú	objectiveNr'   Únone©r"   r#   r&   r   ú6Number of boosting rounds must be greater than 0. Got ú.Úearly_stopping_roundÚfirst_metric_onlyF©Ú
model_fileÚpred_parameter©ÚboosterrN   ÚtrainingTÚvalid_ÚorderÚearly_stopping_min_deltaç        Ú	verbosityr   ©Ústopping_roundsrK   Ú	min_deltaÚverboseÚbefore_iteration©Úkey)r#   r;   ©Ústrict©Úmodelr#   Ú	iterationÚbegin_iterationÚend_iterationÚevaluation_result_list©Úfobj)<Ú
isinstancer   Ú	TypeErrorÚtypeÚ__name__ÚlistÚ	enumerateÚcopyÚdeepcopyr   Úcallabler:   Ú
ValueErrorÚpopr2   Ústrr   r   Úfrom_model_filer   Úfrom_boosterÚdictr#   Úcurrent_iterationÚ_update_paramsÚ_set_predictorÚappendÚset_referencer3   r4   Ú__dict__Ú
setdefaultr   Ú_should_enable_early_stoppingÚaddÚearly_stoppingÚgetattrÚsortedr   Úset_train_data_nameÚzipÚ	add_validÚ_reverse_update_paramsÚbest_iterationÚrangeÚCallbackEnvÚupdateÚextendÚ
eval_trainÚ
eval_validÚEarlyStopExceptionÚ
best_scorer   r   Úmetric_valueÚdataset_nameÚmetric_nameÚmodel_from_stringÚmodel_to_stringÚfree_dataset) r#   r;   r<   r=   r>   r?   r@   rA   rB   ÚiÚ
valid_itemrg   rK   Ú	predictorÚinit_iterationÚis_valid_contain_trainÚtrain_data_nameÚreduced_valid_setsÚname_valid_setsÚ
valid_dataÚcallbacks_setÚcbÚcallbacks_before_iter_setÚcallbacks_after_iter_setÚcallbacks_before_iterÚcallbacks_after_iterrP   Ú	valid_setÚname_valid_setre   ÚearlyStopExceptionÚresults                                    r0   r!   r!   l   s  € ôl �i¤Ô)ÜÐSÔTXÐYbÓTc×TlÑTlÐSmÐmoÐpÓqÐqä�*œdÔ#Ü& zÓ2ò 	‰MˆAˆzÜ˜j¬'Õ2ÜðØ˜3˜k¬$¨zÓ*:×*CÑ*CÐ)DÀBðHóð ð	ô �]‰]˜6Ó"€FÜ Ø#ØØô€Fð
 59€DÜ��{Ñ#Ô$Ø�kÑ"ˆØ$ˆˆ{Ñä#¸/ÐRXÔY€FØÐ-Ñ.€OØ˜!ÒÜÐQÐRaÐQbÐbcÐdÓeÐeô !Ø.ØØô€Fð
 Ð$Ñ%Ð-Ø�
‰
Ð)Ô*ØŸ
™
Ð#6¸Ó>Ðà+/€IÜ�*œs¤D˜kÔ*Ü#×3Ñ3¸zÐZ`Ôa‰	Ü	�J¤Ô	(Ü#×0Ñ0¸ÔTXÐYc×YjÑYjÑTuÐntÑTuÔvˆ	àÐØ"×4Ñ4Ó6‰àˆà×Ñ˜VÓ$×3Ñ3°IÔ>à"ÐØ €OØÐØ€OØÐÜ�j¤'Ô*Ø$˜ˆJÜ�k¤3Ô'Ø&˜-ˆKÜ& zÓ2ò 	5‰MˆAˆzà˜YÑ&Ø)-Ð&ØÐ*Ø&1°!¡n�OØØ×%Ñ% j×&?Ñ&?ÀÓ&G×&UÑ&UÐV_Ó&`ÔaØÐ&¬3¨{Ó+;¸aÒ+?Ø×&Ñ& {°1¡~Õ6à×&Ñ&¨°¨s |Õ4ð	5ð ÐÜ›‰ä˜yÓ)ò 	@‰EˆAˆrØ�K‰K×"Ñ" 7¨A´°I³Ñ,>Õ?ð	@ä˜I›ˆä×-Ñ-¨f¯j©jÐ9OÐQRÓ.SÔTØ×ÑÜ×#Ñ#Ø &Ð'=Ñ >Ø"3Ø Ÿ*™*Ð%?ÀÓEÜ+Ø$/Ø!Ø"#ô÷ ‘#�kÓ"Øñô	
ô	
ð /<Ö f¨¼wÀrÐK]Ð_dÕ?e¢Ð fÐÐ fØ,Ð/HÑHÐÜ"Ð#<Ä*ÈWÓBUÔVÐÜ!Ð":Ä
È7Ó@SÔTÐð	/Ü °9Ô=ˆÙ!Ø×'Ñ'¨Ô8Ü),Ð-?ÀÐY]Ô)^ò 	9Ñ%ˆI�~Ø×Ñ˜i¨Õ8ñ	9ð 	×(Ñ(Ô*Ø+ò 	/ˆIØ×,Ñ,Õ.ñ	/à€GÔô �> >°OÑ#CÓDò $ˆØ'ò 
	ˆBÙÜ×$Ñ$Ø!Ø!ØØ$2Ø"0°?Ñ"BØ+/ôõ	ð
	ð 	�‰˜DˆÔ!à35ÐàÐ!Ù%Ø&×-Ñ-¨g×.@Ñ.@ÀÓ.GÔHØ"×)Ñ)¨'×*<Ñ*<¸UÓ*CÔDð	Ø*ò 
�ÙÜ×(Ñ(Ø%Ø%Ø"#Ø(6Ø&4°Ñ&FØ/Eôõ	ñ
ð-$ôJ %¤[Ó1€GÔØ(ò ZˆØFL×FYÑFYˆ×Ñ˜6×.Ñ.Ñ/°×0BÑ0BÒCðZá Ø×!Ñ! '×"9Ñ"9Ó";Ó<×IÑIÔKØ€Nùò{ !gøð 	×(Ñ(Ô*Ø+ò 	/ˆIØ×,Ñ,Õ.ñ	/ûôL ×*Ñ*ò 	Ø%7×%FÑ%FÈÑ%JˆGÔ"Ø%7×%BÑ%BÐ"Ýûð	ús1   ÍUÍ%UÎAU Ò++VÕ)VÖWÖ V>Ö>Wc                   ó0  — e Zd ZdZ	 ddeeeef      fd„Zde	ee
f   ddfd„Zdee   d	ed
ede	ee
f   fd„Zdedee
e
gee
   f   fd„Zde	ee
f   fd„Zde	ee
f   ddfd„Zdedd fd„Z	 	 	 ddee   d	ed
edefd„Z	 	 	 ddeeef   dee   d	ed
edd f
d„Zy)r    aK  CVBooster in LightGBM.

    Auxiliary data structure to hold and redirect all boosters of ``cv()`` function.
    This class has the same methods as Booster class.
    All method calls, except for the following methods, are actually performed for underlying Boosters and
    then all returned results are returned in a list.

    - ``model_from_string()``
    - ``model_to_string()``
    - ``save_model()``

    Attributes
    ----------
    boosters : list of Booster
        The list of underlying fitted models.
    best_iteration : int
        The best iteration of fitted model.
    NrM   c                 ó°   — g | _         d| _        |�:t        |d«      5 }| j                  t	        j
                  |«      «       ddd«       yy# 1 sw Y   yxY w)z¿Initialize the CVBooster.

        Parameters
        ----------
        model_file : str, pathlib.Path or None, optional (default=None)
            Path to the CVBooster model file.
        éÿÿÿÿNÚr)Úboostersr‡   ÚopenÚ
_from_dictÚjsonÚload)ÚselfrM   Úfiles      r0   Ú__init__zCVBooster.__init__u  sX   € ð (*ˆŒØ ˆÔàÐ!Ü�j #Ó&ð 1¨$Ø—‘¤§	¡	¨$£Ô0÷1ð 1ð "÷1ð 1ús   �%AÁAÚmodelsr$   c                 ó„   — |d   | _         g | _        |d   D ]'  }| j                  j                  t        |¬«      «       Œ) y)zLoad CVBooster from dict.r‡   r­   )Ú	model_strN)r‡   r­   rz   r   )r²   rµ   r·   s      r0   r¯   zCVBooster._from_dict‡  sB   € à$Ð%5Ñ6ˆÔØˆŒØ 
Ñ+ò 	?ˆIØ�M‰M× Ñ ¤°9Ô!=Õ>ñ	?ó    Únum_iterationÚstart_iterationÚimportance_typec                óŒ   — g }| j                   D ]%  }|j                  |j                  |||¬«      «       Œ' || j                  dœS )zSerialize CVBooster to dict.©r¹   rº   r»   )r­   r‡   )r­   rz   r”   r‡   )r²   r¹   rº   r»   Ú
models_strrP   s         r0   Ú_to_dictzCVBooster._to_dictŽ  sX   € ð ˆ
Ø—}‘}ò 	ˆGØ×ÑØ×'Ñ'Ø"/ÀÐbqð (ó õð	ð '¸$×:MÑ:MÑNÐNr¸   Únamec                 óJ   ‡ ‡— dt         dt         dt        t            fˆˆ fd„}|S )z#Redirect methods call of CVBooster.ÚargsÚkwargsr$   c                  óp   •— g }‰j                   D ]#  }|j                   t        |‰«      | i |¤Ž«       Œ% |S )z>Call methods with each booster, and concatenate their results.)r­   rz   r�   )rÂ   rÃ   ÚretrP   rÀ   r²   s       €€r0   Úhandler_functionz/CVBooster.__getattr__.<locals>.handler_function¢  sB   ø€ àˆCØŸ=™=ò D�Ø—
‘
Ð1œ7 7¨DÓ1°4ÐB¸6ÑBÕCðDàˆJr¸   )r   r   )r²   rÀ   rÆ   s   `` r0   Ú__getattr__zCVBooster.__getattr__Ÿ  s)   ù€ ð	¤Cð 	´3ð 	¼4Ä¹9ö 	ð  Ðr¸   c                 ó   — t        | «      S ©N)Úvars)r²   s    r0   Ú__getstate__zCVBooster.__getstate__«  s   € Ü�D‹zÐr¸   Ústatec                 ó8   — t        | «      j                  |«       y rÉ   )rÊ   rŠ   )r²   rÌ   s     r0   Ú__setstate__zCVBooster.__setstate__®  s   € ÜˆT‹
×Ñ˜%Õ r¸   r·   c                 óN   — | j                  t        j                  |«      «       | S )z÷Load CVBooster from a string.

        Parameters
        ----------
        model_str : str
            Model will be loaded from this string.

        Returns
        -------
        self : CVBooster
            Loaded CVBooster object.
        )r¯   r°   Úloads)r²   r·   s     r0   r“   zCVBooster.model_from_string±  s   € ð 	�‰œŸ
™
 9Ó-Ô.Øˆr¸   c                 óP   — t        j                  | j                  |||¬«      «      S )a\  Save CVBooster to JSON string.

        Parameters
        ----------
        num_iteration : int or None, optional (default=None)
            Index of the iteration that should be saved.
            If None, if the best iteration exists, it is saved; otherwise, all iterations are saved.
            If <= 0, all iterations are saved.
        start_iteration : int, optional (default=0)
            Start index of the iteration that should be saved.
        importance_type : str, optional (default="split")
            What type of feature importance should be saved.
            If "split", result contains numbers of times the feature is used in a model.
            If "gain", result contains total gains of splits which use the feature.

        Returns
        -------
        str_repr : str
            JSON string representation of CVBooster.
        r½   )r°   Údumpsr¿   )r²   r¹   rº   r»   s       r0   r”   zCVBooster.model_to_stringÁ  s(   € ô4 �z‰zØ�M‰M¨ÀÐhwˆMÓxó
ð 	
r¸   Úfilenamec                 óš   — t        |d«      5 }t        j                  | j                  |||¬«      |«       ddd«       | S # 1 sw Y   | S xY w)aš  Save CVBooster to a file as JSON text.

        Parameters
        ----------
        filename : str or pathlib.Path
            Filename to save CVBooster.
        num_iteration : int or None, optional (default=None)
            Index of the iteration that should be saved.
            If None, if the best iteration exists, it is saved; otherwise, all iterations are saved.
            If <= 0, all iterations are saved.
        start_iteration : int, optional (default=0)
            Start index of the iteration that should be saved.
        importance_type : str, optional (default="split")
            What type of feature importance should be saved.
            If "split", result contains numbers of times the feature is used in a model.
            If "gain", result contains total gains of splits which use the feature.

        Returns
        -------
        self : CVBooster
            Returns self.
        Úwr½   N)r®   r°   Údumpr¿   )r²   rÓ   r¹   rº   r»   r³   s         r0   Ú
save_modelzCVBooster.save_modelß  sX   € ô: �(˜CÓ ð 	 DÜ�I‰IØ—‘Ø"/ÀÐbqð ó ð ô	÷	ð ˆ÷	ð ˆús   �)A Á A
rÉ   )Nr   Úsplit)rk   Ú
__module__Ú__qualname__Ú__doc__r   r   rs   r   r´   r	   r   r¯   Úintr¿   r   r   rÇ   rË   rÎ   r“   r”   r×   r,   r¸   r0   r    r    a  so  „ ñð* 26ñ1à˜U 3¨ 9Ñ-Ñ.ó1ð$?  c¨3 h¡ð ?°Dó ?ðOð   ‘}ðOð ð	Oð
 ðOð 
ˆc�3ˆh‰óOð"
  ð 
 ¨°#°s°¸TÀ#¹YÐ1FÑ(Gó 
 ð˜d 3¨ 8™nó ð! $ s¨C x¡.ð !°Tó !ð¨3ð °;ó ð$ (,Ø Ø&ñ	
à ‘}ð
ð ð
ð ð	
ð
 
ó
ðB (,Ø Ø&ñ%à˜˜T˜	Ñ"ð%ð   ‘}ð%ð ð	%ð
 ð%ð 
ô%r¸   r    Ú	full_dataÚfoldsÚnfoldÚseedÚfpreprocÚ
stratifiedÚshuffleÚeval_train_metricc        	         óR  ‡— | j                  «       } | j                  «       }	|�ít        |d«      st        |d«      st        d«      ‚t        |d«      �r’| j	                  «       }
|
�Ot        j                  |
t
        j                  ¬«      }
t        j                  t        t        |
«      «      |
¬«      }n%t        j                  |	t
        j                  ¬«      }|j                  t        j                  |	«      | j                  «       |¬«      }�nÕt        ˆfd„t!        j"                  d«      D «       «      r¡t$        st'        d	«      ‚t        j                  | j	                  «       t
        j                  ¬«      }
t        j                  t        t        |
«      «      |
¬«      }t)        |¬
«      }|j                  t        j                  |	«      |¬«      }�n|rTt$        st'        d«      ‚t+        |||¬«      }|j                  t        j                  |	«      | j                  «       ¬«      }n·|r/t
        j,                  j/                  |«      j1                  |	«      }nt        j2                  |	«      }t        j4                  ||«      }t        |«      D ��cg c]7  }t        j6                  t        |«      D �cg c]  }||k7  sŒ	||   ‘Œ c}«      ‘Œ9 }}}t9        ||d¬«      }t;        «       }|D ]§  \  }}| j=                  t?        |«      «      }| j=                  t?        |«      «      }|� |||‰jA                  «       «      \  }}}n‰}tC        ||«      }|r|jE                  |d«       |jE                  |d«       |jF                  jI                  |«       Œ© |S c c}w c c}}w )z2Make a n-fold list of Booster from random indices.Ú__iter__rØ   zyfolds should be a generator or iterator of (train_idx, test_idx) tuples or scikit-learn splitter object with split method)Údtype)Úrepeats)ÚXÚyÚgroupsc              3   óF   •K  — | ]  }‰j                  |d «      dv –— Œ y­w)Ú >   ÚxendcgÚxe_ndcgÚ
lambdarankÚrank_xendcgÚxendcg_martÚxe_ndcg_martN)r2   )r-   Ú	obj_aliasr#   s     €r0   r1   z _make_n_folds.<locals>.<genexpr>%  s/   øè ø€ ò 
ð ð �J‰J�y "Ó%Ø`ôañ
ùs   ƒ!rE   z'scikit-learn is required for ranking cv)Ún_splits)ré   rë   z*scikit-learn is required for stratified cv)rõ   rã   Úrandom_state)ré   rê   Tr^   r!   Úvalid)%Ú	constructÚnum_dataÚhasattrÚAttributeErrorÚ	get_groupÚnpÚasarrayÚint32Úrepeatrˆ   r3   ÚzerosrØ   ÚemptyÚ	get_labelÚanyr   r2   r   r   r   r   ÚrandomÚRandomStateÚpermutationÚarangeÚarray_splitÚconcatenater„   r    Úsubsetr‚   rn   r   r…   r­   rz   )rÝ   rÞ   rß   r#   rà   rá   râ   rã   rä   rù   Ú
group_infoÚflatted_groupÚgroup_kfoldÚskfÚrandidxÚtest_idÚkr–   Útrain_idrÅ   Ú	train_idxÚtest_idxr;   r¥   ÚtparamÚbooster_for_folds      `                      r0   Ú_make_n_foldsr    sô  ø€ ð ×#Ñ#Ó%€IØ×!Ñ!Ó#€HØÐÜ�u˜jÔ)´'¸%ÀÔ2IÜ ðDóð ô �5˜'Õ"Ø"×,Ñ,Ó.ˆJØÐ%ÜŸZ™Z¨
¼"¿(¹(ÔC�
Ü "§	¡	¬%´°J³Ó*@È*Ô U‘ä "§¡¨¼¿¹Ô B�Ø—K‘K¤"§(¡(¨8Ó"4¸	×8KÑ8KÓ8MÐVc�KÓdŠEäó 
ô ,×/Ñ/°Ó<ô
ô 
õ
 %Ü#Ð$MÓNÐNäŸ™ I×$7Ñ$7Ó$9ÄÇÁÔJˆJÜŸI™I¤e¬C°
«OÓ&<ÀjÔQˆMÜ)°5Ô9ˆKØ×%Ñ%¬¯©°Ó(:À=Ð%ÓQŠEÙÝ$Ü#Ð$PÓQÐQÜ&°¸wÐUYÔZˆCØ—I‘I¤§¡¨Ó 2°i×6IÑ6IÓ6K�IÓL‰EáÜŸ)™)×/Ñ/°Ó5×AÑAÀ(ÓK‘äŸ)™) HÓ-�Ü—n‘n W¨eÓ4ˆGÜ^cÐdiÓ^j×kÐYZœŸ™¼EÀ%»LÖ'S°qÈAÐQRËF¨°«
Ò'SÕTÐkˆHÑkÜ˜ '°$Ô7ˆEä
‹+€CØ$ò .Ñˆ	�8Ø×$Ñ$¤V¨IÓ%6Ó7ˆ	Ø×$Ñ$¤V¨HÓ%5Ó6ˆ	àÐÙ+3°I¸yÈ&Ï+É+Ë-Ó+XÑ(ˆI�y¡&àˆFÜ" 6¨9Ó5ÐÙØ×&Ñ& y°'Ô:Ø×"Ñ" 9¨gÔ6Ø�‰×ÑÐ,Õ-ð.ð €Jùò# (TùÓks   Ê!N#Ê:
NËNË	N#ÎN#Úraw_resultsc                 óä  — t        «       }t        «       }| D ]`  }|D ]Y  }|j                  |j                  f}|j                  ||<   |j	                  |g «       ||   j                  |j                  «       Œ[ Œb |j                  «       D ��cg c]V  \  }}t        |d   |d   t        t        j                  |«      «      ||   t        t        j                  |«      «      ¬«      ‘ŒX c}}S c c}}w )z#Aggregate cross-validation results.r   r   )r‘   r’   r�   ÚmaximizeÚmetric_std_dev)r   r‘   r’   r  r}   rz   r�   r7   r   Úfloatrý   ÚmeanÚstd)r  Úmetric_typesÚmetric_valuesÚresult_listr¨   r]   r  Úvs           r0   Ú_agg_cv_resultr$  P  sè   € ô 1<³€LÜ8C»€MØ"ò ;ˆØ!ò 	;ˆFØ×&Ñ&¨×(:Ñ(:Ð;ˆCØ &§¡ˆL˜ÑØ×$Ñ$ S¨"Ô-Ø˜#Ñ×%Ñ% f×&9Ñ&9Õ:ñ		;ð;ð( "×'Ñ'Ó)÷	ñ ˆAˆqô 	Ø˜1™Ø˜!™ÜœrŸw™w q›zÓ*Ø! !‘_Ü ¤§¡¨£Ó+ö	
ó	ð 	ùó 	s   ÂAC,ÚmetricsÚreturn_cvboosterc                 ó¶  — t        |t        «      s"t        dt        |«      j                  › d�«      ‚t        j                  | «      } t        d| d¬«      } d}t        | d   «      r
| d   }d| d<   t        || ¬«      } | d   }|d	k  rt        d
|› d�«      ‚t        d| d¬«      } | d   €| j                  d«       | j                  dd«      }t        |	t        t        f«      rt        j                   |	| ¬«      }n>t        |	t"        «      r,t        j$                  |	t'        |	j(                  fi | ¤Ž¬«      }nd}|�1t+        j                  d«      D ]  }| j                  |d«       Œ || d<   |j-                  | «      j/                  |«       t1        t2        «      }t5        |||| ||
|||¬«	      }|€t7        «       }nFt9        |«      D ]-  \  }}|j:                  j=                  d|t?        |«      z
  «       Œ/ t7        |«      }tA        jB                  | j                  dd	«      «      rY|jE                  tA        jF                  | d   || j                  dd«      t        d| d¬«      j                  d«      d	kD  ¬«      «       |D �ch c]  }tI        |dd«      sŒ|’Œ }}||z
  }tK        |tM        d«      ¬«      }tK        |tM        d«      ¬«      }tO        |«      D ]ö  }|D ]#  } |tA        jP                  || |d	|d¬«      «       Œ% |jS                  |¬«       tU        |jW                  |«      «      }|D ]r  }||jX                  › d|jZ                  › d�   j]                  |j^                  «       ||jX                  › d|jZ                  › d�   j]                  |j`                  «       Œt 	 |D ]#  } |tA        jP                  || |d	||¬«      «       Œ% Œø |r||d <   t'        |«      S c c}w # t@        jb                  $ r]}|jd                  dz   |_2        |jf                  D ]  }|jd                  |_2        Œ |D ]  } ||    d|jd                   || <   Œ Y d}~ Œ‚d}~ww xY w)!a  Perform the cross-validation with given parameters.

    Parameters
    ----------
    params : dict
        Parameters for training. Values passed through ``params`` take precedence over those
        supplied via arguments.
    train_set : Dataset
        Data to be trained on.
    num_boost_round : int, optional (default=100)
        Number of boosting iterations.
    folds : generator or iterator of (train_idx, test_idx) tuples, scikit-learn splitter object or None, optional (default=None)
        If generator or iterator, it should yield the train and test indices for each fold.
        If object, it should be one of the scikit-learn splitter classes
        (https://scikit-learn.org/stable/modules/classes.html#splitter-classes)
        and have ``split`` method.
        This argument has highest priority over other data split arguments.
    nfold : int, optional (default=5)
        Number of folds in CV.
    stratified : bool, optional (default=True)
        Whether to perform stratified sampling.
    shuffle : bool, optional (default=True)
        Whether to shuffle before splitting data.
    metrics : str, list of str, or None, optional (default=None)
        Evaluation metrics to be monitored while CV.
        If not None, the metric in ``params`` will be overridden.
    feval : callable, list of callable, or None, optional (default=None)
        Customized evaluation function.
        Each evaluation function should accept two parameters: preds, eval_data,
        and return (metric_name, metric_value, maximize) or list of such tuples.

            preds : numpy 1-D array or numpy 2-D array (for multi-class task)
                The predicted values.
                For multi-class task, preds are numpy 2-D array of shape = [n_samples, n_classes].
                If custom objective function is used, predicted values are returned before any transformation,
                e.g. they are raw margin instead of probability of positive class for binary task in this case.
            eval_data : Dataset
                A ``Dataset`` to evaluate.
            metric_name : str
                Unique identifier for the metric (e.g. "custom_adjusted_mse").
            metric_value : float
                Value of the evaluation metric.
            maximize : bool
                Are higher values better? e.g. ``True`` for AUC and ``False`` for binary error.

        To ignore the default metric corresponding to the used objective,
        set ``metrics`` to the string ``"None"``.
    init_model : str, pathlib.Path, Booster or None, optional (default=None)
        Filename of LightGBM model or Booster instance used for continue training.
    fpreproc : callable or None, optional (default=None)
        Preprocessing function that takes (dtrain, dtest, params)
        and returns transformed versions of those.
    seed : int, optional (default=0)
        Seed used to generate the folds (passed to numpy.random.seed).
    callbacks : list of callable, or None, optional (default=None)
        List of callback functions that are applied at each iteration.
        See Callbacks in Python API for more information.
    eval_train_metric : bool, optional (default=False)
        Whether to display the train metric in progress.
        The score of the metric is calculated again after each training step, so there is some impact on performance.
    return_cvbooster : bool, optional (default=False)
        Whether to return Booster models trained on each fold through ``CVBooster``.

    Note
    ----
    A custom objective function can be provided for the ``objective`` parameter.
    It should accept two parameters: preds, train_data and return (grad, hess).

        preds : numpy 1-D array or numpy 2-D array (for multi-class task)
            The predicted values.
            Predicted values are returned before any transformation,
            e.g. they are raw margin instead of probability of positive class for binary task.
        train_data : Dataset
            The training dataset.
        grad : numpy 1-D array or numpy 2-D array (for multi-class task)
            The value of the first order derivative (gradient) of the loss
            with respect to the elements of preds for each sample point.
        hess : numpy 1-D array or numpy 2-D array (for multi-class task)
            The value of the second order derivative (Hessian) of the loss
            with respect to the elements of preds for each sample point.

    For multi-class task, preds are numpy 2-D array of shape = [n_samples, n_classes],
    and grad and hess should be returned in the same format.

    Returns
    -------
    eval_results : dict
        History of evaluation results of each metric.
        The dictionary has the following format:
        {'valid metric1-mean': [values], 'valid metric1-stdv': [values],
        'valid metric2-mean': [values], 'valid metric2-stdv': [values],
        ...}.
        If ``return_cvbooster=True``, also returns trained boosters wrapped in a ``CVBooster`` object via ``cvbooster`` key.
        If ``eval_train_metric=True``, also returns the train metric history.
        In this case, the dictionary has the following format:
        {'train metric1-mean': [values], 'valid metric1-mean': [values],
        'train metric2-mean': [values], 'valid metric2-mean': [values],
        ...}.
    z6cv() only accepts Dataset object, train_set has type 'rD   rE   Nr'   rF   rG   r&   r   rH   rI   rJ   rK   FrL   rO   Úmetric)	rÝ   rÞ   rß   r#   rà   rá   râ   rã   rä   rS   rT   rU   rV   r   rW   r[   r\   r`   rf   ú z-meanz-stdvÚ	cvbooster)4rh   r   ri   rj   rk   rn   ro   r   rp   r:   rq   rr   r2   rs   r   r   rt   r   ru   rv   r#   r   rx   ry   r   rl   r  r4   rm   r|   r}   r3   r   r~   r   r€   r�   r‚   r   rˆ   r‰   rŠ   r$  r�   r‘   r’   rz   r�   r  rŽ   r‡   r­   )!r#   r;   r<   rÞ   rß   râ   rã   r%  r?   r@   rá   rà   rB   rä   r&  rg   rK   r˜   Úmetric_aliasÚresultsr*  rŸ   r–   r    r¡   r¢   r£   r¤   re   r¨   r§   Úbstr  s!                                    r0   r   r   x  s¶  € ôh �i¤Ô)ÜÐPÔQUÐV_ÓQ`×QiÑQiÐPjÐjlÐmÓnÐnä�]‰]˜6Ó"€FÜ Ø#ØØô€Fð
 59€DÜ��{Ñ#Ô$Ø�kÑ"ˆØ$ˆˆ{Ñä#¸/ÐRXÔY€FØÐ-Ñ.€OØ˜!ÒÜÐQÐRaÐQbÐbcÐdÓeÐeô !Ø.ØØô€Fð
 Ð$Ñ%Ð-Ø�
‰
Ð)Ô*ØŸ
™
Ð#6¸Ó>Ðä�*œs¤D˜kÔ*Ü#×3Ñ3Ø!Ø!ô
‰	ô 
�J¤Ô	(Ü#×0Ñ0ØÜ 
× 1Ñ 1Ñ<°VÑ<ô
‰	ð
 ˆ	àÐÜ*×.Ñ.¨xÓ8ò 	+ˆLØ�J‰J�| TÕ*ð	+à"ˆˆxÑà×Ñ˜VÓ$×3Ñ3°IÔ>äœ$Ó€GÜØØØØØØØØØ+ô
€Ið ÐÜ›‰ä˜yÓ)ò 	@‰EˆAˆrØ�K‰K×"Ñ" 7¨A´°I³Ñ,>Õ?ð	@ä˜I›ˆä×-Ñ-¨f¯j©jÐ9OÐQRÓ.SÔTØ×ÑÜ×#Ñ#Ø &Ð'=Ñ >Ø"3Ø Ÿ*™*Ð%?ÀÓEÜ+Ø$/Ø!Ø"#ô÷ ‘#�kÓ"Øñô	
ô	
ð /<Ö f¨¼wÀrÐK]Ð_dÕ?e¢Ð fÐÐ fØ,Ð/HÑHÐÜ"Ð#<Ä*ÈWÓBUÔVÐÜ!Ð":Ä
È7Ó@SÔTÐä�?Ó#ò #ˆØ'ò 
	ˆBÙÜ×$Ñ$Ø#Ø!ØØ$%Ø"1Ø+/ôõ	ð
	ð 	×Ñ˜dÐÔ#Ü!/°	×0DÑ0DÀUÓ0KÓ!LÐØ,ò 	fˆFØ�v×*Ñ*Ð+¨1¨V×-?Ñ-?Ð,@ÀÐFÑG×NÑNÈv×ObÑObÔcØ�v×*Ñ*Ð+¨1¨V×-?Ñ-?Ð,@ÀÐFÑG×NÑNÈv×OdÑOdÕeð	fð	Ø*ò 
�ÙÜ×(Ñ(Ø'Ø%Ø"#Ø()Ø&5Ø/Eôõ	ñ
ð%#ñJ Ø(ˆ�Ñä�‹=Ðùò[ !gøôD ×*Ñ*ò 	Ø'9×'HÑ'HÈ1Ñ'LˆIÔ$Ø ×)Ñ)ò >�Ø%.×%=Ñ%=�Õ"ð>àò D�Ø$ Q™ZÐ(B¨)×*BÑ*BÐC�˜’
ðDåûð	ús%   ÊO#ÊO#Î'(O(Ï(QÏ;AQÑQ)éd   NNNNFN)r.  Né   TTNNNNr   NFF)5rÛ   rn   r°   Úcollectionsr   r   Úoperatorr   Úpathlibr   Útypingr   r   r	   r
   r   r   r   r   Únumpyrý   rí   r   Úbasicr   r   r   r   r   r   r   r   r   r   Úcompatr   r   r   r   Ú__all__ÚndarrayÚ_LGBM_CustomMetricFunctionrs   Ú_LGBM_PreprocFunctionrÜ   r:   Úboolr!   r    r  r$  r  r   r,   r¸   r0   ú<module>r<     s  ðá 1ã Û ß 0Ý Ý ß N× NÓ Nã å ÷÷ ÷ ÷ fÓ eò€ð #ØØ	�‰�WÐØ$ð	&ñð Ø	�‰�WÐØÐ)Ñ*ð	,ñð	ñ	Ð ð !Øˆg�t˜C ˜H‘~Ð&Ø	ˆ'�7˜D  c ™NÐ
*Ñ+ð-ñÐ ð5°Sð 5À$ÀsÈCÀxÁ.ð 5ÐUYÐZ]Ð_bÐZbÑUcó 5ðv Ø*.Ø'+Ø[_Ø6:Ø"'Ø*.ñrØ��c�‰Nðràðrð ðrð ˜˜g™Ñ'ð	rð
 ˜$˜s™)Ñ$ðrð �EÐ4°dÐ;UÑ6VÐVÑWÑXðrð ˜˜s D¨'Ð1Ñ2Ñ3ðrð  ðrð ˜˜X™Ñ'ðrð ór÷jcñ cðLFàðFð �E˜( 5¨¯©°R·Z±ZÐ)?Ñ#@ÑAÐCZÐZÑ[Ñ\ðFð ð	Fð
 ��c�‰NðFð ðFð Ð,Ñ-ðFð ðFð ðFð ðFð óFðR%Ø�d˜:Ñ&Ñ'ð%à	ˆ*Ñó%ðV Ø_cØØØØ/3Ø[_Ø6:Ø04ØØ*.Ø#Ø"ñvØ��c�‰Nðvàðvð ðvð �E˜( 5¨¯©°R·Z±ZÐ)?Ñ#@ÑAÐCZÐZÑ[Ñ\ð	vð
 ðvð ðvð ðvð �e˜C  c¡˜NÑ+Ñ,ðvð �EÐ4°dÐ;UÑ6VÐVÑWÑXðvð ˜˜s D¨'Ð1Ñ2Ñ3ðvð Ð,Ñ-ðvð ðvð ˜˜X™Ñ'ðvð ðvð ðvð  
ˆ#ˆu�T˜%‘[ )Ð+Ñ,Ð
,Ñ-ô!vr¸   