
     hz                        d Z ddlmZ ddlmZ ddlZddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ  G d de          Z G d dee          Z G d de          Z G d de          Z G d de          Z G d de          Z G d dee          Z G d de          Z G d de          Z G d de          Zeeeedded ZdS )!zZLosses and corresponding default initial estimators for gradient boosting
decision trees.
    )ABCMeta)abstractmethodN)expit	logsumexp   )	TREE_LEAF)_weighted_percentileDummyClassifierDummyRegressorc                       e Zd ZdZdZd Zed             Zedd            Zed             Z		 	 dd
Z
ed             Zed             ZdS )LossFunctionaM  Abstract base class for various loss functions.

    Parameters
    ----------
    n_classes : int
        Number of classes.

    Attributes
    ----------
    K : int
        The number of regression trees to be induced;
        1 for regression and binary classification;
        ``n_classes`` for multi-class classification.
    Fc                     || _         d S N)K)self	n_classess     W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/ensemble/_gb_losses.py__init__zLossFunction.__init__#   s        c                     dS )z-Default ``init`` estimator for loss function.N r   s    r   init_estimatorzLossFunction.init_estimator&         r   Nc                     dS )ad  Compute the loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   r   yraw_predictionssample_weights       r   __call__zLossFunction.__call__*   r   r   c                     dS )N  Compute the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        Nr   r   r   r    kargss       r   negative_gradientzLossFunction.negative_gradient:   r   r   皙?r   c
                 ~   |                     |          }
|
                                }d|| <   t          j        |j        t
          k              d         D ](}|                     |||||||dd|	f         |           )|dd|	fxx         ||j        ddddf                             |
d          z  z  cc<   dS )az  Update the terminal regions (=leaves) of the given tree and
        updates the current predictions of the model. Traverses tree
        and invokes template method `_update_terminal_region`.

        Parameters
        ----------
        tree : tree.Tree
            The tree object.
        X : ndarray of shape (n_samples, n_features)
            The data array.
        y : ndarray of shape (n_samples,)
            The target labels.
        residual : ndarray of shape (n_samples,)
            The residuals (usually the negative gradient).
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        sample_weight : ndarray of shape (n_samples,)
            The weight of each sample.
        sample_mask : ndarray of shape (n_samples,)
            The sample mask to be used.
        learning_rate : float, default=0.1
            Learning rate shrinks the contribution of each tree by
             ``learning_rate``.
        k : int, default=0
            The index of the estimator being updated.

        r   Naxis)	applycopynpwherechildren_leftr   _update_terminal_regionvaluetake)r   treeXr   residualr    r!   sample_masklearning_ratekterminal_regionsmasked_terminal_regionsleafs                r   update_terminal_regionsz$LossFunction.update_terminal_regionsH   s   R  ::a== #3"7"7"9"902- HT/9<==a@ 
	 
	D(('1%	 	 	 	 	1AAAq!G1D1I1I1 2J 2
 2
 "
 	
r   c	                     dS )z=Template method for updating terminal regions (i.e., leaves).Nr   	r   r5   r;   r=   r6   r   r7   r    r!   s	            r   r2   z$LossFunction._update_terminal_region   r   r   c                     dS )aL  Return the initial raw predictions.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            The data array.
        estimator : object
            The estimator to use to compute the predictions.

        Returns
        -------
        raw_predictions : ndarray of shape (n_samples, K)
            The initial raw predictions. K is equal to 1 for binary
            classification and regression, and equal to the number of classes
            for multiclass classification. ``raw_predictions`` is casted
            into float64.
        Nr   )r   r6   	estimators      r   get_init_raw_predictionsz%LossFunction.get_init_raw_predictions   s	    & 	r   r   r(   r   )__name__
__module____qualname____doc__is_multi_classr   r   r   r"   r'   r>   r2   rC   r   r   r   r   r      s          N   < < ^<    ^   ^, 
?
 ?
 ?
 ?
