
    rii2                       U d Z ddlZddlmZ ddlmZ ddlmZ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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ZddlZddlm Z! dd	lm"Z# dd
lm$Z% ddl&m'Z' ddlm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl*m4Z5 ddl*m6Z7 ddl*m8Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZT ddlUmVZV ddlWmXZY ddlEmZZZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z` ee!ja        e%jb        e%jc        f         Zdeeed<   ee!ja        e%jb        f         Zfeeed<    ed e=ed!          Zgg d"Zh eji        d#          Zjd$ekd%eeeel         eeelekf                  f                  d&eek         d'e7fd(Zmd$ekd)eel         d*eeelekf                  d&eek         d'e7f
d+Zn G d, d-e*jo                  Zod.ed/         d'd/fd0Zp G d1 d2          Zq ed3          Zr ed4          Zsd.ed/         d5eeserf         d6ed7eel         d'erf
d8Zt G d9 d:eq          Zu	 	 dd.d/d$ekd;eeelef                  d<ee5         d'eeleelekf         f         f
d=Zvd'eeelef                  fd>Zwd?eqd@eeeeqelf                           d'eel         fdAZxd7eel         d.d/d'dfdBZyd.d/dCeelef         d;eeelef                  dDeelef         d?eqdEekd@eeeeqelf                           dFee@         dGeek         dHeekezf         dIee=         dJeee3                  dKee?         d<ee5         d'eeg         fdLZ{eC	 ddddddNddddOd.d/dDeelef         d?eqdEekd@eeeeqelf                           dFee@         dGeek         dIee=         dHeekezf         dJeee3                  dKee?         d<ee5         d'efdP            ZXdQezdRed'ezfdSZ|dTedUedVeek         dQezd'ef
dWZ}dXedYdZdTefd[eed         dReekd\f         d]eekelf         d'edfd^Z~dYe=d_ekdQezd`ezdaed'eeekd\f         eekelf         f         fdbZd.d/dcee=edZf         d'dZfddZd.d/dCeelef         dcee=edZf         dTefdeezdfedgezdhezdiezdjezdkezdle1dmezd'edfdnZeCdoej        dododododNdpdodq	d.ed/         dceege=dZf         dTeeqeff         deezdfedgezdhezdiezdjezdkezdle1dmezd'efdr            Zd.d/dCeelef         dcee=edZf         dTefdle1dseldfedkezd[eed         dmezd'edfdtZeCdpduej        dNddodvd.ed/         dceege=dZf         dTefdle1dseldfedkezd[eed         dmezd'efdw            Zd.ed/         dxeel         dyeel         dzeek         daed'eeqeeeeqelf                           f         fd{Zedcd|d.d/d'efd}            Z G d~ d|eK          Z eTdddcg           G d deNe                      Z eTdddcg           G d deJe                      Z eTdddcgdd           G d deMe                      Z eTddcdgd           G d de                      Z eTddcdgd           G d de                      ZdS )aS  
Dask extensions for distributed training
----------------------------------------

See :doc:`Distributed XGBoost with Dask </tutorials/dask>` for simple tutorial.  Also
:doc:`/python/dask-examples/index` for some examples.

There are two sets of APIs in this module, one is the functional API including
``train`` and ``predict`` methods.  Another is stateful Scikit-Learner wrapper
inherited from single-node Scikit-Learn interface.

The implementation is heavily influenced by dask_xgboost:
https://github.com/dask/dask-xgboost

Optional dask configuration
===========================

- **coll_cfg**:
    Specify the scheduler address along with communicator configurations. This can be
    used as a replacement of the existing global Dask configuration
    `xgboost.scheduler_address` (see below). See :ref:`tracker-ip` for more info. The
    `tracker_host_ip` should specify the IP address of the Dask scheduler node.

  .. versionadded:: 3.0.0

  .. code-block:: python

    from xgboost import dask as dxgb
    from xgboost.collective import Config

    coll_cfg = Config(
        retry=1, timeout=20, tracker_host_ip="10.23.170.98", tracker_port=0
    )

    clf = dxgb.DaskXGBClassifier(coll_cfg=coll_cfg)
    # or
    dxgb.train(client, {}, Xy, num_boost_round=10, coll_cfg=coll_cfg)

- **xgboost.scheduler_address**: Specify the scheduler address

  .. versionadded:: 1.6.0

  .. deprecated:: 3.0.0

  .. code-block:: python

      dask.config.set({"xgboost.scheduler_address": "192.0.0.100"})
      # We can also specify the port.
      dask.config.set({"xgboost.scheduler_address": "192.0.0.100:12345"})

    N)defaultdict)contextmanager)partialupdate_wrapper)Thread)Any	AwaitableCallableDict	GeneratorIterableListOptional	ParamSpecSequenceSetTuple	TypeAlias	TypedDict	TypeGuardTypeVarUnion)array)bag)	dataframe)Delayed)Future   )
collectiveconfig)
Categories)FeatureNamesFeatureTypesIterationRange)TrainingCallback)Config)_Args)_ArgVals)_is_cudf_df)BoosterDMatrixMetric	ObjectiveXGBoostError_check_distributed_params_deprecate_positional_args_expect)_is_cudf_ser_is_cupy_alike)XGBClassifierXGBClassifierBaseXGBModel	XGBRankerXGBRankerMixInXGBRegressorBase_can_use_qdm_check_rf_callback_cls_predict_proba_objective_decorator_wrap_evaluation_matricesxgboost_model_doc)RabitTracker)train   )_get_dmatricesno_group_split)_DASK_2024_12_1_DASK_2025_3_0get_address_from_userget_n_threads_DaskCollection_DataTTrainReturnTboosterhistory)CommunicatorContextDaskDMatrixDaskQuantileDMatrixDaskXGBRegressorDaskXGBClassifierDaskXGBRankerDaskXGBRFRegressorDaskXGBRFClassifierrA   predictinplace_predictz[xgboost.dask]	n_workersaddrstimeoutreturnc           	         i }	 t          |d         t                    r5|d         d         }|d         d         }t          | ||d|dn|          }n8|d         }t          |t                    s|J t          | |d|dn|          }|                                 t          |j                  }d|_        |                                 |                    |	                                           n# t          $ r}	t          |          dk     r t                              d	t          |d                   t          |d                   t          |	                     t          | |dd          |          }Y d }	~	nd }	~	ww xY w|S )
Nr   rB   task)rY   host_ipportsortbyr[   )rY   r_   ra   r[   )targetTr   zCFailed to bind address '%s', trying to use '%s' instead. Error:
 %s)
