
    sg_}                         d Z ddlZddlmZmZ ddlm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mZmZ ddlmZ ddlmZ ddlmZ  G d d      Z G d d      Z	 	 ddZy)z
This module contains the TreeGrower class.

TreeGrower builds a regression tree fitting a Newton-Raphson step, based on
the gradients and hessians of the training data.
    N)heappopheappush)default_timer)_openmp_effective_n_threads   )sum_parallel   )!set_raw_bitset_from_binned_bitset)PREDICTOR_RECORD_DTYPEX_BITSET_INNER_DTYPEMonotonicConstraint)HistogramBuilder)TreePredictor)Splitterc                   (    e Zd ZdZdddZd Zd Zy)TreeNodea  Tree Node class used in TreeGrower.

    This isn't used for prediction purposes, only for training (see
    TreePredictor).

    Parameters
    ----------
    depth : int
        The depth of the node, i.e. its distance from the root.
    sample_indices : ndarray of shape (n_samples_at_node,), dtype=np.uint32
        The indices of the samples at the node.
    partition_start : int
        start position of the node's sample_indices in splitter.partition.
    partition_stop : int
        stop position of the node's sample_indices in splitter.partition.
    sum_gradients : float
        The sum of the gradients of the samples at the node.
    sum_hessians : float
        The sum of the hessians of the samples at the node.

    Attributes
    ----------
    depth : int
        The depth of the node, i.e. its distance from the root.
    sample_indices : ndarray of shape (n_samples_at_node,), dtype=np.uint32
        The indices of the samples at the node.
    sum_gradients : float
        The sum of the gradients of the samples at the node.
    sum_hessians : float
        The sum of the hessians of the samples at the node.
    split_info : SplitInfo or None
        The result of the split evaluation.
    is_leaf : bool
        True if node is a leaf
    left_child : TreeNode or None
        The left child of the node. None for leaves.
    right_child : TreeNode or None
        The right child of the node. None for leaves.
    value : float or None
        The value of the leaf, as computed in finalize_leaf(). None for
        non-leaf nodes.
    partition_start : int
        start position of the node's sample_indices in splitter.partition.
    partition_stop : int
        stop position of the node's sample_indices in splitter.partition.
    allowed_features : None or ndarray, dtype=int
        Indices of features allowed to split for children.
    interaction_cst_indices : None or list of ints
        Indices of the interaction sets that have to be applied on splits of
        child nodes. The fewer sets the stronger the constraint as fewer sets
        contain fewer features.
    children_lower_bound : float
    children_upper_bound : float
    N)valuec                8   || _         || _        |j                  d   | _        || _        || _        || _        d| _        d | _        d | _	        | j                  t        d      t        d             d | _        d | _        d | _        d | _        || _        || _        y )Nr   Fz-infz+inf)depthsample_indicesshape	n_samplessum_gradientssum_hessiansr   is_leafallowed_featuresinteraction_cst_indicesset_children_boundsfloat
split_info
left_childright_child
histogramspartition_startpartition_stop)selfr   r   r$   r%   r   r   r   s           b/var/www/html/venv/lib/python3.12/site-packages/sklearn/ensemble/_hist_gradient_boosting/grower.py__init__zTreeNode.__init__W   s     
,'--a0*(
 $'+$  vf>  /,    c                      || _         || _        y)z<Set children values bounds to respect monotonic constraints.N)children_lower_boundchildren_upper_bound)r&   loweruppers      r'   r   zTreeNode.set_children_bounds{   s     %*!$)!r)   c                 \    | j                   j                  |j                   j                  kD  S )ak  Comparison for priority queue.

        Nodes with high gain are higher priority than nodes with low gain.

        heapq.heappush only need the '<' operator.
        heapq.heappop take the smallest item first (smaller is higher
        priority).

        Parameters
        ----------
        other_node : TreeNode
            The node to compare with.
        )r    gain)r&   