B L L ^L   ^  r   r   )	metaclassc                   .     e Zd ZdZ fdZd Zd Z xZS )RegressionLossFunctionz)Base class for regression loss functions.c                 L    t                                          d           d S )N   r   )superr   )r   	__class__s    r   r   zRegressionLossFunction.__init__   s$    1%%%%%r   c                 d    t          |d          rt          |d          st          d          dS )zMake sure estimator has the required fit and predict methods.

        Parameters
        ----------
        estimator : object
            The init estimator to check.
        fitpredictzNThe init parameter must be a valid estimator and support both fit and predict.Nhasattr
ValueErrorr   rB   s     r   check_init_estimatorz+RegressionLossFunction.check_init_estimator   sH     	5)) 	gi.K.K 	0  	 	r   c                     |                     |          }|                    dd                              t          j                  S )Nr*   rN   )rT   reshapeastyper/   float64)r   r6   rB   predictionss       r   rC   z/RegressionLossFunction.get_init_raw_predictions   s;    ''**""2q))00<<<r   )rE   rF   rG   rH   r   rY   rC   __classcell__rQ   s   @r   rL   rL      s\        33& & & & &  = = = = = = =r   rL   c                   8    e Zd ZdZd Zd
dZd Z	 	 ddZd	 ZdS )LeastSquaresErrorzLoss function for least squares (LS) estimation.
    Terminal regions do not need to be updated for least squares.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    c                 "    t          d          S )Nmeanstrategyr   r   s    r   r   z LeastSquaresError.init_estimator   s    v....r   Nc                     |,t          j        ||                                z
  dz            S d|                                z  t          j        |||                                z
  dz  z            z  S )ar  Compute the least squares loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   rN   )r/   rd   ravelsumr   s       r   r"   zLeastSquaresError.__call__   s|      7A 5 5 7 77A=>>> ##%%&&1/D/D/F/F+F1*LMNNOr   c                 0    ||                                 z
  S )aT  Compute half of the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples,)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        rh   r%   s       r   r'   z#LeastSquaresError.negative_gradient   s     ?((****r   r(   r   c
                 ~    |dd|	fxx         ||                     |                                          z  z  cc<   dS )a  Least squares does not need to update terminal regions.

        But it has to update the predictions.

        Parameters
        ----------
        tree : tree.Tree
            The tree object.
        X : ndarray of shape (n_samples, n_features)
            The data array.
        y : ndarray of shape (n_samples,)
            The target labels.
        residual : ndarray of shape (n_samples,)
            The residuals (usually the negative gradient).
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        sample_weight : ndarray of shape (n,)
            The weight of each sample.
        sample_mask : ndarray of shape (n,)
            The sample mask to be used.
        learning_rate : float, default=0.1
            Learning rate shrinks the contribution of each tree by
             ``learning_rate``.
        k : int, default=0
            The index of the estimator being updated.
        N)rT   rh   )
r   r5   r6   r   r7   r    r!   r8   r9   r:   s
             r   r>   z)LeastSquaresError.update_terminal_regions   sJ    P 	1a1F1F1H1H!HHr   c	                     d S r   r   r@   s	            r   r2   z)LeastSquaresError._update_terminal_region"  s	     	r   r   rD   )	rE   rF   rG   rH   r   r"   r'   r>   r2   r   r   r   rb   rb      s         / / /   .+ + +. 