isinstancetupler@   strstartr   wait_fordaemonupdateworker_argsr.   lenLOGGERwarning_try_start_tracker)
rY   rZ   r[   envr_   r`   rabit_trackeraddrthreades
             [/var/www/html/bet.cuttalo.com/ml/venv/lib/python3.11/site-packages/xgboost/dask/__init__.pyrn   rn      s   
 C%@eAh&& 	AhqkG8A;D(#$_'  MM 8DdC((8DLLL(#$_'	  M 	}5666

=,,..//// 	@ 	@ 	@u::>>RaMMaMMFF		
 	
 	
 !E!""Iw??	@ Js   C3C8 8
F	A=FF	addr_from_daskaddr_from_userc                 ,    t          | ||g|          }|S )z8Start Rabit tracker, recurse to try different addresses.)rn   )rY   ru   rv   r[   ro   s        rt   _start_trackerrx      s     Y(H'
R
RCJ    c                   ,     e Zd ZdZdeddf fdZ xZS )rO   zNA context controlling collective communicator initialization and finalization.argsr\   Nc                      t                      j        di | t          j                    }d|j         d|j         | j        d<   d S )Nz[xgboost.dask-z]:DMLC_TASK_ID )super__init__distributed
get_workernameaddressr{   )selfr{   worker	__class__s      rt   r   zCommunicatorContext.__init__   sW      4   '))
 %UV[$T$TFN$T$T	.!!!ry   )__name__
__module____qualname____doc__CollArgsValsr   __classcell__r   s   @rt   rO   rO      s]        XXU| U U U U U U U U U U Ury   rO   clientdistributed.Clientc                 \   t          | t          t          j                              t          d          f          sWt	          t          t          t          j                              t          d          gt          |                               | t          j                    n| }|S )z#Simple wrapper around testing None.N)rc   typer   
get_client	TypeErrorr1   )r   rets     rt   _get_clientr      s    ftK$:$<$<==tDzzJKK 
T+02233T$ZZ@$v,,OO
 
 	
 '-n+
 
"
"
"&CJry   c            #          e Zd ZdZe	 ddddddddddddddded         dedee         d	ee         d
ee         dee         de	dee
         dee         dee         dee         dee         dee         dee         de	ddf d            Zded         fdZdddddddddddedee         dee         d
ee         dee         dee         dee         dee         dd fdZdedeeef         fdZdefdZdS ) rP   aq  DMatrix holding on references to Dask DataFrame or Dask Array.  Constructing a
    `DaskDMatrix` forces all lazy computation to be carried out.  Wait for the input
    data explicitly if you want to see actual computation of constructing `DaskDMatrix`.

    See doc for :py:obj:`xgboost.DMatrix` constructor for other parameters.  DaskDMatrix
    accepts only dask collection.

    .. note::

        `DaskDMatrix` does not repartition or move data between workers.  It's the
        caller's responsibility to balance the data.

    .. note::

        For aligning partitions with ranking query groups, use the
        :py:class:`DaskXGBRanker` and its ``allow_group_split`` option.

    .. versionadded:: 1.0.0

    Parameters
    ----------
    client :
        Specify the dask client used for training.  Use default client returned from
        dask if it's set to None.

    NF)weightbase_marginmissingsilentfeature_namesfeature_typesgroupqidlabel_lower_boundlabel_upper_boundfeature_weightsenable_categoricalr   r   datalabelr   r   r   r   r   r   r   r   r   r   r   r   r\   c                4   t          |          }|| _        |	| _        t          |	t                    rt          d          ||nt          j        | _        || _	        ||t          d          |
t          d          t          |j                  dk    rt          d|j                   t          |t          j        t           j        f          s@t          t%          t          j        t           j        ft'          |                              t          |t          j        t           j        t          j        t'          d           f          sKt          t%          t          j        t           j        t          j        ft'          |                              |j        d         | _        t          | j        t,                    sJ t/          t0                    | _        d| _        |                    | j        |||||||||
  
        | _        d S )	NzFThe Dask interface can handle categories from DataFrame automatically.z$per-group weight is not implemented.z4group structure is not implemented, use qid instead.r   z$Expecting 2 dimensional input, got: rB   F)	r   r   r   weightsr   r   r   r   r   )r   r   r   rc   r!   r   numpynanr   r   NotImplementedErrorrk   shape
ValueErrordd	DataFramedaArrayr1   r   Series_n_colsintr   list
worker_mapis_quantilesync_map_local_data_init)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                   rt   r   zDaskDMatrix.__init__&  s   ( V$$**mZ00 	X   #*"5ww59"4?v1%&LMMM%F   tz??aPDJPPQQQ$rx 899 	KGR\28$<d4jjIIJJJ%",")T$ZZ!PQQ 	WGR\28RY$GeUUVVVz!}$,,,,,,3>t3D3D!&[[ #+// ! 
 



ry   )NNrP   c                 4    | j                                         S N)r   	__await__r   s    rt   r   zDaskDMatrix.__await__f  s    z##%%%ry   )r   r   r   r   r   r   r   r   c       	   
         !K   dt           t                   dt          dt           t                   dt          dt          f
ddt          dt           t                   ffd!d	t
          t                   dt
          t           t                            f!fd
}
 !|           |
|          } |
|          } |
|          } |
|          } |
|          } |
|	          }di dt
          t           t                            dt          ddf fd} ||d            ||d            ||d            ||d            ||d            ||d           g }t          t                              D ]>}i }                                 D ]\  }}||         ||<   |	                    |           ?t          t          t          j        |                    }                    |          }t          j        |           d{V  |D ]}|j        dk    sJ |j                    i | _        t'          |          D ]\  }}|| j        |j        <   d |D             }j                            d |D                        d{V }t/          t                    }|                                D ]@\  }}|t1          t3          |                             	                    ||                    A|| _        |d| _        n2                    |                                           d{V | _        | S )z Obtain references to local data.left	left_nameright
right_namer\   c           
      X    d| d| dt          |            dt          |           d	}|S )NzPartitions between z and z are not consistent: z != z/.  Please try to repartition/rechunk your data.rk   )r   r   r   r   msgs        rt   inconsistentz1DaskDMatrix._map_local_data.<locals>.inconsistentx  s`    @i @ @j @ @"4yy@ @.1%jj@ @ @ 
 Jry   dc                                         |           } t          | j        d          rBt          | j        j                  dk    r%| j        j        d         dk    rt          d                              |           S )zBreaking data into partitions.r   rB   zData should be partitioned by row. To avoid this specify the number of columns for your dask Array explicitly. e.g. chunks=(partition_size, -1]))persisthasattr
partitionsrk   r   r   
futures_of)r   r   s    rt   
to_futuresz/DaskDMatrix._map_local_data.<locals>.to_futures  s    q!!Ag..
*++a//L&q)A-- 4   $$Q'''ry   metac                 &    |  |           }|S d S r   r~   )r   
meta_partsr   s     rt   flatten_metaz1DaskDMatrix._map_local_data.<locals>.flatten_meta  s#    +5:d+;+;
!!4ry   r   m_partsr   Nc                     | =t                    t          |           k    sJ  d| |                      | |<   d S d S )NXr   )r   r   X_partsr   partss     rt   append_metaz0DaskDMatrix._map_local_data.<locals>.append_meta  sZ    "7||s7||333\\S'46 6333 &d	 #"ry   r   r   r   r   r   r   finishedc                     i | ]
}|j         |S r~   key.0parts     rt   
<dictcomp>z/DaskDMatrix._map_local_data.<locals>.<dictcomp>  s    AAAtDHdAAAry   c                     g | ]	}|j         
S r~   r   r   s     rt   
<listcomp>z/DaskDMatrix._map_local_data.<locals>.<listcomp>  s    111t$(111ry   )keys)r   r   re   rI   r   r   rangerk   itemsappendr   mapdaskdelayedcomputer   waitstatuspartition_order	enumerater   	schedulerwho_hasr   nextiterr   r   result)"r   r   r   r   r   r   r   r   r   r   r   y_partsw_partsmargin_parts	qid_partsll_partslu_partsr   packed_partsi	part_dictr   valuedelayed_parts	fut_partsr   key_to_partitionr   r   workersr   r   r   r   s"    `                            @@@@rt   r   zDaskDMatrix._map_local_datai  s     	s)	(+	48I	KN		 	 	 		(/ 	(d6l 	( 	( 	( 	( 	( 	( 	x8 	Xd6l=S 	 	 	 	 	 	 *T"",u%%,w''#|K00 L%%	< 122< 122*0'):	&$v,!7 	&s 	&t 	& 	& 	& 	& 	& 	& 	& 	& 	GW%%%GX&&&L-000Iu%%%H1222H1222
 13s7||$$ 	+ 	+A+-I#kkmm * *
U!&q	#	**** (,Cl,K,K'L'L #).."?"?	y))))))))) 	: 	:D;*,,,dk,,,,  " ++ 	/ 	/GAt-.D **AAyAAA4:4D4L4L11y111 5M 5
 5
 /
 /
 /
 /
 /
 /
 /:$.?.?
#MMOO 	J 	JLCtDMM**+223CC3HIIII$"#'D  )/)H)H)O)O)Q)Q#Q#Q#Q#Q#Q#QD ry   worker_addrc           	          | j         | j        | j        | j        | j        | j                            |d          | j        dS )z\Create a dictionary of objects that can be pickled for function
        arguments.

        N)r   r   r   r   r   r   r   )r   r   r   r   r   r   getr   )r   r  s     rt   _create_fn_argszDaskDMatrix._create_fn_args  sK     "/!/#3|"&"9_((d;;+
 
 	
ry   c                     | j         S )zxGet the number of columns (features) in the DMatrix.

        Returns
        -------
        number of columns
        )r   r   s    rt   num_colzDaskDMatrix.num_col  s     |ry   r   )r   r   r   r   r0   r   rJ   rI   floatboolr"   r#   r   r   r   r   re   r   r   r  r   r  r~   ry   rt   rP   rP   	  s        6  
 ,0	=
 -115#'0404+/)-7;7;59#(#=
 =
 =
-.=
 =
 (	=
 )=
 o.=
 %=
 =
  -=
  -=
 (=
 o&=
 $O4=
 $O4=
  "/2!=
" !#=
$ 
%=
 =
 =
  =
~&9%>? & & & & ,0-115)-597;7;x x x %x 	x
 (x /*x o.x o&x "/2x $O4x $O4x 
x x x xt
3 
4S> 
 
 
 
      ry   rP   _MapRetT_Pfuncrefsr   c          
        K   t          |           } g }|D ]}g }|D ]U}t          |t                    r)|                    |                    |                     @|                    |           Vdt
          dt          j        dt          j        dt          t                   ffd} | j        t          t          ||          |          g|R d|gdd}	|                    |	           d	t          t          t                            dt          t                   fd
}
t!          j        |          }|                    |
|
           d{V }	|                     |	                                           d{V }|S )z.Map a function onto partitions of each worker._addressr{   kwargsr\   c                     t          j                    }|j        | k    rt          d|j         d|  d           |i |gS )NzInvalid worker address: z, expecting zz. This is likely caused by one of the workers died and Dask re-scheduled a different one. Resilience is not yet supported.)r   r   r   r   )r  r{   r  r   r  s       rt   fnz!map_worker_partitions.<locals>.fn  st     +--F~)) Uv~ U U8 U U U   D$)&))**ry   FT)purer   allow_other_workersresultsc                     | D ]}||c S 	d S r   r~   )r  vs     rt   first_validz*map_worker_partitions.<locals>.first_valid:  s)     	 	A} try   N)r   rc   rP   r   r  re   r
  r{   r  r   r	  submitr   r   r   r   dbfrom_delayed	reductionr   r   )r   r  r   r  futuresrq   r{   refr  futr  r   r   s    `           rt   map_worker_partitionsr     s        FG - - 	! 	!C#{++ !C//556666C    
	+ 
	+RW 
	+	 
	+d8n 
	+ 
	+ 
	+ 
	+ 
	+ 
	+: fm72t,,b11

 
 F $
 
 
 	sXhx&89 hx>P     /'