other_nodes     r'   __lt__zTreeNode.__lt__   s%     ##j&;&;&@&@@@r)   )__name__
__module____qualname____doc__r(   r   r2    r)   r'   r   r      s    5@ "-H*Ar)   r   c                       e Zd ZdZdddddddddddddej
                  j                         ddfd	Zd
 Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zy)
TreeGrowera  Tree grower class used to build a tree.

    The tree is fitted to predict the values of a Newton-Raphson step. The
    splits are considered in a best-first fashion, and the quality of a
    split is defined in splitting._split_gain.

    Parameters
    ----------
    X_binned : ndarray of shape (n_samples, n_features), dtype=np.uint8
        The binned input samples. Must be Fortran-aligned.
    gradients : ndarray of shape (n_samples,)
        The gradients of each training sample. Those are the gradients of the
        loss w.r.t the predictions, evaluated at iteration ``i - 1``.
    hessians : ndarray of shape (n_samples,)
        The hessians of each training sample. Those are the hessians of the
        loss w.r.t the predictions, evaluated at iteration ``i - 1``.
    max_leaf_nodes : int, default=None
        The maximum number of leaves for each tree. If None, there is no
        maximum limit.
    max_depth : int, default=None
        The maximum depth of each tree. The depth of a tree is the number of
        edges to go from the root to the deepest leaf.
        Depth isn't constrained by default.
    min_samples_leaf : int, default=20
        The minimum number of samples per leaf.
    min_gain_to_split : float, default=0.
        The minimum gain needed to split a node. Splits with lower gain will
        be ignored.
    min_hessian_to_split : float, default=1e-3
        The minimum sum of hessians needed in each node. Splits that result in
        at least one child having a sum of hessians less than
        ``min_hessian_to_split`` are discarded.
    n_bins : int, default=256
        The total number of bins, including the bin for missing values. Used
        to define the shape of the histograms.
    n_bins_non_missing : ndarray, dtype=np.uint32, default=None
        For each feature, gives the number of bins actually used for
        non-missing values. For features with a lot of unique values, this
        is equal to ``n_bins - 1``. If it's an int, all features are
        considered to have the same number of bins. If None, all features
        are considered to have ``n_bins - 1`` bins.
    has_missing_values : bool or ndarray, dtype=bool, default=False
        Whether each feature contains missing values (in the training data).
        If it's a bool, the same value is used for all features.
    is_categorical : ndarray of bool of shape (n_features,), default=None
        Indicates categorical features.
    monotonic_cst : array-like of int of shape (n_features,), dtype=int, default=None
        Indicates the monotonic constraint to enforce on each feature.
          - 1: monotonic increase
          - 0: no constraint
          - -1: monotonic decrease

        Read more in the :ref:`User Guide <monotonic_cst_gbdt>`.
    interaction_cst : list of sets of integers, default=None
        List of interaction constraints.
    l2_regularization : float, default=0.
        The L2 regularization parameter penalizing leaves with small hessians.
        Use ``0`` for no regularization (default).
    feature_fraction_per_split : float, default=1
        Proportion of randomly chosen features in each and every node split.
        This is a form of regularization, smaller values make the trees weaker
        learners and might prevent overfitting.
    rng : Generator
        Numpy random Generator used for feature subsampling.
    shrinkage : float, default=1.
        The shrinkage parameter to apply to the leaves values, also known as
        learning rate.
    n_threads : int, default=None
        Number of OpenMP threads to use. `_openmp_effective_n_threads` is called
        to determine the effective number of threads use, which takes cgroups CPU
        quotes into account. See the docstring of `_openmp_effective_n_threads`
        for details.

    Attributes
    ----------
    histogram_builder : HistogramBuilder
    splitter : Splitter
    root : TreeNode
    finalized_leaves : list of TreeNode
    splittable_nodes : list of TreeNode
    missing_values_bin_idx : int
        Equals n_bins - 1
    n_categorical_splits : int
    n_features : int
    n_nodes : int
    total_find_split_time : float
        Time spent finding the best splits
    total_compute_hist_time : float
        Time spent computing histograms
    total_apply_split_time : float
        Time spent splitting nodes
    with_monotonic_cst : bool
        Whether there are monotonic constraints that apply. False iff monotonic_cst is
        None.
    N           gMbP?   Fg      ?c                 <   | j                  |||       t        |      }|