(I (I (I (IT    r   rb   c                   ,    e Zd ZdZd ZddZd Zd ZdS )LeastAbsoluteErrorzLoss function for least absolute deviation (LAD) regression.

    Parameters
    ----------
    n_classes : int
        Number of classes
    c                 $    t          dd          S Nquantile      ?rf   rr   r   r   s    r   r   z!LeastAbsoluteError.init_estimator9      zC@@@@r   Nc           	      (   |;t          j        ||                                z
                                            S d|                                z  t          j        |t          j        ||                                z
            z            z  S )at  Compute the least absolute error.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        NrN   )r/   absrh   rd   ri   r   s       r   r"   zLeastAbsoluteError.__call__<  s      6!o3355566;;=== ##%%&&O4I4I4K4K0K)L)LLMMNr   c                 H    |                                 }d||z
  dk    z  dz
  S )a  Compute the negative gradient.

        1.0 if y - raw_predictions > 0.0 else -1.0

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        r   r   rN   rk   r%   s       r   r'   z$LeastAbsoluteError.negative_gradientS  s/     *//11A'!+,q00r   c	                    t          j        ||k              d         }	|                    |	d          }|                    |	d          |                    |	d          z
  }
t          |
|d          |j        |ddf<   dS )z1LAD updates terminal regions to median estimates.r   r+   2   
percentileN)r/   r0   r4   r	   r3   )r   r5   r;   r=   r6   r   r7   r    r!   terminal_regiondiffs              r   r2   z*LeastAbsoluteError._update_terminal_regiond  s     (#3t#;<<Q?%**?*CCvvoAv..1E1E! 2F 2
 2
 
 "6-B"
 "
 "

4A:r   r   )rE   rF   rG   rH   r   r"   r'   r2   r   r   r   ro   ro   0  sb         A A A   .1 1 1"
 
 
 
 
r   ro   c                   @     e Zd ZdZd	 fd	Zd Zd
dZd
dZd Z xZ	S )HuberLossFunctionah  Huber loss function for robust regression.

    M-Regression proposed in Friedman 2001.

    Parameters
    ----------
    alpha : float, default=0.9
        Percentile at which to extract score.

    References
    ----------
    J. Friedman, Greedy Function Approximation: A Gradient Boosting
    Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.
    ?c                 d    t                                                       || _        d | _        d S r   )rP   r   alphagammar   r   rQ   s     r   r   zHuberLossFunction.__init__  s+    



r   c                 $    t          dd          S rq   r   r   s    r   r   z HuberLossFunction.init_estimator  ru   r   Nc                    |                                 }||z
  }| j        }|]|0t          j        t          j        |          | j        dz            }n+t          t          j        |          || j        dz            }t          j        |          |k    }|jt          j        d||         dz  z            }t          j        |t          j        ||                    |dz  z
  z            }||z   |j        d         z  }	nt          j        d||         z  ||         dz  z            }t          j        |||          z  t          j        ||                    |dz  z
  z            }||z   |                                z  }	|	S )a  Compute the Huber loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nd   rs   r   r   )	rh   r   r/   r|   rw   r   r	   ri   shape)
r   r   r    r!   r~   r   
gamma_masksq_losslin_losslosss
             r   r"   zHuberLossFunction.__call__  s    *//11?"
=$bfTllDJ4DEE,F4LL-c1A  VD\\U*
 fS4
#3q#8899GvervdJ;.?'@'@519'LMNNHh&!'!*4DDfS=#<<tJ?OST?TTUUGv,-6$
{+,,uqy8: H
 h&-*;*;*=*==Dr   c                    |                                 }||z
  }|0t          j        t          j        |          | j        dz            }n+t          t          j        |          || j        dz            }t          j        |          |k    }t          j        |j        d         ft          j                  }||         ||<   |t          j	        ||                    z  || <   || _
        |S )a  Compute the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   r   dtype)rh   r/   r|   rw   r   r	   zerosr   r]   signr   )	r   r   r    r!   r&   r~   r   r   r7   s	            r   r'   z#HuberLossFunction.negative_gradient  s     *//11?" M"&,,
S0@AAEE(}djSVFVWWEVD\\U*
8QWQZM<<<#J/ %j[0A(B(B B*
r   c	           	         t          j        ||k              d         }	|                    |	d          }| j        }
|                    |	d          |                    |	d          z
  }t	          ||d          }||z
  }|t          j        t          j        |          t          j        t          j        |          |
          z            z   |j	        |df<   d S )Nr   r+   rz   r{   )