"
"Ck;77
7
7
7
7
7
7C>>#&&--////////FMry   c            )           e Zd ZdZe	 dddddddddddddddddded         dedee         d	ee         d
ee         dee         de	dee
         deeeee         f                  dee         dee         dee         dee         dee         dee         dee         de	dee         ddf& fd            Zdedeeef         f fdZ xZS )rQ   zmA dask version of :py:class:`QuantileDMatrix`. See :py:class:`DaskDMatrix` for
    parameter documents.

    NF)r   r   r   r   r   r   max_binr  r   r   r   r   r   r   max_quantile_batchesr   r   r   r   r   r   r   r   r   r   r!  r  r   r   r   r   r   r   r"  r\   c                    t                                          ||||||||||||||	|           |
| _        || _        d| _        |t          |          nd | _        d S )N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   T)r   r   r!  r"  r   id_ref)r   r   r   r   r   r   r   r   r   r   r!  r  r   r   r   r   r   r   r"  r   s                      rt   r   zDaskQuantileDMatrix.__init__M  s    . 	#//+''1 	 	
 	
 	
" $8!.1o2c7774			ry   r  c                     t                                          |          }| j        |d<   | j        |d<   | j        
| j        |d<   |S )Nr!  r"  r  )r   r  r!  r"  r%  )r   r  r{   r   s      rt   r  z#DaskQuantileDMatrix._create_fn_argsz  sK    ww&&{33,Y'+'@#$9 )DKry   r   )r   r   r   r   r0   r   rJ   rI   r  r  r"   r   r   r   r   rP   r   re   r   r  r   r   s   @rt   rQ   rQ   G  s        
  
 ,0	*H -115#'049=!%%)+/)-7;7;59#(.2)*H *H *H-.*H *H (	*H )*H o.*H %*H *H  -*H  c49n 56*H #*H k"*H (*H o&*H  $O4!*H" $O4#*H$ "/2%*H& !'*H( 'sm)*H* 
+*H *H *H *H *H  *HX3 4S>          ry   rQ   dconfigcoll_cfgc                   K   |t                      n|}d}d}t          ||          \  }}|||f}nd}	 t          j                            | j        j                  }|                    d          }n# t          $ r d}Y nw xY w| 	                    t          ||||j                   d{V }|                    |          }|J |S )zBGet rabit context arguments from data distribution in DaskDMatrix.Nr   z/:)
CollConfigrG   r   commget_address_hostr   r   strip	Exceptionrun_on_schedulerrx   tracker_timeoutget_comm_config)	r   rY   r'  r(  r_   r`   	user_addr
sched_addrro   s	            rt   _get_rabit_argsr4    s       (/z|||XH!GD)'8<<MGTdO			 %66v7G7OPP
%%d++

   


 ''	:y(:R       C 
"
"3
'
'C???Js   >A5 5BBc                  D    t           j                            dd           S )Nxgboost)default)r   r    r  r~   ry   rt   _get_dask_configr8    s    ;??9d?333ry   dtrainevalsc                    t          | j                                                  }|r|D ]}t          |          dk    sJ t	          |d         t
                    rt	          |d         t                    sJ |d         | u rZt          |d         j                                                  }|                    |          }t          |          S )Nr   r   rB   )	setr   r   rk   rc   rP   re   unionr   )r9  r:  X_worker_maprs   r   s        rt   _get_workers_from_datar?    s     !!2!7!7!9!9::L : 	: 	:Aq66Q;;;;adK00JZ!c5J5JJJJtv~~QqT_113344J'--j99LLry   c                    K   |j                                          d {V }|d                                         }t          |           |z
  }|rt	          d|           d S )Nr   zMissing required workers: )r   identityr   r<  RuntimeError)r   r   infocurrent_workersmissing_workerss        rt   _check_workers_are_aliverF    s       !**,,,,,,,,D9o**,,O'll_4O KIIIJJJK Kry   global_configparamsnum_boost_roundobjearly_stopping_roundsverbose_eval	xgb_model	callbackscustom_metricc                   	
K   t          ||          }t          ||            d {V  t          | t          |          ||           d {V }t	          |           dt
          dt
          t          t          t          t          f         f         dt          dt          t                   dt          t                   dt          dt          d	t          t                   f	
fd
}t          j        ||           4 d {V  |%d |D             }d |D             }d |D             }ng }g }g }t          | |||t!          |          ||g|g|z   R d|i d {V }|cd d d           d {V  S # 1 d {V swxY w Y   d S )N)r'  r(  
parameters	coll_argstrain_id
evals_nameevals_id	train_refr  r\   c                 >   t          j                    }|                                 }t          ||          }	|                    |	|	d           i }
                    d|	i           t          di |5  t          j        di 5  t          ||g|R |||	d\  }}t          |||
t          |          dk    r|nd           }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                                dk    r||
d}nd }|S )N)nthreadn_jobsrX  )rU  rT  	n_threadsmodelr   )rH  r9  rI  evals_resultr:  rJ  rO  rK  rL  rM  rN  rL   r~   )r   r   copyrH   ri   rO   r    config_contextrC   worker_trainrk   num_row)rQ  rR  rS  rT  rU  rV  r  r   local_paramrZ  local_historyXyr:  rM   r   rN  rO  rK  rG  rI  rJ  rL  rM  s                  rt   do_trainz_train_async.<locals>.do_train  s    ')) oo''!+v66	yIFFGGG35i3444 --9-- 	 	v/D/U/U}/U/U 	 	&	 	 	 "%#	 	 	IB #" /*"5zzQeeD+&;)##  G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	8 ::<<1"(+ +CC
 C
s7   6C2ACC2C	C2"C	#C22C69C6c                     g | ]\  }}|S r~   r~   r   r   ns      rt   r   z _train_async.<locals>.<listcomp>"      ...1!...ry   c                     g | ]\  }}|S r~   r~   rf  s      rt   r   z _train_async.<locals>.<listcomp>#  rh  ry   c                 ,    g | ]}t          |          S r~   )r$  )r   r   s     rt   r   z _train_async.<locals>.<listcomp>$  s    222!1222ry   r   )r?  rF  r4  rk   r/   r   re   r   r   r   dictr   rK   r   	MultiLockr  r$  )r   rG  r'  rH  r9  rI  r:  rJ  rK  rL  rM  rN  rO  r(  r   rR  rd  
evals_datarT  rU  r   s    `   ` ``````        rt   _train_asyncrn    s     " %VU33G
"7F
3
33333333%Gg        I f%%%44U38_,-4 4 I	4
 s)4 4 4 
,	4 4 4 4 4 4 4 4 4 4 4 4 4l $Wf55        .....J.....J22z222HHJJH,vJJ
 h#
 
 
 
 
 
 
 
 
 
 
 /                             s   AE::