|	dz
  }
t        |
t        j                        r7t        j                  |
g|j                  d   z  t
        j                        }
n%t        j                  |
t
        j                        }
t        |t              r|g|j                  d   z  }t        j                  |t
        j                        }|Bt        j                  |j                  d   t        j                  t
        j                        }n%t        j                  |t
        j                        }t        j                   |t        j                  k7        | _        |3t        j$                  |j                  d   t
        j                        }n%t        j                  |t
        j                        }t        j                   t        j&                  |dk(  |t        j                  k7              rt)        d      |j                  d   dk(  }t+        ||	||||      | _        |	dz
  }t/        ||
||||||||||||      | _        || _        || _        || _        || _        || _        |
| _        || _        || _         || _!        || _"        || _#        || _$        || _%        |j                  d   | _&        || _'        g | _(        g | _)        d| _*        d| _+        d| _,        d| _-        | j]                  ||       d| _/        y )	Nr	   dtype)r   
fill_valuer?   )r   r?   z7Categorical features cannot have monotonic constraints.r   )X_binnedn_bins_non_missingmissing_values_bin_idxhas_missing_valuesis_categoricalmonotonic_cstl2_regularizationmin_hessian_to_splitmin_samples_leafmin_gain_to_splithessians_are_constantfeature_fraction_per_splitrng	n_threadsr;   )0_validate_parametersr   
isinstancenumbersIntegralnparrayr   uint32asarraybooluint8fullr   NO_CSTint8anywith_monotonic_cstzeroslogical_and
ValueErrorr   histogram_builderr   splitterrA   max_leaf_nodes	max_depthrI   rJ   rB   rC   rD   rE   rF   interaction_cstrG   	shrinkage
n_featuresrN   splittable_nodesfinalized_leavestotal_find_split_timetotal_compute_hist_timetotal_apply_split_timen_categorical_splits_initialize_rootn_nodes)r&   rA   	gradientshessiansrc   rd   rI   rJ   rH   n_binsrB   rD   rE   rF   re   rG   rL   rM   rf   rN   rK   rC   s                         r'   r(   zTreeGrower.__init__   s   , 	!! 	

 0	:	%!'!('*:*:;!##$x~~a'88		" "$,>bii!P($/"4!5q8I!IZZ(:"((K
  GGnnQ'.55ggM JJ}BGGDM"$&&:M:T:T)T"U!XXHNN1,=RXXNNZZbhhGN66NN!#]6I6P6P%P

 VWW (q 1Q 6!1fi3H)"
 "(! 1#91)'/!5-/"7'A
  !," 0!2"4&<#"4,*.!2""..+" " "%("'*$&)#$%!i2r)   c                    |j                   t        j                  k7  rt        d      |j                  j
                  st        d      |dk  rt        dj                  |            |dk  rt        dj                  |            y)zfValidate parameters passed to __init__.

        Also validate parameters passed to splitter.
        zX_binned must be of type uint8.zMX_binned should be passed as Fortran contiguous array for maximum efficiency.r   z&min_gain_to_split={} must be positive.z)min_hessian_to_split={} must be positive.N)r?   rS   rX   NotImplementedErrorflagsf_contiguousr`   format)r&   rA   rJ   rH   s       r'   rO   zTreeGrower._validate_parametersg  s     >>RXX%%&GHH~~**0  q 8??@QR   !#;BBCWX  $r)   c                 v    | j                   r| j                          | j                   r| j                          y)z#Grow the tree, from root to leaves.N)rh   
split_next_apply_shrinkage)r&   s    r'   growzTreeGrower.grow  s.    ##OO ## 	r)   c                 d    | j                   D ]!  }|xj                  | j                  z  c_        # y)a  Multiply leaves values by shrinkage parameter.

        This must be done at the very end of the growing process. If this were
        done during the growing process e.g. in finalize_leaf(), then a leaf
        would be shrunk but its sibling would potentially not be (if it's a
        non-leaf), which would lead to a wrong computation of the 'middle'
        value needed to enforce the monotonic constraints.
        N)ri   r   rf   )r&   leafs     r'   rz   zTreeGrower._apply_shrinkage  s+     )) 	)DJJ$..(J	)r)   c           	         | j                   j                  d   }d}t        || j                        }| j                  j
                  r	|d   |z  }nt        || j                        }t        || j                  j                  d|||d      | _	        | j                  j                  d| j                  z  k  r| j                  | j                         y|| j                  j                  k  r| j                  | j                         y| j                  t        t!        | j                              | j                  _         t%               j&                  | j                   }t)        j*                  |t(        j,                  t!        |            | j                  _        t1               }| j                  j3                  | j                  j4                  | j                  j.                        | j                  _        | xj8                  t1               |z
  z  c_        t1               }| j;                  | j                         | xj<                  t1               |z
  z  c_        y)z/Initialize root node and finalize it if needed.r   r   r   r$   r%   r   r   r      Nr?   count)rA   r   r   rN   ra   rK   r   rb   	partitionrootr   rI   _finalize_leafrH   re   rangelenr   setunionrS   fromiterrU   r   timecompute_histograms_bruter   r#   rk   _compute_best_split_and_pushrj   )	r&   rp   rq   r   r   r   r   r   tics	            r'   rn   zTreeGrower._initialize_root  s   MM''*	$Y?!!77#A;2L'$..AL==22$'%
	 99T%:%:!::		*$--<<<		*+05c$:N:N6O0PDII-*su{{D,@,@A)+ 		=M9N*DII& f#55NNII$$dii&@&@ 
		 	$$4$f))$))4""dfsl2"r)   c           
      v   | j                   j                  |j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                        |_
        |j                  j                  dk  r| j                  |       yt        | j                  |       y)a]  Compute the best possible split (SplitInfo) of a given node.

        Also push it in the heap of splittable nodes if gain isn't zero.
        The gain of a node is 0 if either all the leaves are pure
        (best gain = 0), or if no split would satisfy the constraints,
        (min_hessians_to_split, min_gain_to_split, min_samples_leaf)
        )r   r#   r   r   r   lower_boundupper_boundr   r   N)rb   find_node_splitr   r#   r   r   r   r+   r,   r   r    r0   r   r   rh   r&   nodes     r'   r   z'TreeGrower._compute_best_split_and_push  s     --77nn,,****1111!22 8 	
 ??1$%T**D1r)   c           	         t        | j                        }t               }| j                  j	                  |j
                  |j                        \  }}}| xj                  t               |z
  z  c_        |j                  dz   }t        | j                        t        | j                        z   }|dz  }t        |||j                  |j                  |z   |j
                  j                  |j
                  j                  |j
                  j                        }t        |||j                   |j                   |j
                  j"                  |j
                  j$                  |j
                  j&                        }	|	|_        ||_        | j,                  @| j/                  |      \  |_        |_        |j2                  |	_        |j0                  |	_        | j4                  |j
                  j6                     s(|j8                  |	j8                  kD  |j
                  _        | xj<                  dz  c_        | xj>                  |j
                  j@                  z  c_        | jB                  E|| jB                  k(  r6| jE                  |       | jE                  |	       | jG                          ||	fS | jH                  5|| jH                  k(  r&| jE                  |       | jE                  |	       ||	fS |j8                  | jJ                  dz  k  r| jE                  |       |	j8                  | jJ                  dz  k  r| jE                  |	       | jL                  r| jN                  |j
                  j6                     tP        jR                  k(  r|jT                  x}
}|jV                  x}}n|jX                  |	jX                  z   dz  }| jN                  |j
                  j6                     tP        jZ                  k(  r|jT                  |}}
||jV                  }}n||jV                  }}
|jT                  |}}|j]                  |
|       |	j]                  ||       |j^                   }|	j^                   }|s|rN|j                  j`                  d   }|	j                  j`                  d   }||k  r|}|	}n|	}|}t               }| jb                  je                  |j                  |j0                        |_3        | jb                  ji                  |jf                  |jf                  |j0                        |_3        d|_3        | xjj                  t               |z
  z  c_5        t               }|r| jm                  |       |r| jm                  |	       | xjn                  t               |z
  z  c_7        ||	fD ]  }|j^                  s|`3 |`3||	fS )zSplit the node with highest potential gain.

        Returns
        -------
        left : TreeNode
            The resulting left child.
        right : TreeNode
            The resulting right child.
        r	   r   r   Nr   )8r   rh   r   rb   split_indicesr    r   rl   r   r   ri   r   r$   sum_gradient_leftsum_hessian_left
value_leftr%   sum_gradient_rightsum_hessian_rightvalue_rightr"   r!   re   _compute_interactionsr   r   rD   feature_idxr   missing_go_to_leftro   rm   rE   rc   r   _finalize_splittable_nodesrd   rI   r]   rF   r   rZ   r+   r,   r   POSr   r   r   ra   r   r#   compute_histograms_subtractionrk   r   rj   )r&   r   r   sample_indices_leftsample_indices_rightright_child_posr   n_leaf_nodesleft_child_noderight_child_node
lower_leftlower_right
upper_leftupper_rightmidshould_split_leftshould_split_rightn_samples_leftn_samples_rightsmallest_childlargest_childchilds                         r'   ry   zTreeGrower.split_next  s<    t,,-f
 MM''9L9LM		
 ##tv|3#

Q4001C8M8M4NN". 00///A//;;99//,,
 $/+::..//<<:://--
 ,) + **4007  77 4 1@0P0P-&&t'B'BC
  )),<,F,FF OO. 	!!T__%C%CC!*|t?R?R/R0 01++-"$444>>%%4>>*A0 01"$444$$t'<'<q'@@0%%(=(=(AA 01"" ""4??#>#>?&--. ,0+D+DD
[+/+D+DD
[&,,/?/E/EEJ&&t'B'BC*../ .2-F-F
J/2D4M4MK-0$2K2K
J/3/H/H#K//
JG00kJ !0 7 77!1!9!99 2 -;;AA!DN.==CCAFO/!0 0!1 / &C(,(>(>(W(W--~/N/N)N% &&EEOO"--"33 $ #DO((DFSL8(&C 11/B!112BC&&$&3,6& *+;< )==() O 000r)   c                 H   t               }g }|j                  D ]W  }|j                  j                  | j                  |   v s)|j                  |       |j                  | j                  |          Y t        j                  |t        j                  t        |            |fS )a]  Compute features allowed by interactions to be inherited by child nodes.

        Example: Assume constraints [{0, 1}, {1, 2}].
           1      <- Both constraint groups could be applied from now on
          / \
         1   2    <- Left split still fulfills both constraint groups.
        / \ / \      Right split at feature 2 has only group {1, 2} from now on.

        LightGBM uses the same logic for overlapping groups. See
        https://github.com/microsoft/LightGBM/issues/4481 for details.

        Parameters:
        ----------
        node : TreeNode
            A node that might have children. Based on its feature_idx, the interaction
            constraints for possible child nodes are computed.

        Returns
        -------
        allowed_features : ndarray, dtype=uint32
            Indices of features allowed to split for children.
        interaction_cst_indices : list of ints
            Indices of the interaction sets that have to be applied on splits of
            child nodes. The fewer sets the stronger the constraint as fewer sets
            contain fewer features.
        r   )r   r   r    r   re   appendupdaterS   r   rU   r   )r&   r   r   r   is        r'   r   z TreeGrower._compute_interactions  s    > 5"$-- 	AA**d.B.B1.EE'..q1 ''(<(<Q(?@	A
 KK(		EUAVW#
 	
r)   c                 H    d|_         | j                  j                  |       y)z)Make node a leaf of the tree being grown.TN)r   ri   r   r   s     r'   r   zTreeGrower._finalize_leaf  s     $$T*r)   c                     t        | j                        dkD  rE| j                  j                         }| j                  |       t        | j                        dkD  rDyy)zTransform all splittable nodes into leaves.

        Used when some constraint is met e.g. maximum number of leaves or
        maximum depth.r   N)r   rh   popr   r   s     r'   r   z%TreeGrower._finalize_splittable_nodes  sN    
 $''(1,((,,.D% $''(1,r)   c                 J   t        j                  | j                  t              }t        j                  | j                  dft
              }t        j                  | j                  dft
              }t        |||| j                  || j                         t        |||      S )a  Make a TreePredictor object out of the current tree.

        Parameters
        ----------
        binning_thresholds : array-like of floats
            Corresponds to the bin_thresholds_ attribute of the BinMapper.
            For each feature, this stores:

            - the bin frontiers for continuous features
            - the unique raw category values for categorical features

        Returns
        -------
        A TreePredictor object.
        r>      )
rS   r^   ro   r   rm   r   _fill_predictor_arraysr   rB   r   )r&   binning_thresholdspredictor_nodesbinned_left_cat_bitsetsraw_left_cat_bitsetss        r'   make_predictorzTreeGrower.make_predictor  s      ((4<<7MN"$((&&*2F#
  "xx&&*2F 
 	# II##	
 46J
 	
r)   )r3   r4   r5   r6   rS   randomdefault_rngr(   rO   r{   rz   rn   r   ry   r   r   r   r   r7   r)   r'   r9   r9      s    ^J ! #&II!!#)ob4 
)*3X22d1L(
T+&!
r)   r9   c           
         | |   }|j                   |d<   |j                  |d<   |j                  |j                  j                  |d<   nd|d<   |j                  |d<   |j
                  rd|d<   |dz   |fS |j                  }	|	j                  |	j                  }}
|
|d	<   ||d
<   |	j                  |d<   |	j                  |d<   |	j                  ||
   dz
  k(  rt        j                  |d<   nP|	j                  r9||
   }||d<   |	j                  ||<   t        ||   |	j                  |       |dz  }n||
   |   |d<   |dz  }||d<   t        | |||j                  ||||      \  }}||d<   t        | |||j                   ||||      S )z>Helper used in make_predictor to set the TreePredictor fields.r   r   r0   r   Tr   r	   r   bin_thresholdr   rE   num_threshold
bitset_idxleft)r   rB   next_free_node_idxnext_free_bitset_idxright)r   r   r    r0   r   r   r   bin_idxr   rE   rS   infleft_cat_bitsetr
   r   r!   r"   )r   r   r   grower_noder   rB   r   r   r   r    r   r   
categoriess                r'   r   r     s    -.D))DM%%DM)"--22VV%%DMY!A%';;;''J%11:3E3EK%D#D!+!>!>D	'66D	/<q@@ !#_		"	"'4
1\8B8R8R 45) !56&&	

 	! 2; ? H_!%DL/E---1	0,, 'DM!---1	 	r)   )r   r   )r6   rQ   heapqr   r   timeitr   r   numpyrS   sklearn.utils._openmp_helpersr   utils.arrayfuncsr   _bitsetr
   commonr   r   r   	histogramr   	predictorr   	splittingr   r   r9   r   r7   r)   r'   <module>r      s`     # (  E , 6 
 ( $ sA sAlD	
 D	
\ Kr)   