r/   r0   r4   r   r	   rd   r   minimumrw   r3   )r   r5   r;   r=   r6   r   r7   r    r!   r}   r   r~   mediandiff_minus_medians                 r   r2   z)HuberLossFunction._update_terminal_region  s     (#3t#;<<Q?%**?*CC
vvoAv..1E1E! 2F 2
 2
 
 &dMbIII 6M$rwG%&&BF;L4M4Mu)U)UU(
 (
 

47r   r   r   
rE   rF   rG   rH   r   r   r"   r'   r2   r_   r`   s   @r   r   r   z  s              
A A A' ' ' 'R   8
 
 
 
 
 
 
r   r   c                   >     e Zd ZdZd	 fd	Zd Zd
dZd Zd Z xZ	S )QuantileLossFunctionzLoss function for quantile regression.

    Quantile regression allows to estimate the percentiles
    of the conditional distribution of the target.

    Parameters
    ----------
    alpha : float, default=0.9
        The percentile.
    r   c                 j    t                                                       || _        |dz  | _        d S )Nr   )rP   r   r   r|   r   s     r   r   zQuantileLossFunction.__init__  s/    
#+r   c                 .    t          d| j                  S )Nrr   rt   )r   r   r   s    r   r   z#QuantileLossFunction.init_estimator   s    zDJGGGGr   Nc                    |                                 }||z
  }| j        }||k    }|N|||                                         z  d|z
  ||                                          z  z
  |j        d         z  }ng|t	          j        ||         ||         z            z  d|z
  t	          j        ||          ||          z            z  z
  |                                z  }|S )a  Compute the Quantile loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        NrN   r   )rh   r   ri   r   r/   )r   r   r    r!   r~   r   maskr   s           r   r"   zQuantileLossFunction.__call__  s     *//11?"
?" T
(((AIte9J9J+JJ
DD
 }T2T$Z?@@@u9}dU';dD5k'I J JJK!!##$D r   c                 b    | j         }|                                }||k    }||z  d|z
  | z  z
  S )r$   rN   )r   rh   )r   r   r    r&   r   r   s         r   r'   z&QuantileLossFunction.negative_gradient"  s@     
)//11?"!e)u!455r   c	                    t          j        ||k              d         }	|                    |	d          |                    |	d          z
  }