FF
   T)r:  rJ  rK  rM  rL  rN  rO  r(  c                    t          |           }  | j        t          ft          j                    t                      dt                      S )a  Train XGBoost model.

    .. versionadded:: 1.0.0

    .. note::

        Other parameters are the same as :py:func:`xgboost.train` except for
        `evals_result`, which is returned as part of function return value instead of
        argument.

    Parameters
    ----------
    client :
        Specify the dask client used for training.  Use default client returned from
        dask if it's set to None.

    coll_cfg :
        Configuration for the communicator used during training. See
        :py:class:`~xgboost.collective.Config`.

    Returns
    -------
    results: dict
        A dictionary containing trained booster and evaluation history.  `history` field
        is the same as `eval_result` from `xgboost.train`.

        .. code-block:: python

            {'booster': xgboost.Booster,
             'history': {'train': {'logloss': ['0.48253', '0.35953']},
                         'eval': {'logloss': ['0.480385', '0.357756']}}}

    )rG  r'  )r   r   rn  r    
get_configr8  locals)r   rH  r9  rI  r:  rJ  rK  rM  rL  rN  rO  r(  s               rt   rA   rA   :  sX    b   F6;')) ""  ((	  ry   is_dfoutput_shapec                 ,    | ot          |          dk    S Nr   r   )rs  rt  s     rt   _can_output_dfrw  t  s    +S&&!++ry   r   
predictioncolumnsc                    t          ||j                  rt          | dd          }t          |           rUddl}|j        dk    r"|                    i |t          j                  S |                    ||t          j        |          }nUddl	}|j        dk    r#|                    i |t          j        |          S |                    ||t          j        |          }|S )z0Return dataframe for prediction when applicable.indexNr   )ry  dtype)ry  r|  r{  )
rw  r   getattrr)   cudfsizer   r   float32pandas)r   rx  ry  rs  r{  r~  pds          rt   _maybe_dataframer  x  s     eZ-.. 
 gt,,t 	KKK!##~~b'~OOOG5= (  JJ  !##||u}E $    G5= &  J ry   mapped_predictrM   zdistributed.Futurer   .r   c                   K   t          |                                          }t          |          dk    r)t          |t          j                  rt          d          t          t          |t          j                  |          r|/t          |t          j	                  r|
                                }n|}t	          j        | ||d||t          j                            |                    }t          |          dk    r|j        d d df         }n?|;t          |t          j        t          j        f          r|                                }	n|}	t          |          dk    rdg}
g }nrg }
t          |t          j                  r-t#          t%          t          |          dz
                      }n)d t%          t          |          dz
            D             }t          |          dk    r8t#          |j                  }t          |t"                    sJ |d         f|d<   nd }t          j        | ||d	||	||
|t*          j        

  
        }|S )N   zGUse `da.Array` or `DaskDMatrix` when output has more than 2 dimensions.T)r   rB   r   r   c                     g | ]}|d z   S )r   r~   )r   r   s     rt   r   z(_direct_predict_impl.<locals>.<listcomp>  s    HHHaAEHHHry   F)chunks	drop_axisnew_axisr|  )rd   r   rk   rc   r   r   r   rw  r   r   to_dask_dataframemap_partitionsutils	make_metailocr   to_dask_arrayr   r   r  
map_blocksr   r  )r  rM   r   r   rt  r   ry  base_margin_dfpredictionsbase_margin_arrayr  r  r  s                rt   _direct_predict_implr    sn      DIIKK  G
<A*T2<"@"@ U
 
 	
 jr|44lCC >
"z+rx'H'H" --// N )N'##D))
 
 
 |!!%*111a40K"z")R\2(
 (
" 5@4M4M4O4O + |!!01sI.0HHI$-- Ic,&7&7!&; < <==HH5\1B1BQ1F+G+GHHH|!! -1,=,=Ffd+++++%a*F1IIFm-
 
 
 ry   featuresinplacer  c                     t          |t                    sJ t          j                            d          }|                    d|          }|r2|                                }|                    d          dk    rd|d<   t          |d          } | j	        |fdd	i|}t          |j                  dk    r|j        d         nd}	i }
t          ||j                  rt          |	          D ]}d
|
|<   |j        |
fS )z@Create a dummy test sample to infer output shape for prediction.i  rB   predict_typemarginToutput_margin)r   validate_featuresFf4)rc   r   r   randomRandomStaterandnr]  popr+   rW   rk   r   rw  r   )rM   r  rs  r  r  rngtest_samplem
test_predt	n_columnsr   r   s               rt   _infer_predict_outputr    s    h$$$$$
,
"
"4
(
(C))Ax((K +::n%%11&*F?#555A FFeFvFFJ'*:+;'<'<q'@'@
 ##aIDeZ-.. y!! 	 	ADGGT!!ry   r[  c                   K   t          |t                    r|                     |d           d {V }nt          |t                    r$|                     |d         d           d {V }nt          |t          j                  r%|}|j        }|t          urt          d|           nAt          t          t          t          t          j        gt          |                              |S )NF)hashrM   z9Underlying type of model future should be `Booster`, got )	rc   r*   scatterrk  r   r   r   r   r1   )r   r[  rM   ts       rt   _get_model_futurer    s      %!! Su599999999	E4	 	  
SuY'7eDDDDDDDD	E;-	.	. SLGOAOO   
 $0B CT%[[QQRRRNry   r  r   	pred_leafpred_contribsapprox_contribspred_interactionsr  iteration_rangestrict_shapec       	         t  	
%&'K   t          | |           d {V }t          |t          t          j        t
          j        f          sFt          t          t          t          j        t
          j        gt          |                              dt          dt          dt          dt          t                   dt          dt          f	
f
d}t          |t          j        t
          j        f          r|                     |                     t"          ||j        d         t          |t
          j                  d		
                     d {V \  }}t'          |||d ||           d {V S |                     |                     t"          ||                                d	d		                     d {V \  }}|j        '|j        %|j        &|j        dt          dt2          t4          t          f         dt6          j        f%&	
fd}g }g }g }g }t;          |j                                                  }|D ]k}|j        |         }|                     |           |                     tC          |          |gz             |                     'fd|D                        ltE          ||          D ]4\  }}|                     d ||g          }|#                    |           5t;          tE          ||||                    }tI          |d           }d |D             }d |D             }d |D             }g }tE          ||          D ]4\  }}|                     ||||g          } |#                    |            5g }!| %                    |           d {V }tM          |          D ]K\  }"}#|!#                    t          j'        ||"         |#f|dd          z   t6          j(                             Lt          j)        |!d          }$|$S )NrM   	partitionrs  ry  _r\   c                    
 t          j        di 5  t          |
d          }|                     |		  	        }t	          ||||          }|cd d d            S # 1 swxY w Y   d S )NT)r   r   r   )	r   r  r  r  r  r  r  r  r  r~   )r    r^  r+   rW   r  )rM   r  rs  ry  r  r  predtr  rG  r  r   r  r  r  r  r  r  s          rt   r  z&_predict_async.<locals>.mapped_predict0  s     "33]33 	 	#'  A
 OO+#+ /"3"3 /) $ 
 
E %YwFFE%	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA$$A(+A(rB   F)	r  rs  r  r  r  r  r  r  r  r  rM   r   r   rt  r   )
rM   r  rs  r  r  r  r  r  r  r  r   c                     |d         }|                     dd           }t          j        di 	5  t          ||d          }|                     |
	  	        }|cd d d            S # 1 swxY w Y   d S )Nr   r   T)r   r   r   r   r   )r  r  r  r  r  r  r  r  r~   )r  r    r^  r+   rW   )rM   r   r   r   r  r  r  r   r   rG  r  r   r  r  r  r  r  r  s         rt   dispatched_predictz*_predict_async.<locals>.dispatched_predictv  s    F|hh}d33"33]33 	 	'++#'  A OO+#+ /"3"3 /) $ 
 
E )	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   5A33A7:A7c                 *    g | ]}|j                  S r~   r   )r   r   r   s     rt   r   z"_predict_async.<locals>.<listcomp>  s     OOO?484OOOry   c                 (    | d         j         d         S )Nr   r   )r   )r   s    rt   <lambda>z _predict_async.<locals>.<lambda>  s    tF|'9!'< ry   )r   c                     | d         S rv  r~   )ps    rt   r  z _predict_async.<locals>.<lambda>  s
    ad ry   r   c                     g | ]	\  }}}}|
S r~   r~   r   r   r   orderws        rt   r   z"_predict_async.<locals>.<listcomp>  s!    DDD/$uaDDDry   c                     g | ]	\  }}}}|
S r~   r~   r  s        rt   r   z"_predict_async.<locals>.<listcomp>  s!    FFF1D%%FFFry   c                     g | ]	\  }}}}|
S r~   r~   r  s        rt   r   z"_predict_async.<locals>.<listcomp>  s!    CCC.ueQ1CCCry   )r   r|  r   axis)*r  rc   rP   r   r   r   r   r   r1   r   r*   r   r  r   r   r   r  r  r   r  r  r   r   r   r   r   re   r   ndarrayr   r   r   extendrk   zipr   sortedgatherr   r  r  concatenate)(r   rG  r[  r   r  r   r  r  r  r  r  r  r  _boosterr  _output_shaper   rt  r  r  	all_parts
all_orders
all_shapesall_workersworkers_addressr  list_of_partsr  r   sparts_with_orderr  farraysr   rowsr  r   r   r   s(    `  `````````                        @@@rt   _predict_asyncr    s\       'vu55555555Hd["(BLABB Tbh EtDzzRRSSS%(15@DS	NQ	              0 $2<011 
$*NNMM%A r|44+#+ /"3)   %
 %
 
 
 
 
 
 
t *)&
 
 
 
 
 
 
 
 
 	
 #NN!\\^^''+/% 	 	
 	
       OL!  *O&M&MlGG 4S> em                 2 IJJK4?//1122O& Q Q4'''3}--=>>>OOOOOOOPPPP{I..  4MM<<dQCMPP!C	:z;OOPP.NNCCCDD3CDDDIFF5EFFFJCC2BCCCKG{I..  4MM,hqcMJJq F}}Z00000000JZ(( 
 
4O
4'L,<"<EM  	
 	
 	
 	

 .a000Kry   F)r   r   )	r  r   r  r  r  r  r  r  r  c       	             t          |           }  | j        t          fdt          j                    it                      S )a  Run prediction with a trained booster.

    .. note::

        Using ``inplace_predict`` might be faster when some features are not needed.
        See :py:meth:`xgboost.Booster.predict` for details on various parameters.  When
        output has more than 2 dimensions (shap value, leaf with strict_shape), input
        should be ``da.Array`` or ``DaskDMatrix``.

    .. versionadded:: 1.0.0

    Parameters
    ----------
    client:
        Specify the dask client used for training.  Use default client
        returned from dask if it's set to None.
    model:
        The trained model.  It can be a distributed.Future so user can
        pre-scatter it onto all workers.
    data:
        Input data used for prediction.  When input is a dataframe object,
        prediction output is a series.
    missing:
        Used when input data is not DaskDMatrix.  Specify the value
        considered as missing.

    Returns
    -------
    prediction: dask.array.Array/dask.dataframe.Series
        When input data is ``dask.array.Array`` or ``DaskDMatrix``, the return value is
        an array, when input data is ``dask.dataframe.DataFrame``, return value can be
        ``dask.dataframe.Series``, ``dask.dataframe.DataFrame``, depending on the output
        shape.

    rG  )r   r   r  r    rq  rr  )r   r[  r   r  r   r  r  r  r  r  r  r  s               rt   rW   rW     s>    f   F6;~UUV5F5H5HUFHHUUUry   r  c        
           	K   t          |           } t          | |           d {V }
t          |t          j        t
          j        f          s@t          t          t          j        t
          j        gt          |                              ||t          |t          j        t
          j        t
          j
        f          sKt          t          t          j        t
          j        t
          j
        gt          |                              dt          dt          dt          dt          t                   dt          dt          f	fd}|                     |                     t$          |
|j        d         t          |t
          j                  d		
                     d {V \  }}t)          ||
||||           d {V S )NrM   r  rs  ry  r   r\   c           
          t          j        di 5  |                     |	|
          }d d d            n# 1 swxY w Y   t          ||||          }|S )N)r  r  r   r   r  r  r~   )r    r^  rX   r  )rM   r  rs  ry  r   rx  rG  r  r   r  r  r  s         rt   r  z._inplace_predict_async.<locals>.mapped_predict  s     "33]33 		 		 00 /)'"3) 1  J		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 &iWeLL
s   <A A rB   T)r  rs  r  r  r  r  r  )r   r  rc   r   r   r   r   r   r1   r   r   r*   r   r  r   r   r   r  r  r   r  )r   rG  r[  r   r  r  r   r  r   r  rM   r  r   r   s    `  ```` `    rt   _inplace_predict_asyncr    s        F%fe44444444GdRXr|455 G2< 8$t**EEFFFzrxry1( ( 2< CT+EVEVWWXXX  c	
  
          * !Z]T2<00%+% 	 		
 		
       KE4 &%         ry   r   )r  r  r   r  r   r  c                    t          |           }  | j        t          fdt          j                    it                      S )a  Inplace prediction. See doc in :py:meth:`xgboost.Booster.inplace_predict` for
    details.

    .. versionadded:: 1.1.0

    Parameters
    ----------
    client:
        Specify the dask client used for training.  Use default client
        returned from dask if it's set to None.
    model:
        See :py:func:`xgboost.dask.predict` for details.
    data :
        dask collection.
    iteration_range:
        See :py:meth:`xgboost.Booster.predict` for details.
    predict_type:
        See :py:meth:`xgboost.Booster.inplace_predict` for details.
    missing:
        Value in the input data which needs to be present as a missing
        value. If None, defaults to np.nan.
    base_margin:
        See :py:obj:`xgboost.DMatrix` for details.

        .. versionadded:: 1.4.0

    strict_shape:
        See :py:meth:`xgboost.Booster.predict` for details.

        .. versionadded:: 1.4.0

    Returns
    -------
    prediction :
        When input data is ``dask.array.Array``, the return value is an array, when
        input data is ``dask.dataframe.DataFrame``, return value can be
        ``dask.dataframe.Series``, ``dask.dataframe.DataFrame``, depending on the output
        shape.

    rG  )r   r   r  r    rq  rr  )	r   r[  r   r  r  r   r  r   r  s	            rt   rX   rX   .  sO    j   F 6; .4.?.A.AEKXX  ry   devicetree_methodr!  c                 F   K   dt           t                   dt          dt          f fd}t          d	d|i|\  }}| d{V }|||fS g }|D ]K}	|	d         |u r|                    |	           "|                    |	d          d{V |	d         f           L||fS )