|                    |	d          }t          |
|| j                  }||j        |df<   d S )Nr   r+   )r/   r0   r4   r	   r|   r3   )r   r5   r;   r=   r6   r   r7   r    r!   r}   r~   vals               r   r2   z,QuantileLossFunction._update_terminal_region3  s     (#3t#;<<Q?vvoAv..1E1E! 2F 2
 2
 
 &**?*CC"4HH!
47r   r   r   r   r`   s   @r   r   r     s        	 	& & & & & &
H H H   >6 6 6"" " " " " " "r   r   c                   D    e Zd ZdZed             Zed             Zd ZdS )ClassificationLossFunctionz-Base class for classification loss functions.c                     dS )a  Template method to convert raw predictions into probabilities.

        Parameters
        ----------
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        Returns
        -------
        probas : ndarray of shape (n_samples, K)
            The predicted probabilities.
        Nr   r   r    s     r   _raw_prediction_to_probaz3ClassificationLossFunction._raw_prediction_to_probaK  r   r   c                     dS )a  Template method to convert raw predictions to decisions.

        Parameters
        ----------
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        Returns
        -------
        encoded_predictions : ndarray of shape (n_samples, K)
            The predicted encoded labels.
        Nr   r   s     r   _raw_prediction_to_decisionz6ClassificationLossFunction._raw_prediction_to_decision[  r   r   c                 d    t          |d          rt          |d          st          d          dS )zMake sure estimator has fit and predict_proba methods.

        Parameters
        ----------
        estimator : object
            The init estimator to check.
        rS   predict_probazTThe init parameter must be a valid estimator and support both fit and predict_proba.NrU   rX   s     r   rY   z/ClassificationLossFunction.check_init_estimatork  sH     	5)) 	gi.Q.Q 	:  	 	r   N)rE   rF   rG   rH   r   r   r   rY   r   r   r   r   r   H  s]        77  ^   ^    r   r   c                   N     e Zd ZdZ fdZd ZddZd Zd Zd Z	d	 Z
d
 Z xZS )BinomialDeviancea  Binomial deviance loss function for binary classification.

    Binary classification is a special case; here, we only need to
    fit one tree instead of ``n_classes`` trees.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    c                     |dk    r-t          d                    | j        j        |                    t	                                          d           d S Nr   z-{0:s} requires 2 classes; got {1:d} class(es)rN   rO   rW   formatrQ   rE   rP   r   r   r   rQ   s     r   r   zBinomialDeviance.__init__  ^    >>?FFN+Y    	1%%%%%r   c                 "    t          d          S Npriorre   r
   r   s    r   r   zBinomialDeviance.init_estimator  s     0000r   Nc           	         |                                 }|0dt          j        ||z  t          j        d|          z
            z  S d|                                z  t          j        |||z  t          j        d|          z
  z            z  S )a  Compute the deviance (= 2 * negative log-likelihood).

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   )rh   r/   rd   	logaddexpri   r   s       r   r"   zBinomialDeviance.__call__  s      *//11 _$Q(H(HH   
 ##%%&&!O+r|A/O/OOQ r   c                 J    |t          |                                          z
  S )aP  Compute half of the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        )r   rh   r%   s       r   r'   z"BinomialDeviance.negative_gradient  s#     5..001111r   c	                    t          j        ||k              d         }	|                    |	d          }|                    |	d          }|                    |	d          }t          j        ||z            }
t          j        |||z
  z  d|z
  |z   z            }t	          |          dk     rd|j        |ddf<   dS |
|z  |j        |ddf<   dS )zMake a single Newton-Raphson step.

        our node estimate is given by:

            sum(w * (y - prob)) / sum(w * prob * (1 - prob))

        we take advantage that: y - prob = residual
        r   r+   rN   u?j/         N)r/   r0   r4   ri   rw   r3   r   r5   r;   r=   r6   r   r7   r    r!   r}   	numeratordenominators               r   r2   z(BinomialDeviance._update_terminal_region  s    & (#3t#;<<Q?==q=99FF?F++%**?*CCF=8344	f]a(l;q1ux?OPQQ {f$$%(DJtQz"""%.%<DJtQz"""r   c                     t          j        |j        d         dft           j                  }t	          |                                          |d d df<   |d d dfxx         |d d df         z  cc<   |S )Nr   r   r   rN   r/   onesr   r]   r   rh   r   r    probas      r   r   z)BinomialDeviance._raw_prediction_to_proba  sy    .q115RZHHHO113344aaadaaaduQQQT{"r   c                 X    |                      |          }t          j        |d          S NrN   r+   r   r/   argmaxr   s      r   r   z,BinomialDeviance._raw_prediction_to_decision  *    --o>>yQ''''r   c                 V   |                     |          }|d d df         }t          j        t          j                  j        }t          j        ||d|z
            }t          j        |d|z
  z            }|                    dd                              t          j	                  S )NrN   r*   
r   r/   finfofloat32epscliplogr[   r\   r]   r   r6   rB   probasproba_pos_classr   r    s          r   rC   z)BinomialDeviance.get_init_raw_predictions  s    ((++ A,hrz""&'/3C@@&A4G!HII&&r1--44RZ@@@r   r   rE   rF   rG   rH   r   r   r"   r'   r2   r   r   rC   r_   r`   s   @r   r   r   z  s        	 	& & & & &1 1 1
   >2 2 2= = =B  ( ( (A A A A A A Ar   r   c                   T     e Zd ZdZdZ fdZd ZddZddZd	 Z	d
 Z
d Zd Z xZS )MultinomialDeviancezMultinomial deviance loss function for multi-class classification.

    For multi-class classification we need to fit ``n_classes`` trees at
    each stage.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    Tc                     |dk     r,t          d                    | j        j                            t	                                          |           d S )N   z#{0:s} requires more than 2 classes.r   r   s     r   r   zMultinomialDeviance.__init__  sT    q==5<<T^=TUU   	#####r   c                 "    t          d          S r   r
   r   s    r   r   z"MultinomialDeviance.init_estimator      0000r   Nc                 2   t          j        |j        d         | j        ft           j                  }t          | j                  D ]}||k    |dd|f<   t          j        d||z                      d          z  t          |d          z   |          S )a  Compute the Multinomial deviance.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        r   r   Nr*   rN   r+   )weights)	r/   r   r   r   r]   rangeaverageri   r   )r   r   r    r!   Yr:   s         r   r"   zMultinomialDeviance.__call__  s      Hagaj$&)<<<tv 	 	A1fAaaadGGz!o%***222YUV5W5W5WW!
 
 
 	
r   r   c           
          |t          j        t          j        |dd|f         t          |d          z
                      z
  S )a  Compute negative gradient for the ``k``-th class.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.

        k : int, default=0
            The index of the class.
        NrN   r+   )r/   