z(A switch function for async environment.r  r  r\   c                 `    t                    rt          d| d|S t          ddi|S )N)r   r  r!  r   r~   )r:   rQ   rP   )r  r  r   r  r!  r  s     rt   	_dispatchz2_async_wrap_evaluation_matrices.<locals>._dispatchu  s[    V,, 	& 3 ;A   33&3F333ry   create_dmatrixNr   rB   r~   )r   rP   r   r>   r   )
r   r  r  r!  r  r  train_dmatrixr:  awaitedrs   s
   ````      rt   _async_wrap_evaluation_matricesr  l  s	     4x, 4 4 4 4 4 4 4 4 4 4 4 5XXIXQWXXM5'''''''M}e##G + +Q4=  NN1ad





AaD)****'!!ry   DaskScikitLearnBasec              #   H   K   	 || _         | V  d| _         dS # d| _         w xY w)z-Temporarily set the client for sklearn model.N)r   )r[  r   s     rt   _set_worker_clientr    s9      
ts    	!c                       e Zd ZdZdZdddee         deddf fdZde	d	e
d
e
dee         dee         defdZedddddde	d	e
d
e
dee         dee         defd            Z	 dde	dee         defdZ	 dde	dee         defdZdee         fdZdefdZedd            Zej        dd            ZdededefdZ xZS ) r  z<Base class for implementing scikit-learn interface with DaskN)r(  r(  r  r\   c                H     t                      j        di | || _        d S )Nr~   )r   r   r(  )r   r(  r  r   s      rt   r   zDaskScikitLearnBase.__init__  s+    ""6""" ry   r   r  r  r   r  c          
        K   |                      |          }|                                 sJ t          | j        |                                 |||rdnd| j        ||           d {V }t          |t          j                  rD|	                                }t                      r"t                      s|                                }|S )Nr  r   )r   r[  r   r  r  r   r   r  )_get_iteration_range_can_use_inplace_predictrX   r   get_boosterr   rc   r   r   r  rE   rF   r   )r   r   r  r  r   r  predtss          rt   r  z"DaskScikitLearnBase._predict_async  s       33ODD ,,.....&;""$$+%2?L#/	
 	
 	
 	
 	
 	
 	
 	
 	
 fbl++ 	*))++F    	*)9)9 	*  ))ry   FTr  r  r   r  r   c                L    | j                             | j        |||||          S )Nr  )r   r   r  )r   r   r  r  r   r  s         rt   rW   zDaskScikitLearnBase.predict  s8     {'/#+   
 
 	
ry   c                    K   |                      |          }t          | j        || j        | j                   d {V }t          | j        |                                 |d|           d {V }|S )N)r   r   r   T)r[  r   r  r  )r  rP   r   r   r   rW   r  )r   r   r  test_dmatrixr  s        rt   _apply_asyncz DaskScikitLearnBase._apply_async  s      
 33ODD*5KL,	+
 +
 +
 %
 %
 %
 %
 %
 %
 K""$$+
 
 
 
 
 
 
 
 
 ry   c                 F    | j                             | j        ||          S )N)r  )r   r   r  )r   r   r  s      rt   applyzDaskScikitLearnBase.apply  s$    
 { 11oVVVry   c                      dt           t                   f fd}                     |                                          S )Nr\   c                     K    S r   r~   r   s   rt   r  z(DaskScikitLearnBase.__await__.<locals>._  s      Kry   )r	   r   _client_syncr   )r   r  s   ` rt   r   zDaskScikitLearnBase.__await__  sN    	3 	 	 	 	 	 	   ##--///ry   c                 F    | j                                         }d|v r|d= |S )N_client)__dict__r]  )r   thiss     rt   __getstate__z DaskScikitLearnBase.__getstate__  s,    }!!##Yry   r   c                 .    t          | j                  }|S )zThe dask client used in this model.  The `Client` object can not be
        serialized for transmission, so if task is launched from a worker instead of
        directly from the client process, this attribute needs to be set at that worker.

        )r   r  )r   r   s     rt   r   zDaskScikitLearnBase.client  s     T\**ry   cltc                 4    ||j         nd| _        || _        d S )NF)asynchronous_asynchronousr  )r   r  s     rt   r   zDaskScikitLearnBase.client  s#     25S--ery   r  c                    | j         t          | dd          }	 t          j                     d}n# t          $ r d}Y nw xY w|rzt          j                    5 }t          | |          5 } |j        j        |fi |d|i}|cddd           cddd           S # 1 swxY w Y   |cddd           S # 1 swxY w Y    | j        j        |fi |d| j        j	        iS )zGet the correct client, when method is invoked inside a worker we
        should use `worker_client' instead of default client.

        Nr  FTr  )
r  r}  r   r   r   worker_clientr  r   r   r  )r   r  r  r  	in_workerr   r  r   s           rt   r  z DaskScikitLearnBase._client_sync  s    <"4%@@L"&((( 		 " " "!			"  .00 F+D&99 #T.dk.  $* 9E    #	# # # # # # #       # # # # # # # #
                  t{VVVVT[=UVVVVsB   0 ??B3(BB3B	B3!B	"B33B7:B7r   )r\   r   )r  r   r\   N)r   r   r   r   r  r   r*  r   r   rJ   r  rI   r$   r  r0   rW   r  r  r	   r   r   r  propertyr   setterr
   r  r   r   s   @rt   r  r    s|       FFG;? ! ! !HZ$8 !3 !SW ! ! ! ! ! !
%% 	%
  % o.% ".1% 
% % % %N  
 $"&1548
 
 

 	

  
 o.
 ".1
 

 
 
  
* 59  ".1 
	   0 59W WW ".1W 
	W W W W09S> 0 0 0 0d        X ]   ]W WS WS W W W W W W W Wry   z3Implementation of the Scikit-Learn API for XGBoost.
estimatorsc                      e Zd ZdZdededee         dee         deeeeef                           deee                  deee                  d	e	e
ef         d
ee	eef                  dee         defdZeddddddddddededee         dee         deeeeef                           d	ee	e
ef                  d
ee	eeef                  deee                  deee                  dee         dd fd            ZdS )rR   zAdummy doc string to workaround pylint, replaced by the decorator.r   ysample_weightr   eval_setsample_weight_eval_setbase_margin_eval_setverboserM  r   r\   c                  K   |                                  }|                     |	||
          \  }}}}
t          di d| j        d| j        d| j        d| j        d|d|dd dd d	|d
|d|
d|d|d|dd dd d| j        d| j        d| j	         d {V \  }}t          | j                  rt          | j                  }nd }| j                            t          d| j        t          j                    t#                      |||                                 ||||| j        | j        | j        |           d {V }|d         | _        |                     |d                    | S )Nr   r  r  r!  r   r  r   r   r  r   r   r  r  r  
eval_groupeval_qidr   r   r   Tr  r   rG  r'  rH  r9  rI  r:  rJ  rO  rL  rK  rN  r(  rM  rM   rN   r~   )get_xgb_params_configure_fitr  r   r  r  r!  r   r   r   callable	objectiver=   r   rn  r    rq  r8  get_num_boosting_roundsrK  rN  r(  _Booster_set_evaluation_resultr   r   r  r  r   r  r  r  r  rM  r   rH  r[  metricr9  r:  rJ  r  s                     rt   
_fit_asynczDaskXGBRegressor._fit_async3  sP      $$&&151D1Dv2
 2
.vv > 
 
 
;;
;;
 ((
 LL	

 a
 a
 $
 
 (-
 $
 ,O
 X
 $:#9
 "6!5
 t
  T!
" LL#
$  $66%
& ,,'
 
 
 
 
 
 
, DN## 	&:4>&J&JCCC((; +--$&& 88::  "&"<n]! ) 
 
 
 
 
 
 
 
$  	*##GI$6777ry   NTr  r   r  r  rM  r  r  r   c                |    d t                                                      D             } | j        | j        fi |S )Nc                 "    i | ]\  }}|d v	||S )r   r   r~   r   kr  s      rt   r   z(DaskXGBRegressor.fit.<locals>.<dictcomp>  )    TTTAQ>S5S5S15S5S5Sry   rr  r   r  r"  r   r   r  r  r   r  r  rM  r  r  r   r{   s               rt   fitzDaskXGBRegressor.fitw  sC     UT!1!1TTT t 99D999ry   )r   r   r   r   rJ   rI   r   r   r   r   r   r  r*   r6   r"  r0   re   r,  r~   ry   rt   rR   rR   -  s        LKAA A
  0A o.A 8E/?*J$KLMA !)/)B CA 'x'@AA sDy!A E'8"345A "/2A 
A A A AH   4815PT.2=AFJDH59: : :: :
  0: o.: 8E/?*J$KLM: %T	*+: E'3"89:: !)/)B C: 'x'@A: "/2: 
: : :  : : :ry   rR   zBImplementation of the scikit-learn API for XGBoost classification.c                       e Zd Zdededee         dee         deeeeef                           deee                  deee                  dee	e
f         d	eeeef                  d
ee         dd fdZddddddddddededee         dee         deeeeef                           deee	e
f                  d	eeeeef                  deee                  deee                  d
ee         dd fdZdede
dee         dee         def
 fdZ	 	 	 ddede
dee         dee         def
dZej        j        e_        dede
de
dee         dee         def fdZ xZS )rS   r   r  r  r   r  r  r  r  rM  r   r\   c                  K   |                                  }|                     |	||
          \  }}}}
t          | j        fi d| j        d| j        d| j        d|d|dd dd d|d	|d
|
d|d|d|dd dd d| j        d| j        d| j	         d {V \  }}t          |t          j                  r8| j                            t          j        |                     d {V | _        n7| j                            |                                           d {V | _        t#          | j                  r| j                                        | _        t'          | j                  r| j                                        | _        t+          j        | j                  | _        t/          | j                  | _        | j        dk    rd|d<   | j        |d<   nd|d<   t3          | j                  rt7          | j                  }nd }| j                            t:          d| j        t=          j                    tA                      ||| !                                ||||| j"        | j#        | j$        |           d {V }|d         | _%        t3          | j                  s|d         | _        | &                    |d                    | S )Nr  r  r!  r   r  r   r   r  r   r   r  r  r  r  r  r   r   r   r   zmulti:softprobr  	num_classzbinary:logisticTr  rM   rN   )'r  r  r  r   r  r  r!  r   r   r   rc   r   r   r   uniqueclasses_drop_duplicatesr2   to_cupyr3   r  r   r   rk   
n_classes_r  r  r=   r   rn  r    rq  r8  r  rK  rN  r(  r  r  r   s                     rt   r"  zDaskXGBClassifier._fit_async  s      $$&&151D1Dv2
 2
.vv >K
 
 
;;
 ((
 LL	

 a
 a
 $
 
 (-
 $
 ,O
 X
 $:#9
 "6!5
 t
  T!
" LL#
$  $66%
& ,,'
 
 
 
 
 
 
. a"" 	K"&+"5"5bill"C"CCCCCCCDMM"&+"5"5a6G6G6I6I"J"JJJJJJJDM&& 	4 M1133DM$-(( 	0 M--//DMDM22dm,,?Q"2F;"&/F;"3F;DN## 	&:4>&J&JCCC((; +--$&& 88::  "&"<n]! ) 
 
 
 
 
 
 
 
$  	*'' 	1#K0DN##GI$6777ry   NTr#  c                |    d t                                                      D             } | j        | j        fi |S )Nc                 "    i | ]\  }}|d v	||S r&  r~   r'  s      rt   r   z)DaskXGBClassifier.fit.<locals>.<dictcomp>  r)  ry   r*  r+  s               rt   r,  zDaskXGBClassifier.fit  sC     UT!1!1TTT t 99D999ry   r  r  c                 :  K   | j         dk    rt          d          t                                          |d|||           d {V }t	          t          t          j        d          t          j                  }t          t          | dd          ||          S )	Nzmulti:softmaxzSmulti:softmax doesn't support `predict_proba`.  Switch to `multi:softproba` insteadF)r   r  r  r   r  T)allow_unknown_chunksizesr4  r   )
r  r   r   r  r   r   r   vstackr<   r}  )r   r   r  r   r  r  r9  r   s          rt   _predict_proba_asyncz&DaskXGBClassifier._predict_proba_async  s       >_,,6   ww--/#+ . 
 
 
 
 
 
 
 
  BI===ry
 
 "'$a"@"@&&QQQry   c                 @    |                      | j        ||||          S )N)r   r  r   r  )r  r:  )r   r   r  r   r  s        rt   predict_probazDaskXGBClassifier.predict_proba  s3       %/#+ ! 
 
 	
ry   r   r  c                  K   t                                          |||||           d {V }|r|S t          |j                  dk    r|dk                        t
                    }n_t          |j                  dk    sJ t          |t          j                  sJ dt          dt          fd}t          j
        ||d          }|S )	Nr  rB   g      ?r   xr\   c                 .    |                      d          S )NrB   r  )argmax)r>  s    rt   _argmaxz1DaskXGBClassifier._predict_async.<locals>._argmax>  s    xxQx'''ry   )r  )r   r  rk   r   astyper   rc   r   r   r   r  )
r   r   r  r  r   r  
pred_probspredsrA  r   s
            rt   r  z DaskXGBClassifier._predict_async#  s      !7711'/#+ 2 
 
 
 
 
 
 
 

  	z  A%%#%--c22EEz'((A----j"(33333(3 (3 ( ( ( ( M':CCCEry   )TNN)r   r   r   rJ   rI   r   r   r   r   r   r  r*   r6   r"  re   r,  r$   r:  r   r<  r4   r   r  r   r   s   @rt   rS   rS     s+       UU U
  0U o.U 8E/?*J$KLMU !)/)B CU 'x'@AU sDy!U E'8"345U "/2U 
U U U Uz 4815PT.2=AFJDH59: : :: :
  0: o.: 8E/?*J$KLM: %T	*+: E'3"89:: !)/)B C: 'x'@A: "/2: 
: : : :"RR  R o.	R
 ".1R 
R R R R R R8 #'1548
 

  
 o.	

 ".1
 

 
 
 
 *7?M 	
   o. ".1 
         ry   rS   zZImplementation of the Scikit-Learn API for XGBoost Ranking.

    .. versionadded:: 1.4.0

aq  
    allow_group_split :

        .. versionadded:: 3.0.0

        Whether a query group can be split among multiple workers. When set to `False`,
        inputs must be Dask dataframes or series. If you have many small query groups,
        this can significantly increase the fragmentation of the data, and the internal
        DMatrix construction can take longer.

zf
        .. note::

            For the dask implementation, group is not supported, use qid instead.
)extra_parametersend_notec                        e Zd Zedddddededee         ded	df
 fd
            Z	d	e
e         f fdZdededee         dee         dee         deeeeef                           deee                  deee                  deee                  deeef         deeeef                  dee         d	d fdZeddddddddddddddededee         dee         dee         dee         deeeeef                           deee                  deee                  deeeef                  deeeeef                  deee                  deee                  dee         d	d fd            Zej        j        e_         xZS )rT   z	rank:ndcgFN)r  allow_group_splitr(  r  rH  r(  r  r\   c                    t          |          rt          d          || _         t                      j        d||d| d S )Nz5Custom objective function not supported by XGBRanker.)r  r(  r~   )r  r   rH  r   r   )r   r  rH  r(  r  r   s        rt   r   zDaskXGBRanker.__init__^  sW     I 	VTUUU!2J9xJJ6JJJJJry   c                 r    t                                                      }|                    d           |S )NrH  )r   _wrapper_paramsaddr   rH  r   s     rt   rK  zDaskXGBRanker._wrapper_paramsl  s0    ((**

&'''ry   r   r  r   r  r   r  r  r  r  r  rM  r   c       
         h  K   |                                  }|                     |||          \  }}}}t          | j        fi d| j        d| j        d| j        d|d|dd d|d|d	|d
|d|d|d|dd d|	d| j        d| j        d| j	         d {V \  }}| j        
                    t          d| j        t          j                    t                      |||                                 |d ||
| j        | j        || j                   d {V }|d         | _        |d         | _        | S )Nr  r  r!  r   r  r   r   r  r   r   r  r  r  r  r  r   r   r   T)r  r   rG  r'  rH  r9  rI  r:  rJ  rO  rL  rK  rN  rM  r(  rM   rN   )r  r  r  r   r  r  r!  r   r   r   r   rn  r    rq  r8  r  rK  rN  r(  r  evals_result_)r   r   r  r   r  r   r  r  r  r  r  rM  r   rH  r[  r!  r9  r:  r  s                      rt   r"  zDaskXGBRanker._fit_asyncq  s       $$&&151D1Dv2
 2
.vv >K
 
 
;;
 ((
 LL	

 a
 a
 $
 
 (-
 $
 ,O
 X
 $:#9
 "6!5
 t
  X!
" LL#
$  $66%
& ,,'
 
 
 
 
 
 
* ((; +--$&& 88::  "&"<n]! ) 
 
 
 
 
 
 
 
$  	*$Y/ry   )r   r   r  r   r  r  r  r  rM  r  r  r   r   r  c                   d}||t          |          |t          d          dt          dt          t          j                 fd}dt
          t                   dt          dt          t
          t          j                          fd}| j        s ||          r0 ||d          r$ ||d	          r ||d
          r ||d          sJ ||J t          |          }t          | j        |||||          \  }}}}}|;g }g }g }g }|	sJ t          |          D ]\  }\  }}|r||         nd }|r||         nd } ||          sJ |	sJ |	|         }|	r0 ||d          r$ ||d	          r ||d
          r ||d          sJ ||J t          |          |k    r t          | j        |||||          \  }}}}}n|||||f\  }}}}}|                    ||f           |                    |           ||                    |           ||                    |           |}|}	|r|nd }|r|nd }|                     | j        |||||||	|
||||          S )Nz=Use the `qid` instead of the `group` with the dask interface.z`qid` is required for ranking.r   r\   c                 X    t          | t          j                  st          d          dS )NzJWhen `allow_group_split` is set to False, X is required to be a dataframe.T)rc   r   r   r   )r   s    rt   check_dfz#DaskXGBRanker.fit.<locals>.check_df  s4    a.. $   4ry   r   r   c                 d    t          | t          j                  s| t          d| d          dS )Nz*When `allow_group_split` is set to False, z is required to be a series.T)rc   r   r   r   )r   r   s     rt   	check_serz$DaskXGBRanker.fit.<locals>.check_ser  sK     c29-- #/# # # #   4ry   r  r  r   )r  r  r   )r   r  r   r  r   r  r  r  rM  r  r  r   )r   rI   r   r   r   r   re   r   rH  r$  rD   r  r   r   r  r"  )r   r   r  r   r   r  r   r  r  r  r  rM  r  r  r   r   rR  rT  X_idnew_eval_setnew_eval_qidnew_sample_weight_eval_setnew_base_margin_eval_setr   Xeyewebeqes                                rt   r,  zDaskXGBRanker.fit  s   & N*"4S//!;=>>>	 	Ibl,C 	 	 	 		/*	25	x	*+	 	 	 	 % =	Ic5)) Ia%% Im_==	
 Ik=99   ?q}}}a55D4B+'5 5 51AsA}k #!!-/*+-(#,X#6#6 < <KAxB6LV/22RVB4HR-a00dB#8B<<'''#OOO!!B %Ib%00 &Ib#.. &Ib/::	
 &Ib-88   >bnnn"vv~~-; KRR. .*BB ./Q{-R*BB ''R111 ''+++~299"===~077;;;''2LV..RV ' 1IR,,d %   O'##9!5+ ! 
 
 	
ry   )r   r   r   r0   re   r  r   r*  r   r   r   rK  rJ   rI   r   r   r   r   r6   r*   r"  r,  r7   r   r   r   s   @rt   rT   rT   E  s0       2   %"')-K K K K  	K
 :&K K 
K K K K K  KS      
== =
 o&=  0= o.= 8E/?*J$KLM= !)/)B C= 'x'@A= 8O45= sDy!= E(G"345= "/2= 
= = = =@   ,0)-3715PT:>8<.3=AFJDH59!w
 w
 w
w
 w

 (w
 o&w
  0w
 o.w
 8E/?*J$KLMw
 Xo67w
 8O45w
 %T	*+w
 E(C"89:w
 !)/)B Cw
 'x'@Aw
  "/2!w
" 
#w
 w
 w
  w
v -'CKKKKKry   rT   zjImplementation of the Scikit-Learn API for XGBoost Random Forest Regressor.

    .. versionadded:: 1.4.0

r  zI
    n_estimators : int
        Number of trees in random forest to fit.
)rE  c                       e Zd Zedddddddee         dee         dee         d	ee         d
ee         deddf fd            Zde	e
ef         f fdZdefdZddddddddddededee         dee         deeeeef                           deeeef                  deeee
ef                  deee                  deee                  dee         dd f fdZ xZS )rU   rB   皙?h㈵>Nlearning_rate	subsamplecolsample_bynode
reg_lambdar(  rc  rd  re  rf  r(  r  r\   c          	      F     t                      j        d|||||d| d S Nrb  r~   r   r   r   rc  rd  re  rf  r(  r  r   s          rt   r   zDaskXGBRFRegressor.__init__=  M     	 	
'-!	
 	
 	
 	
 	
 	
 	
ry   c                 \    t                                                      }| j        |d<   |S Nnum_parallel_treer   r  n_estimatorsrM  s     rt   r  z!DaskXGBRFRegressor.get_xgb_paramsQ  +    ''))&*&7"#ry   c                     dS NrB   r~   r   s    rt   r  z*DaskXGBRFRegressor.get_num_boosting_roundsV      qry   Tr#  r   r  r  r   r  r  rM  r  r  r   c                    d t                                                      D             }t          | j        | j                    t                      j        di | | S )Nc                 "    i | ]\  }}|d v	||S r&  r~   r'  s      rt   r   z*DaskXGBRFRegressor.fit.<locals>.<dictcomp>h  r)  ry   r~   rr  r   r;   rK  rN  r   r,  r   r   r  r  r   r  r  rM  r  r  r   r{   r   s               rt   r,  zDaskXGBRFRegressor.fitZ  ]     UT!1!1TTT45t~FFFdry   r   r   r   r0   r   r  r*  r   r   r   re   r  r   r  rJ   rI   r   r   r   r  r*   r6   r,  r   r   s   @rt   rU   rU   0  s          *+%(,/&*)-
 
 
  
 E?	

 #5/
 UO
 :&
 
 

 
 
 
 
  
&S#X      
     4815PT.2=AFJDH59   
  0 o. 8E/?*J$KLM %T	*+ E'3"89: !)/)B C 'x'@A "/2 
         ry   rU   zkImplementation of the Scikit-Learn API for XGBoost Random Forest Classifier.

    .. versionadded:: 1.4.0

c                       e Zd Zedddddddee         dee         dee         d	ee         d
ee         deddf fd            Zde	e
ef         f fdZdefdZddddddddddededee         dee         deeeeef                           deeeef                  deeee
ef                  deee                  deee                  dee         dd f fdZ xZS )rV   rB   r`  ra  Nrb  rc  rd  re  rf  r(  r  r\   c          	      F     t                      j        d|||||d| d S rh  ri  rj  s          rt   r   zDaskXGBRFClassifier.__init__{  rk  ry   c                 \    t                                                      }| j        |d<   |S rm  ro  rM  s     rt   r  z"DaskXGBRFClassifier.get_xgb_params  rq  ry   c                     dS rs  r~   r   s    rt   r  z+DaskXGBRFClassifier.get_num_boosting_rounds  rt  ry   Tr#  r   r  r  r   r  r  rM  r  r  r   c                    d t                                                      D             }t          | j        | j                    t                      j        di | | S )Nc                 "    i | ]\  }}|d v	||S r&  r~   r'  s      rt   r   z+DaskXGBRFClassifier.fit.<locals>.<dictcomp>  r)  ry   r~   rw  rx  s               rt   r,  zDaskXGBRFClassifier.fit  ry  ry   rz  r   s   @rt   rV   rV   n  s          *+%(,/&*)-
 
 
  
 E?	

 #5/
 UO
 :&
 
 

 
 
 
 
  