nan_to_numexpr   )r   r   r    r:   kwargss        r   r'   z%MultinomialDeviance.negative_gradient(  sK     2=F?111a4(9_1+M+M+MMNN
 
 
 	
r   c	                    t          j        ||k              d         }	|                    |	d          }|                    |	d          }|                    |	d          }t          j        ||z            }
|
| j        dz
  | j        z  z  }
t          j        |||z
  z  d|z
  |z   z            }t          |          dk     rd|j        |ddf<   dS |
|z  |j        |ddf<   dS )z"Make a single Newton-Raphson step.r   r+   rN   r   r   N)r/   r0   r4   ri   r   rw   r3   r   s               r   r2   z+MultinomialDeviance._update_terminal_region;  s     (#3t#;<<Q?==q=99FF?F++%**?*CCF=8344	dfqjDF**	f]a(l;q1ux?OPQQ {f$$%(DJtQz"""%.%<DJtQz"""r   c           	          t          j        t          j        |t          |d          d d t           j        f         z
                      S r   )r/   r   r   r   newaxisr   s     r   r   z,MultinomialDeviance._raw_prediction_to_probaW  sI    }F9_1#E#E#Eaaam#TU 
 
 	
r   c                 X    |                      |          }t          j        |d          S r   r   r   s      r   r   z/MultinomialDeviance._raw_prediction_to_decision^  r   r   c                 
   |                     |          }t          j        t          j                  j        }t          j        ||d|z
            }t          j        |                              t          j                  }|S )NrN   )	r   r/   r   r   r   r   r   r\   r]   )r   r6   rB   r   r   r    s         r   rC   z,MultinomialDeviance.get_init_raw_predictionsb  sc    ((++hrz""&a#g..&..//
;;r   r   )r   )rE   rF   rG   rH   rI   r   r   r"   r'   r2   r   r   rC   r_   r`   s   @r   r   r     s        	 	 N$ $ $ $ $1 1 1
 
 
 
2
 
 
 
&= = =8
 
 
( ( (      r   r   c                   N     e Zd ZdZ fdZd ZddZd Zd Zd Z	d	 Z
d
 Z xZS )ExponentialLossa  Exponential loss function for binary classification.

    Same loss as AdaBoost.

    Parameters
    ----------
    n_classes : int
        Number of classes.

    References
    ----------
    Greg Ridgeway, Generalized Boosted Models: A guide to the gbm package, 2007
    c                     |dk    r-t          d                    | j        j        |                    t	                                          d           d S r   r   r   s     r   r   zExponentialLoss.__init__y  r   r   c                 "    t          d          S r   r
   r   s    r   r   zExponentialLoss.init_estimator  r   r   Nc                 $   |                                 }|0t          j        t          j        d|z  dz
   |z                      S d|                                z  t          j        |t          j        d|z  dz
   |z            z            z  S )a  Compute the exponential loss

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        N       @      ?r   rN   )rh   r/   rd   r   ri   r   s       r   r"   zExponentialLoss.__call__  s     *//11 726C!GcM"2_"DEEFFF ##%%&&!a%!)0N)O)OOPPQr   c                 l    d|z  dz
  }|t          j        | |                                z            z  S )aU  Compute the residual (= negative gradient).

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        r   r   )r/   r   rh   )r   r   r    r&   y_s        r   r'   z!ExponentialLoss.negative_gradient  s9     1Ws]BFB3!6!6!8!889999r   c	                    t          j        ||k              d         }	|                    |	d          }|                    |	d          }|                    |	d          }d|z  dz
  }
t          j        |
|z  t          j        |
 |z            z            }t          j        |t          j        |
 |z            z            }t          |          dk     rd|j        |ddf<   d S ||z  |j        |ddf<   d S )Nr   r+   r   r   r   r   )r/   r0   r4   ri   r   rw   r3   )r   r5   r;   r=   r6   r   r7   r    r!   r}   r   r   r   s                r   r2   z'ExponentialLoss._update_terminal_region  s	    (#3t#;<<Q?)..Q.GGFF?F++%**?*CC1Ws]F2-s_7L0M0MMNN	f]RVRC/4I-J-JJKK {f$$%(DJtQz"""%.%<DJtQz"""r   c                     t          j        |j        d         dft           j                  }t	          d|                                z            |d d df<   |d d dfxx         |d d df         z  cc<   |S )Nr   r   r   r   rN   r   r   s      r   r   z(ExponentialLoss._raw_prediction_to_proba  s~    .q115RZHHHC/"7"7"9"99::aaadaaaduQQQT{"r   c                 b    |                                 dk                        t                    S )Nr   )rh   r\   intr   s     r   r   z+ExponentialLoss._raw_prediction_to_decision  s'    %%''1,44S999r   c                 \   |                     |          }|d d df         }t          j        t          j                  j        }t          j        ||d|z
            }dt          j        |d|z
  z            z  }|                    dd                              t          j	                  S )NrN   rs   r*   r   r   s          r   rC   z(ExponentialLoss.get_init_raw_predictions  s    ((++ A,hrz""&'/3C@@ !o:M'N O OO&&r1--44RZ@@@r   r   r   r`   s   @r   r   r   j  s         & & & & &1 1 1   2: : := = =6  : : :	A 	A 	A 	A 	A 	A 	Ar   r   )squared_errorabsolute_errorhuberrr   deviancelog_lossexponential)rH   abcr   r   numpyr/   scipy.specialr   r   
tree._treer   utils.statsr	   dummyr   r   r   rL   rb   ro   r   r   r   r   r   r   LOSS_FUNCTIONSr   r   r   <module>r     s                    * * * * * * * * " " " " " " . . . . . . # # # # # # " " " " " "Y Y Y Y YW Y Y Y Yx= = = = =\W = = = =2g g g g g. g g gTG
 G
 G
 G
 G
/ G
 G
 G
Tr
 r
 r
 r
 r
. r
 r
 r
jV" V" V" V" V"1 V" V" V"r/ / / / / / / / /dzA zA zA zA zA1 zA zA zAzp p p p p4 p p pfqA qA qA qA qA0 qA qA qAj '($"	 	r   