&S#X      
     4815PT.2=AFJDH59   
  0 o. 8E/?*J$KLM %T	*+ E'3"89: !)/)B C 'x'@A "/2 
         ry   rV   )NN)ro  )r   loggingcollectionsr   
contextlibr   	functoolsr   r   	threadingr   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   dask.delayedr   r    r   r    _data_utilsr!   _typingr"   r#   r$   callbackr%   r&   r*  r'   CollArgsr(   r   compatr)   corer*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   sklearnr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   trackerr@   trainingrA   r_  rC   rD   r  rE   rF   rG   rH   r   r   r   rI   __annotations__rJ   rK   __all__	getLoggerrl   r   re   rn   rx   rO   r   rP   r	  r
  r  rQ   r4  r8  r?  rF  r  rn  rw  r  r  r  r  r  r  r   rW   r  rX   r  r  r  rR   rS   rT   rU   rV   r~   ry   rt   <module>r     s  2 2 2f  # # # # # # % % % % % % - - - - - - - -                                           (                                                 ! ! ! ! ! ! ! ! $ $ $ $ $ $ @ @ @ @ @ @ @ @ @ @ ' ' ' ' ' ' - - - - - - * * * * * * 1 1 1 1 1 1            	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 0 / / / / / / /                            # " " " " " , , , , , , 0 0 0 0 0 0 0 0 X X X X X X X X X X X X"28R\29#DE E E E"(BL01	 1 1 1y    F 
	+	,	,--hsmXeCHo%>>?@- c]- 	- - - -`SM U38_- c]	
    U U U U U*8 U U U!56 ;O    "p p p p p p p pf 7:Yt__D)*D
2x<
 D D c]	D
 D D D DN9 9 9 9 9+ 9 9 9~ )-%)	& & && d38n%& z"	&
 
#uS#X
& & & &R4(4S>2 4 4 4 4 (%S8H2I)J K	#Y   K#YK 4K	K K K Kg g S>g d38n%	g
 cNg g g HU;#3456g 
)	g $C=g T	"g  g !123g F#g z"g  l!g g g gT 
 	6 :>#+/#'%)6:&*%)6 6 6 6cN6 6 	6 HU;#34566 
)	6 $C=6  6 T	"6 !1236 F#6 z"6 	6 6 6 6r,$ ,e , , , , ,
)-c;?   BRR "R 	R
 /*R S/R sCx.R R R R Rj"" #",0";?"KN"
5c?DcN*+" " " ", ).w>R/R)S   0W WS>W $ 445W 	W W W W W W W W $W W W W W Wt   Y!#"&,3V 3V 3V)*3Vw(<<=3V V#
$3V
 3V 3V 3V 3V 3V 3V 3V $3V 3V 	3V 3V 3V 3Vl> > S>> $ 445	>
 > $> > > > /*> > > > > >B  '-Y"-1: : :)*:w(<<=: :
 $: : : : /*: : 	: : : :z")*"SM" #" c]	"
 " ;eK,<&=!>??@" " " ":  *>   UW UW UW UW UW( UW UW UWp =g?V W: W: W: W: W:')< W: W: W:t H7 t t t t t)+> t t	 tn 
 7
%  0P( P( P( P( P(N$7 P( P(1 0P(f 
 k  / / / / /) / / /d 
 k  / / / / /+ / / / / /ry   