
\c           @  s=  d  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	 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 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 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% 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. d d" l/ m0 Z0 d d# l1 m2 Z2 d$ e3 f d%     YZ4 d& e3 f d'     YZ5 d( e3 f d)     YZ6 d* e6 f d+     YZ7 d, e3 f d-     YZ8 d. e3 f d/     YZ9 d0 e j: e e3  f d1     YZ; d2 e j: e e;  f d3     YZ< d4 e< f d5     YZ= d6 e< f d7     YZ> d8 e< f d9     YZ? d: e< f d;     YZ@ d< e j: e e;  f d=     YZA d> eA f d?     YZB d@ eA f dA     YZC dB eA f dC     YZD i e= dD 6e> dE 6e? dF 6e@ dG 6eE dH 6eD dI 6ZF i e9 dJ 6ZG dK e3 f dL     YZH dM e j: e e  f dN     YZI dO eI e	 f dP     YZJ dQ eI e
 f dR     YZK d S(S   s>  Gradient Boosted Regression Trees

This module contains methods for fitting gradient boosted regression trees for
both classification and regression.

The module structure is the following:

- The ``BaseGradientBoosting`` base class implements a common ``fit`` method
  for all the estimators in the module. Regression and classification
  only differ in the concrete ``LossFunction`` used.

- ``GradientBoostingClassifier`` implements gradient boosting for
  classification problems.

- ``GradientBoostingRegressor`` implements gradient boosting for
  regression problems.
i(   t   print_function(   t   division(   t   ABCMeta(   t   abstractmethodi   (   t   BaseEnsemblei   (   t   ClassifierMixin(   t   RegressorMixin(   t   six(   t   predict_stages(   t   predict_stage(   t   _random_sample_maskN(   t
   csc_matrix(   t
   csr_matrix(   t   issparse(   t   expit(   t   time(   t   train_test_split(   t   DecisionTreeRegressor(   t   DTYPE(   t	   TREE_LEAF(   t   check_random_state(   t   check_array(   t	   check_X_y(   t   column_or_1d(   t   check_consistent_length(   t
   deprecated(   t	   logsumexp(   t   _weighted_percentile(   t   check_is_fitted(   t   check_classification_targets(   t   NotFittedErrort   QuantileEstimatorc           B  s/   e  Z d  Z d d  Z d d  Z d   Z RS(   s   An estimator predicting the alpha-quantile of the training targets.

    Parameters
    ----------
    alpha : float
        The quantile
    g?c         C  s<   d | k  o d k  n s/ t  d |   n  | |  _ d  S(   Ni    g      ?s&   `alpha` must be in (0, 1.0) but was %r(   t
   ValueErrort   alpha(   t   selfR!   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   __init__I   s    c         C  sK   | d k r+ t j | |  j d  |  _ n t | | |  j d  |  _ d S(   s  Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : array, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : numpy array of shape (n_samples,)
            Individual weights for each sample
        g      Y@N(   t   Nonet   npt
   percentileR!   t   quantileR   (   R"   t   Xt   yt   sample_weight(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   fitN   s    	c         C  sF   t  |  d  t j | j d d f d t j } | j |  j  | S(   s  Predict labels

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        y : array, shape (n_samples,)
            Returns predicted values.
        R'   i    i   t   dtype(   R   R%   t   emptyt   shapet   float64t   fillR'   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   predictb   s    %N(   t   __name__t
   __module__t   __doc__R#   R$   R+   R1   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR   A   s   t   MeanEstimatorc           B  s#   e  Z d  Z d d  Z d   Z RS(   s9   An estimator predicting the mean of the training targets.c         C  s=   | d k r! t j |  |  _ n t j | d | |  _ d S(   s  Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : array, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : numpy array of shape (n_samples,)
            Individual weights for each sample
        t   weightsN(   R$   R%   t   meant   average(   R"   R(   R)   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR+   x   s    c         C  sF   t  |  d  t j | j d d f d t j } | j |  j  | S(   s  Predict labels

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        y : array, shape (n_samples,)
            Returns predicted values.
        R7   i    i   R,   (   R   R%   R-   R.   R/   R0   R7   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1      s    %N(   R2   R3   R4   R$   R+   R1   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR5   v   s   t   LogOddsEstimatorc           B  s)   e  Z d  Z d Z d d  Z d   Z RS(   s+   An estimator predicting the log odds ratio.g      ?c         C  s   | d k r/ t j |  } | j d | } n* t j | |  } t j | d |  } | d k sq | d k r t d   n  |  j t j | |  |  _ d S(   s  Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : array, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : numpy array of shape (n_samples,)
            Individual weights for each sample
        i    i   s   y contains non binary labels.N(   R$   R%   t   sumR.   R    t   scalet   logt   prior(   R"   R(   R)   R*   t   post   neg(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR+      s    c         C  sF   t  |  d  t j | j d d f d t j } | j |  j  | S(   s  Predict labels

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        y : array, shape (n_samples,)
            Returns predicted values.
        R=   i    i   R,   (   R   R%   R-   R.   R/   R0   R=   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1      s    %N(   R2   R3   R4   R;   R$   R+   R1   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR9      s   t   ScaledLogOddsEstimatorc           B  s   e  Z d  Z d Z RS(   s6   Log odds ratio scaled by 0.5 -- for exponential loss. g      ?(   R2   R3   R4   R;   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR@      s   t   PriorProbabilityEstimatorc           B  s#   e  Z d  Z d d  Z d   Z RS(   sT   An estimator predicting the probability of each
    class in the training data.
    c         C  sS   | d k r' t j | d t j } n  t j | d | } | | j   |  _ d S(   sx  Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : array, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : array, shape (n_samples,)
            Individual weights for each sample
        R,   R6   N(   R$   R%   t	   ones_likeR/   t   bincountR:   t   priors(   R"   R(   R)   R*   t   class_counts(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR+      s    c         C  sJ   t  |  d  t j | j d |  j j d f d t j } |  j | (| S(   s  Predict labels

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        y : array, shape (n_samples,)
            Returns predicted values.
        RD   i    R,   (   R   R%   R-   R.   RD   R/   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1      s    /
N(   R2   R3   R4   R$   R+   R1   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRA      s   t   ZeroEstimatorc           B  s#   e  Z d  Z d d  Z d   Z RS(   s(   An estimator that simply predicts zero. c         C  s\   t  j | j t  j  rO t  j |  j d |  _ |  j d k rX d |  _ qX n	 d |  _ d S(   sx  Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : numpy, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : array, shape (n_samples,)
            Individual weights for each sample
        i    i   i   N(   R%   t
   issubdtypeR,   t   signedintegert   uniqueR.   t	   n_classes(   R"   R(   R)   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR+     s
    c         C  sF   t  |  d  t j | j d |  j f d t j } | j d  | S(   s  Predict labels

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        y : array, shape (n_samples,)
            Returns predicted values.
        RJ   i    R,   g        (   R   R%   R-   R.   RJ   R/   R0   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1     s    (N(   R2   R3   R4   R$   R+   R1   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRF     s   t   LossFunctionc           B  se   e  Z d  Z e Z d   Z d   Z e d	 d   Z	 e d    Z
 d d d  Z e d    Z RS(
   sL  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.
    c         C  s   | |  _  d  S(   N(   t   K(   R"   RJ   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   A  s    c         C  s   t     d S(   s.   Default ``init`` estimator for loss function. N(   t   NotImplementedError(   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   init_estimatorD  s    c         C  s   d S(   s$  Compute the loss.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        N(    (   R"   R)   t   predR*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   __call__H  s    c         K  s   d S(   s   Compute the negative gradient.

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

        y_pred : array, shape (n_samples,)
            The predictions.
        N(    (   R"   R)   t   y_predt   kargs(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   negative_gradientX  s    g      ?i    c
         C  s   | j  |  }
 |
 j   } d | | <xV t j | j t k  d D]8 } |  j | | | | | | | d d  |	 f |  qC W| d d  |	 f c | | j d d  d d f j |
 d d 7<d S(   s  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 : array, shape (n, m)
            The data array.
        y : array, shape (n,)
            The target labels.
        residual : array, shape (n,)
            The residuals (usually the negative gradient).
        y_pred : array, shape (n,)
            The predictions.
        sample_weight : array, shape (n,)
            The weight of each sample.
        sample_mask : array, 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.

        ii    Nt   axis(	   t   applyt   copyR%   t   wheret   children_leftR   t   _update_terminal_regiont   valuet   take(   R"   t   treeR(   R)   t   residualRQ   R*   t   sample_maskt   learning_ratet   kt   terminal_regionst   masked_terminal_regionst   leaf(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   update_terminal_regionse  s    #c	   	      C  s   d S(   s9   Template method for updating terminal regions (=leaves). N(    (	   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    N(   R2   R3   R4   t   Falset   is_multi_classR#   RN   R   R$   RP   RS   Rd   RY   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRK   /  s   		-t   RegressionLossFunctionc           B  s   e  Z d  Z d   Z RS(   s{   Base class for regression loss functions.

    Parameters
    ----------
    n_classes : int
        Number of classes
    c         C  s9   | d k r t  d |   n  t t |   j |  d  S(   Ni   s1   ``n_classes`` must be 1 for regression but was %r(   R    t   superRg   R#   (   R"   RJ   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#     s    (   R2   R3   R4   R#   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRg     s   t   LeastSquaresErrorc           B  sD   e  Z d  Z d   Z d d  Z d   Z d d d  Z d   Z RS(	   s   Loss function for least squares (LS) estimation.
    Terminal regions need not to be updated for least squares.

    Parameters
    ----------
    n_classes : int
        Number of classes
    c         C  s   t    S(   N(   R5   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN     s    c         C  sX   | d k r' t j | | j   d  Sd | j   t j | | | j   d  Sd S(   s2  Compute the least squares loss.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        g       @g      ?N(   R$   R%   R7   t   ravelR:   (   R"   R)   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP     s    c         K  s   | | j    S(   s   Compute the negative gradient.

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

        pred : array, shape (n_samples,)
            The predictions.
        (   Rj   (   R"   R)   RO   RR   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS     s    g      ?i    c
   
      C  s3   | d d  |	 f c | | j  |  j   7<d S(   se  Least squares does not need to update terminal regions.

        But it has to update the predictions.

        Parameters
        ----------
        tree : tree.Tree
            The tree object.
        X : array, shape (n, m)
            The data array.
        y : array, shape (n,)
            The target labels.
        residual : array, shape (n,)
            The residuals (usually the negative gradient).
        y_pred : array, shape (n,)
            The predictions.
        sample_weight : array, shape (n,)
            The weight of each sample.
        sample_mask : array, 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(   R1   Rj   (
   R"   R\   R(   R)   R]   RQ   R*   R^   R_   R`   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRd     s    c	   	      C  s   d  S(   N(    (	   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    N(	   R2   R3   R4   RN   R$   RP   RS   Rd   RY   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRi     s   		t   LeastAbsoluteErrorc           B  s5   e  Z d  Z d   Z d d  Z d   Z d   Z RS(   s   Loss function for least absolute deviation (LAD) regression.

    Parameters
    ----------
    n_classes : int
        Number of classes
    c         C  s   t  d d  S(   NR!   g      ?(   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN     s    c         C  s_   | d k r) t j | | j    j   Sd | j   t j | t j | | j     Sd S(   s4  Compute the least absolute error.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        g      ?N(   R$   R%   t   absRj   R7   R:   (   R"   R)   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP     s    c         K  s"   | j    } d | | d k d S(   s  Compute the negative gradient.

        1.0 if y - pred > 0.0 else -1.0

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

        pred : array, shape (n_samples,)
            The predictions.
        g       @g        g      ?(   Rj   (   R"   R)   RO   RR   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS     s    c	         C  s   t  j | | k  d }	 | j |	 d d } | j |	 d d | j |	 d d }
 t |
 | d d | j | d d f <d S(   s2   LAD updates terminal regions to median estimates. i    RT   R&   i2   N(   R%   RW   R[   R   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   t   terminal_regiont   diff(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY   +  s    (N(   R2   R3   R4   RN   R$   RP   RS   RY   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRk     s
   		t   HuberLossFunctionc           B  sD   e  Z d  Z d d  Z d   Z d d  Z d d  Z d   Z RS(   s  Huber loss function for robust regression.

    M-Regression proposed in Friedman 2001.

    References
    ----------
    J. Friedman, Greedy Function Approximation: A Gradient Boosting
    Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.

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

    alpha : float
        Percentile at which to extract score
    g?c         C  s,   t  t |   j |  | |  _ d  |  _ d  S(   N(   Rh   Ro   R#   R!   R$   t   gamma(   R"   RJ   R!   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   G  s    	c         C  s   t  d d  S(   NR!   g      ?(   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN   L  s    c   
      C  sk  | j    } | | } |  j } | d k r | d k r\ t j t j |  |  j d  } q t t j |  | |  j d  } n  t j |  | k } | d k r t j d | | d  } t j | t j | |  | d  } | | | j	 d }	 ni t j d | | | | d  } t j | | | t j | |  | d  } | | | j   }	 |	 S(   s*  Compute the Huber loss.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        id   g      ?g       @i    N(
   Rj   Rp   R$   R%   R&   Rl   R!   R   R:   R.   (
   R"   R)   RO   R*   Rn   Rp   t
   gamma_maskt   sq_losst   lin_losst   loss(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP   O  s"    
	%%)# c   	      K  s   | j    } | | } | d k rG t j t j |  |  j d  } n" t t j |  | |  j d  } t j |  | k } t j | j d f d t j	 } | | | | <| t j
 | |  | | <| |  _ | S(   s8  Compute the negative gradient.

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

        pred : array, shape (n_samples,)
            The predictions.

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        id   i    R,   N(   Rj   R$   R%   R&   Rl   R!   R   t   zerosR.   R/   t   signRp   (	   R"   R)   RO   R*   RR   Rn   Rp   Rq   R]   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS   r  s    
%""	c	         C  s   t  j | | k  d }	 | j |	 d d } |  j }
 | j |	 d d | j |	 d d } t | | d d } | | } | t  j t  j |  t  j t  j |  |
   | j	 | d f <d  S(   Ni    RT   R&   i2   (
   R%   RW   R[   Rp   R   R7   Rv   t   minimumRl   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   Rm   Rp   Rn   t   mediant   diff_minus_median(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    	
	N(	   R2   R3   R4   R#   RN   R$   RP   RS   RY   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRo   4  s   	#t   QuantileLossFunctionc           B  sA   e  Z d  Z d d  Z d   Z d d  Z d   Z d   Z RS(   s,  Loss function for quantile regression.

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

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

    alpha : float, optional (default = 0.9)
        The percentile
    g?c         C  s0   t  t |   j |  | |  _ | d |  _ d  S(   Ng      Y@(   Rh   Rz   R#   R!   R&   (   R"   RJ   R!   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#     s    	c         C  s   t  |  j  S(   N(   R   R!   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN     s    c         C  s   | j    } | | } |  j } | | k } | d k rp | | | j   d | | | j   | j d } nL | t j | | | |  d | t j | | | |  | j   } | S(   s-  Compute the Quantile loss.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        g      ?i    N(   Rj   R!   R$   R:   R.   R%   (   R"   R)   RO   R*   Rn   R!   t   maskRt   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP     s    
	(#c         K  s6   |  j  } | j   } | | k } | | d | | S(   s   Compute the negative gradient.

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

        pred : array, shape (n_samples,)
            The predictions.
        g      ?(   R!   Rj   (   R"   R)   RO   RR   R!   R{   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS     s    	c	         C  s   t  j | | k  d }	 | j |	 d d | j |	 d d }
 | j |	 d d } t |
 | |  j  } | | j | d f <d  S(   Ni    RT   (   R%   RW   R[   R   R&   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   Rm   Rn   t   val(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    N(	   R2   R3   R4   R#   RN   R$   RP   RS   RY   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRz     s   		t   ClassificationLossFunctionc           B  s&   e  Z d  Z d   Z e d    Z RS(   s.   Base class for classification loss functions. c         C  s   t  d t |   j   d S(   s   Template method to convert scores to probabilities.

         the does not support probabilities raises AttributeError.
        s!   %s does not support predict_probaN(   t	   TypeErrort   typeR2   (   R"   t   score(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _score_to_proba  s    c         C  s   d S(   sU   Template method to convert scores to decisions.

        Returns int arrays.
        N(    (   R"   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _score_to_decision  s    (   R2   R3   R4   R   R   R   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR}     s   	t   BinomialDeviancec           B  sP   e  Z d  Z d   Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 RS(	   s  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         C  sG   | d k r- t  d j |  j j |    n  t t |   j d  d  S(   Ni   s-   {0:s} requires 2 classes; got {1:d} class(es)i   (   R    t   formatt	   __class__R2   Rh   R   R#   (   R"   RJ   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#     s    	c         C  s   t    S(   N(   R9   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN     s    c         C  st   | j    } | d k r= d t j | | t j d |   Sd | j   t j | | | t j d |   Sd S(   sH  Compute the deviance (= 2 * negative log-likelihood).

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        g       g        N(   Rj   R$   R%   R7   t	   logaddexpR:   (   R"   R)   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP     s
    %c         K  s   | t  | j    S(   s   Compute the residual (= negative gradient).

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels
        (   R   Rj   (   R"   R)   RO   RR   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS   %  s    c	         C  s   t  j | | k  d }	 | j |	 d d } | j |	 d d } | j |	 d d } t  j | |  }
 t  j | | | d | |  } t |  d k  r d | j | d d f <n |
 | | j | d d f <d S(   s   Make 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
        i    RT   i   gu?j/ g        N(   R%   RW   R[   R:   Rl   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   Rm   t	   numeratort   denominator(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY   2  s    
#c         C  sw   t  j | j d d f d t  j } t | j    | d  d   d f <| d  d   d f c | d  d   d f 8<| S(   Ni    i   R,   i   (   R%   t   onesR.   R/   R   Rj   (   R"   R   t   proba(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR   J  s    %",c         C  s"   |  j  |  } t j | d d S(   NRT   i   (   R   R%   t   argmax(   R"   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR   P  s    N(   R2   R3   R4   R#   RN   R$   RP   RS   RY   R   R   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s   
					t   MultinomialDeviancec           B  sY   e  Z d  Z e Z d   Z d   Z d	 d  Z d d  Z	 d   Z
 d   Z d   Z RS(
   s   Multinomial 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
    c         C  sD   | d k  r* t  d j |  j j    n  t t |   j |  d  S(   Ni   s#   {0:s} requires more than 2 classes.(   R    R   R   R2   Rh   R   R#   (   R"   RJ   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   c  s    	c         C  s   t    S(   N(   RA   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN   i  s    c         C  s   t  j | j d |  j f d t  j } x3 t |  j  D]" } | | k | d d  | f <q8 W| d k r t  j d | | j d d  t | d d  St  j d | | | j d d  t | d d  Sd S(   s4  Compute the Multinomial deviance.

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        i    R,   NiRT   i   (	   R%   Ru   R.   RL   R/   t   rangeR$   R:   R   (   R"   R)   RO   R*   t   YR`   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP   l  s    ( !i    c         K  s:   | t  j t  j | d d  | f t | d d   S(   s7  Compute negative gradient for the ``k``-th class.

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

        pred : array, shape (n_samples,)
            The predictions.

        k : int, optional (default=0)
            The index of the class
        NRT   i   (   R%   t
   nan_to_numt   expR   (   R"   R)   RO   R`   t   kwargs(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS     s    "c	         C  s   t  j | | k  d }	 | j |	 d d } | j |	 d d } | j |	 d d } t  j | |  }
 |
 |  j d |  j 9}
 t  j | | | d | |  } t |  d k  r d | j | d d f <n |
 | | j | d d f <d S(   s#   Make a single Newton-Raphson step. i    RT   i   g      ?gu?j/ g        N(   R%   RW   R[   R:   RL   Rl   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   Rm   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    c         C  s9   t  j t  j | t | d d d  d   t  j f   S(   NRT   i   (   R%   R   R   R   t   newaxis(   R"   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s    c         C  s"   |  j  |  } t j | d d S(   NRT   i   (   R   R%   R   (   R"   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s    N(   R2   R3   R4   t   TrueRf   R#   RN   R$   RP   RS   RY   R   R   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR   U  s   
				t   ExponentialLossc           B  sP   e  Z d  Z d   Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 RS(	   s  Exponential loss function for binary classification.

    Same loss as AdaBoost.

    References
    ----------
    Greg Ridgeway, Generalized Boosted Models: A guide to the gbm package, 2007

    Parameters
    ----------
    n_classes : int
        Number of classes.
    c         C  sG   | d k r- t  d j |  j j |    n  t t |   j d  d  S(   Ni   s-   {0:s} requires 2 classes; got {1:d} class(es)i   (   R    R   R   R2   Rh   R   R#   (   R"   RJ   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#     s    	c         C  s   t    S(   N(   R@   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRN     s    c         C  st   | j    } | d k r; t j t j d | d |   Sd | j   t j | t j d | d |   Sd S(   s/  Compute the exponential loss

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels

        sample_weight : array-like, shape (n_samples,), optional
            Sample weights.
        g       @g      ?i   i   N(   Rj   R$   R%   R7   R   R:   (   R"   R)   RO   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRP     s
    #c         K  s*   d | d } | t  j | | j    S(   s   Compute the residual (= negative gradient).

        Parameters
        ----------
        y : array, shape (n_samples,)
            True labels

        pred : array, shape (n_samples,)
            Predicted labels
        g       @g      ?(   R%   R   Rj   (   R"   R)   RO   RR   t   y_(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRS     s    c	         C  s   t  j | | k  d }	 | j |	 d d } | j |	 d d } | j |	 d d } d | d }
 t  j |
 | t  j |
 |   } t  j | t  j |
 |   } t |  d k  r d | j | d d f <n | | | j | d d f <d  S(   Ni    RT   g       @g      ?gu?j/ g        (   R%   RW   R[   R:   R   Rl   RZ   (   R"   R\   Ra   Rc   R(   R)   R]   RO   R*   Rm   R   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRY     s    %!c         C  s{   t  j | j d d f d t  j } t d | j    | d  d   d f <| d  d   d f c | d  d   d f 8<| S(   Ni    i   R,   g       @i   (   R%   R   R.   R/   R   Rj   (   R"   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s    %&,c         C  s   | j    d k j t j  S(   Ng        (   Rj   t   astypeR%   t   int(   R"   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s    N(   R2   R3   R4   R#   RN   R$   RP   RS   RY   R   R   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s   					t   lst   ladt   huberR'   t   deviancet   exponentialt   zerot   VerboseReporterc           B  s,   e  Z d  Z d   Z d d  Z d   Z RS(   s  Reports verbose output to stdout.

    Parameters
    ----------
    verbose : int
        Verbosity level. If ``verbose==1`` output is printed once in a while
        (when iteration mod verbose_mod is zero).; if larger than 1 then output
        is printed for each update.
    c         C  s   | |  _  d  S(   N(   t   verbose(   R"   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   "  s    i    c         C  s   d d g } d d g } | j  d k  rD | j d  | j d  n  | j d  | j d	  t d
 d t |  d t |   d j |  |  _ d |  _ t   |  _	 | |  _
 d S(   s   Initialize reporter

        Parameters
        ----------
        est : Estimator
            The estimator

        begin_at_stage : int
            stage at which to begin reporting
        t   Iters
   Train Losss   {iter:>10d}s   {train_score:>16.4f}i   s   OOB Improves   {oob_impr:>16.4f}s   Remaining Times   {remaining_time:>16s}s   %10s s   %16s t    N(   t	   subsamplet   appendt   printt   lent   tuplet   joint   verbose_fmtt   verbose_modR   t
   start_timet   begin_at_stage(   R"   t   estR   t   header_fieldsR   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   init%  s    		c      
   C  s#  | j  d k  } | |  j } | d |  j d k r| rF | j | n d } | j | d t   |  j t | d  } | d k r d j | d  } n d j |  } t	 |  j
 j d | d d | j | d	 | d
 |   |  j d k r| d |  j d d k r|  j d 9_ qn  d S(   s   Update reporter with new iteration.

        Parameters
        ----------
        j : int
            The new iteration
        est : Estimator
            The estimator
        i   i    i<   s   {0:.2f}mg      N@s   {0:.2f}st   itert   train_scoret   oob_imprt   remaining_timei
   N(   R   R   R   t   oob_improvement_t   n_estimatorsR   R   t   floatR   R   R   t   train_score_R   (   R"   t   jR   t   do_oobt   iR   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   updateD  s    
-
*(   R2   R3   R4   R#   R   R   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR     s   		t   BaseGradientBoostingc        
   B  s   e  Z d  Z e d d d e d d d d d   Z d d d  Z d   Z d	   Z	 d
   Z
 d   Z d   Z d   Z e e d  d     Z d d d  Z d d d d  Z e d  Z d   Z d   Z d   Z e d    Z d   Z d   Z RS(   s+   Abstract base class for Gradient Boosting. g?i    t   autog?g-C6?c         C  s   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 |	 |  _
 |
 |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R   R_   Rt   t	   criteriont   min_samples_splitt   min_samples_leaft   min_weight_fraction_leafR   t   max_featurest	   max_deptht   min_impurity_decreaset   min_impurity_splitR   t   random_stateR!   R   t   max_leaf_nodest
   warm_startt   presortt   validation_fractiont   n_iter_no_changet   tol(   R"   Rt   R_   R   R   R   R   R   R   R   R   R   R   R   R   R!   R   R   R   R   R   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   e  s,    
																					c         C  s  | j  t j k s t  |  j } | } xqt | j  D]`} | j rg t j | | k d t j	 } n  | j
 | | d | d | } t d |  j d d d |  j d |  j d	 |  j d
 |  j d |  j d |  j d |  j d |  j d | d |  j  } |  j d k  r| | j t j	  } n  |
 d k	 r.|
 n | } | j | | d | d t d | | j | j | | | | | | |  j d | | |  j | | f <q7 W| S(   sA   Fit another stage of ``n_classes_`` trees to the boosting model. R,   R`   R*   R   t   splittert   bestR   R   R   R   R   R   R   R   R   R   g      ?t   check_inputt   X_idx_sortedN(   R,   R%   t   boolt   AssertionErrort   loss_R   RL   Rf   t   arrayR/   RS   R   R   R   R   R   R   R   R   R   R   R   R   R   R$   R+   Re   Rd   t   tree_R_   t   estimators_(   R"   R   R(   R)   RQ   R*   R^   R   R   t   X_csct   X_csrRt   t
   original_yR`   R]   R\   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt
   _fit_stage  s>    		!										c         C  s  |  j  d k r% t d |  j    n  |  j d k rJ t d |  j   n  |  j |  j k sk |  j t k r t d j |  j    n  |  j d k r t |  j  d k r t	 n t
 } n t |  j } |  j d k r | |  j |  j  |  _ n | |  j  |  _ d |  j k  od
 k n s7t d |  j   n  |  j d k	 rt |  j t j  r|  j t k rt d |  j   qqt |  j d  st |  j d  rt d |  j   qn  d |  j k  od
 k  n st d |  j   n  t |  j t j  r|  j d k rZ|  j d k rNt d t t j |  j    } q|  j } qh|  j d k rt d t t j |  j    } qh|  j d k rt d t t j |  j    } qht d |  j   n |  j d k r|  j } nw t |  j t j t j f  r|  j } nM d |  j k  o5d
 k n r\t t |  j |  j  d  } n t d   | |  _  t |  j! t j t j t" d  f  st d |  j!   n  d t# t$ f } |  j% | k rt d j | |  j%    n  d S(   s@   Check validity of parameters and raise ValueError if not valid. i    s.   n_estimators must be greater than 0 but was %rg        s/   learning_rate must be greater than 0 but was %rs   Loss '{0:s}' not supported. R   i   R   R'   g      ?s%   subsample must be in (0,1] but was %rs   init="%s" is not supportedR+   R1   sD   init=%r must be valid BaseEstimator and support both fit and predicts&   alpha must be in (0.0, 1.0) but was %rR   i   t   sqrtt   log2sW   Invalid value for max_features: %r. Allowed string values are 'auto', 'sqrt' or 'log2'.s'   max_features must be in (0, n_features]sC   n_iter_no_change should either be None or an integer. %r was passeds,   'presort' should be in {}. Got {!r} instead.N(   R   R'   (&   R   R    R_   Rt   t   _SUPPORTED_LOSSt   LOSS_FUNCTIONSR   R   t   classes_R   R   t
   n_classes_R!   R   R   R   R$   t
   isinstanceR   t   string_typest   INIT_ESTIMATORSt   hasattrR   t   maxR   R%   R   t   n_features_R   t   numberst   Integralt   integert   max_features_R   R   R   Re   R   (   R"   t
   loss_classR   t   allowed_presort(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _check_params  st    	$$$			c         C  s   |  j  d k r$ |  j j   |  _ n7 t |  j  t j  rO t |  j    |  _ n |  j  |  _ t	 j
 |  j |  j j f d t	 j |  _ t	 j |  j f d t	 j |  _ |  j d k  r t	 j |  j d t	 j |  _ n  d S(   sA   Initialize model state and allocate model state data structures. R,   g      ?N(   R   R$   R   RN   t   init_R   R   R   R   R%   R-   R   RL   t   objectR   Ru   R/   R   R   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _init_state  s    !c         C  s   t  |  d  r- t j d	 d t j |  _ n  t  |  d  rE |  ` n  t  |  d  r] |  ` n  t  |  d  ru |  ` n  t  |  d  r |  ` n  d S(
   s0   Clear the state of the gradient boosting model. R   i    R,   R   R   R   t   _rngN(   i    i    (	   R   R%   R-   R   R   R   R   R   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _clear_state  s    			c         C  s   |  j  } | |  j j d k  r? t d | |  j d f   n  t j |  j | |  j j f  |  _ t j |  j |  |  _ |  j	 d k  s t
 |  d  r t
 |  d  r t j |  j |  |  _ q t j | f d t j |  _ n  d S(   s;   Add additional ``n_estimators`` entries to all attributes. i    s(   resize with smaller n_estimators %d < %di   R   R,   N(   R   R   R.   R    R%   t   resizeR   RL   R   R   R   R   Ru   R/   (   R"   t   total_n_estimators(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _resize_state*  s    	c         C  s   t  t |  d g    d k S(   NR   i    (   R   t   getattr(   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _is_initialized>  s    c         C  s   t  |  d  d S(   sA   Check that the estimator is initialized, raising an error if not.R   N(   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _check_initializedA  s    sP   Attribute n_features was deprecated in version 0.19 and will be removed in 0.21.c         C  s   |  j  S(   N(   R   (   R"   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt
   n_featuresE  s    c         C  s	  |  j  s |  j   n  t | | d d d d g d t \ } } | j \ } |  _ | d k ry t j | d t j	 } n t
 | d t } t | | |  |  j | |  } |  j d k	 r t | | | d |  j d |  j \ } } } } } } n d } } } |  j   |  j   sf|  j   |  j j | | |  |  j j |  }	 d	 }
 t |  j  |  _ n |  j |  j j d	 k  rt d
 |  j |  j j d	 f   n  |  j j d	 }
 t | d t d d d d } |  j |  }	 |  j   |  j  t k rt! |  rt d   n  |  j  } | d k r;t! |  } n  d } | rqt j" t j# | d d	 d t j$ } n  |  j% | | |	 | |  j | | | |
 | |  } | |  j j d	 k r|  j |  |  _ |  j& |  |  _& t' |  d  r|  j( |  |  _( qn  | |  _) |  S(   s!  Fit the gradient boosting model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        y : array-like, shape (n_samples,)
            Target values (strings or integers in classification, real numbers
            in regression)
            For classification, labels must correspond to classes.

        sample_weight : array-like, shape (n_samples,) or None
            Sample weights. If None, then samples are equally weighted. Splits
            that would create child nodes with net zero or negative weight are
            ignored while searching for a split in each node. In the case of
            classification, splits are also ignored if they would result in any
            single class carrying a negative weight in either child node.

        monitor : callable, optional
            The monitor is called after each iteration with the current
            iteration, a reference to the estimator and the local variables of
            ``_fit_stages`` as keyword arguments ``callable(i, self,
            locals())``. If the callable returns ``True`` the fitting procedure
            is stopped. The monitor can be used for various things such as
            computing held-out estimates, early stopping, model introspect, and
            snapshoting.

        Returns
        -------
        self : object
        t   accept_sparset   csrt   csct   cooR,   t   warnR   t	   test_sizei    sX   n_estimators=%d must be larger or equal to estimators_.shape[0]=%d when warm_start==Truet   ordert   Cs0   Presorting is not supported for sparse matrices.R   RT   R   N(*   R   R   R   R   R.   R   R$   R%   R   t   float32R   R   R   t   _validate_yR   R   R   R   R   R   R   R   R+   R1   R   R   R   R   R    R   t   _decision_functionR   R   R   t   asfortranarrayt   argsortt   int32t   _fit_stagesR   R   R   t   n_estimators_(   R"   R(   R)   R*   t   monitort	   n_samplest   X_valt   y_valt   sample_weight_valRQ   R   R   R   t   n_stages(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR+   K  sb    $	*	!


			c         C  s  | j  d } |  j d k  } t j | f d t j } t d t |  j |   } |  j } |  j d k r | d k	 r |  j t j
 |  } n d } |  j r t |  j  } | j |  |	  n  t |  r t |  n d } t |  r t |  n d } |  j d k	 r6t j |  j t j  } |  j |  } n  |	 } xt |	 |  j  D]} | rt | | |  } | | | | | | |  } n  |  j | | | | | | | | | | 
 } | r| | | | | | |  |  j | <| | | | | | | |  |  j | <n | | | |  |  j | <|  j d k rP| j | |   n  |
 d k	 r~|
 | |  t    } | r~Pq~n  |  j d k	 rO| | t |  |  } t j | |  j | k   r| | | t  |  <qPqOqOW| d S(   s   Iteratively fits the stages.

        For each stage it computes the progress (OOB, train score)
        and delegates to ``_fit_stage``.
        Returns the number of stages fit; might differ from ``n_estimators``
        due to early stopping.
        i    g      ?R,   i   g        N(!   R.   R   R%   R   R   R   R   R   R   R$   R:   R   R   R   R   R   R   R   t   fullt   inft   _staged_decision_functionR   R   R
   R   R   R   R   t   localst   nextt   anyR   R   (   R"   R(   R)   RQ   R*   R   R  R  R  R   R	  R   R
  R   R^   t   n_inbagR   t   min_weight_leaft   verbose_reporterR   R   t   loss_historyt   y_val_pred_iterR   t   old_oob_scoret   early_stoppingt   validation_loss(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR    sb    
					
	c         C  s   t     d  S(   N(   RM   (   R"   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _make_estimator!  s    c         C  s   |  j    |  j d j | d t } | j d |  j k ra t d j |  j | j d    n  |  j j	 |  j
 t j  } | S(   s0   Check input and compute prediction of ``init``. i    R   i   s&   X.shape[1] should be {0:d}, not {1:d}.(   i    i    (   R   R   t   _validate_X_predictR   R.   R   R    R   R   R1   R   R%   R/   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   _init_decision_function%  s    
	c         C  s,   |  j  |  } t |  j | |  j |  | S(   N(   R  R   R   R_   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR  /  s    c         c  sy   t  | d t d d d d } |  j |  } xE t |  j j d  D]- } t |  j | | |  j |  | j   VqD Wd S(   s'  Compute decision function of ``X`` for each iteration.

        This method allows monitoring (i.e. determine error on testing set)
        after each stage.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        score : generator of array, shape (n_samples, k)
            The decision function of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
            Regression and binary classification are special cases with
            ``k == 1``, otherwise ``k==n_classes``.
        R,   R   R   R   R   i    N(	   R   R   R  R   R   R.   R	   R_   RV   (   R"   R(   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR  7  s
    c         C  s   |  j    t j |  j f d t j } x; |  j D]0 } t d   | D  t |  } | | 7} q2 W| t |  j  } | | j   } | S(   s   Return the feature importances (the higher, the more important the
           feature).

        Returns
        -------
        feature_importances_ : array, shape (n_features,)
        R,   c         s  s$   |  ] } | j  j d  t  Vq d S(   t	   normalizeN(   R   t   compute_feature_importancesRe   (   t   .0R\   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pys	   <genexpr>_  s   (   R   R%   Ru   R   R/   R   R:   R   (   R"   t	   total_sumt   staget	   stage_sumt   importances(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   feature_importances_R  s    	
	c         C  s4   d |  _  | j j d k r0 | j t j  } n  | S(   Ni   t   O(   R   R,   t   kindR   R%   R/   (   R"   R)   R*   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR  g  s    	c         C  s   |  j    |  j d j | d t } |  j j \ } } t j | j d | | f  } xf t |  D]X } xO t |  D]A } |  j | | f } | j | d t	 | d d  | | f <qw Wqd W| S(   s  Apply trees in the ensemble to X, return leaf indices.

        .. versionadded:: 0.17

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, its dtype will be converted to
            ``dtype=np.float32``. If a sparse matrix is provided, it will
            be converted to a sparse ``csr_matrix``.

        Returns
        -------
        X_leaves : array-like, shape (n_samples, n_estimators, n_classes)
            For each datapoint x in X and for each tree in the ensemble,
            return the index of the leaf x ends up in each estimator.
            In the case of binary classification n_classes is 1.
        i    R   N(   i    i    (
   R   R   R  R   R.   R%   Ru   R   RU   Re   (   R"   R(   R   RJ   t   leavesR   R   t	   estimator(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRU   p  s    
0N(   R2   R3   R4   R   R$   Re   R#   R   R   R   R   R   R   R   t   propertyR   R   R+   R  R   R  R  R  R  R'  R  RU   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR   b  s0   	/	U					|Z	
				t   GradientBoostingClassifierc           B  s   e  Z d  Z d Z d d d d d d d d	 d
 d	 d d d d d d e d d d d d  Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z RS(   s*  Gradient Boosting for classification.

    GB builds an additive model in a
    forward stage-wise fashion; it allows for the optimization of
    arbitrary differentiable loss functions. In each stage ``n_classes_``
    regression trees are fit on the negative gradient of the
    binomial or multinomial deviance loss function. Binary classification
    is a special case where only a single regression tree is induced.

    Read more in the :ref:`User Guide <gradient_boosting>`.

    Parameters
    ----------
    loss : {'deviance', 'exponential'}, optional (default='deviance')
        loss function to be optimized. 'deviance' refers to
        deviance (= logistic regression) for classification
        with probabilistic outputs. For loss 'exponential' gradient
        boosting recovers the AdaBoost algorithm.

    learning_rate : float, optional (default=0.1)
        learning rate shrinks the contribution of each tree by `learning_rate`.
        There is a trade-off between learning_rate and n_estimators.

    n_estimators : int (default=100)
        The number of boosting stages to perform. Gradient boosting
        is fairly robust to over-fitting so a large number usually
        results in better performance.

    subsample : float, optional (default=1.0)
        The fraction of samples to be used for fitting the individual base
        learners. If smaller than 1.0 this results in Stochastic Gradient
        Boosting. `subsample` interacts with the parameter `n_estimators`.
        Choosing `subsample < 1.0` leads to a reduction of variance
        and an increase in bias.

    criterion : string, optional (default="friedman_mse")
        The function to measure the quality of a split. Supported criteria
        are "friedman_mse" for the mean squared error with improvement
        score by Friedman, "mse" for mean squared error, and "mae" for
        the mean absolute error. The default value of "friedman_mse" is
        generally the best as it can provide a better approximation in
        some cases.

        .. versionadded:: 0.18

    min_samples_split : int, float, optional (default=2)
        The minimum number of samples required to split an internal node:

        - If int, then consider `min_samples_split` as the minimum number.
        - If float, then `min_samples_split` is a fraction and
          `ceil(min_samples_split * n_samples)` are the minimum
          number of samples for each split.

        .. versionchanged:: 0.18
           Added float values for fractions.

    min_samples_leaf : int, float, optional (default=1)
        The minimum number of samples required to be at a leaf node.
        A split point at any depth will only be considered if it leaves at
        least ``min_samples_leaf`` training samples in each of the left and
        right branches.  This may have the effect of smoothing the model,
        especially in regression.

        - If int, then consider `min_samples_leaf` as the minimum number.
        - If float, then `min_samples_leaf` is a fraction and
          `ceil(min_samples_leaf * n_samples)` are the minimum
          number of samples for each node.

        .. versionchanged:: 0.18
           Added float values for fractions.

    min_weight_fraction_leaf : float, optional (default=0.)
        The minimum weighted fraction of the sum total of weights (of all
        the input samples) required to be at a leaf node. Samples have
        equal weight when sample_weight is not provided.

    max_depth : integer, optional (default=3)
        maximum depth of the individual regression estimators. The maximum
        depth limits the number of nodes in the tree. Tune this parameter
        for best performance; the best value depends on the interaction
        of the input variables.

    min_impurity_decrease : float, optional (default=0.)
        A node will be split if this split induces a decrease of the impurity
        greater than or equal to this value.

        The weighted impurity decrease equation is the following::

            N_t / N * (impurity - N_t_R / N_t * right_impurity
                                - N_t_L / N_t * left_impurity)

        where ``N`` is the total number of samples, ``N_t`` is the number of
        samples at the current node, ``N_t_L`` is the number of samples in the
        left child, and ``N_t_R`` is the number of samples in the right child.

        ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,
        if ``sample_weight`` is passed.

        .. versionadded:: 0.19

    min_impurity_split : float, (default=1e-7)
        Threshold for early stopping in tree growth. A node will split
        if its impurity is above the threshold, otherwise it is a leaf.

        .. deprecated:: 0.19
           ``min_impurity_split`` has been deprecated in favor of
           ``min_impurity_decrease`` in 0.19. The default value of
           ``min_impurity_split`` will change from 1e-7 to 0 in 0.23 and it
           will be removed in 0.25. Use ``min_impurity_decrease`` instead.

    init : estimator, optional
        An estimator object that is used to compute the initial
        predictions. ``init`` has to provide ``fit`` and ``predict``.
        If None it uses ``loss.init_estimator``.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    max_features : int, float, string or None, optional (default=None)
        The number of features to consider when looking for the best split:

        - If int, then consider `max_features` features at each split.
        - If float, then `max_features` is a fraction and
          `int(max_features * n_features)` features are considered at each
          split.
        - If "auto", then `max_features=sqrt(n_features)`.
        - If "sqrt", then `max_features=sqrt(n_features)`.
        - If "log2", then `max_features=log2(n_features)`.
        - If None, then `max_features=n_features`.

        Choosing `max_features < n_features` leads to a reduction of variance
        and an increase in bias.

        Note: the search for a split does not stop until at least one
        valid partition of the node samples is found, even if it requires to
        effectively inspect more than ``max_features`` features.

    verbose : int, default: 0
        Enable verbose output. If 1 then it prints progress and performance
        once in a while (the more trees the lower the frequency). If greater
        than 1 then it prints progress and performance for every tree.

    max_leaf_nodes : int or None, optional (default=None)
        Grow trees with ``max_leaf_nodes`` in best-first fashion.
        Best nodes are defined as relative reduction in impurity.
        If None then unlimited number of leaf nodes.

    warm_start : bool, default: False
        When set to ``True``, reuse the solution of the previous call to fit
        and add more estimators to the ensemble, otherwise, just erase the
        previous solution. See :term:`the Glossary <warm_start>`.

    presort : bool or 'auto', optional (default='auto')
        Whether to presort the data to speed up the finding of best splits in
        fitting. Auto mode by default will use presorting on dense data and
        default to normal sorting on sparse data. Setting presort to true on
        sparse data will raise an error.

        .. versionadded:: 0.17
           *presort* parameter.

    validation_fraction : float, optional, default 0.1
        The proportion of training data to set aside as validation set for
        early stopping. Must be between 0 and 1.
        Only used if ``n_iter_no_change`` is set to an integer.

        .. versionadded:: 0.20

    n_iter_no_change : int, default None
        ``n_iter_no_change`` is used to decide if early stopping will be used
        to terminate training when validation score is not improving. By
        default it is set to None to disable early stopping. If set to a
        number, it will set aside ``validation_fraction`` size of the training
        data as validation and terminate training when validation score is not
        improving in all of the previous ``n_iter_no_change`` numbers of
        iterations.

        .. versionadded:: 0.20

    tol : float, optional, default 1e-4
        Tolerance for the early stopping. When the loss is not improving
        by at least tol for ``n_iter_no_change`` iterations (if set to a
        number), the training stops.

        .. versionadded:: 0.20

    Attributes
    ----------
    n_estimators_ : int
        The number of estimators as selected by early stopping (if
        ``n_iter_no_change`` is specified). Otherwise it is set to
        ``n_estimators``.

        .. versionadded:: 0.20

    feature_importances_ : array, shape (n_features,)
        The feature importances (the higher, the more important the feature).

    oob_improvement_ : array, shape (n_estimators,)
        The improvement in loss (= deviance) on the out-of-bag samples
        relative to the previous iteration.
        ``oob_improvement_[0]`` is the improvement in
        loss of the first stage over the ``init`` estimator.

    train_score_ : array, shape (n_estimators,)
        The i-th score ``train_score_[i]`` is the deviance (= loss) of the
        model at iteration ``i`` on the in-bag sample.
        If ``subsample == 1`` this is the deviance on the training data.

    loss_ : LossFunction
        The concrete ``LossFunction`` object.

    init_ : estimator
        The estimator that provides the initial predictions.
        Set via the ``init`` argument or ``loss.init_estimator``.

    estimators_ : ndarray of DecisionTreeRegressor,shape (n_estimators, ``loss_.K``)
        The collection of fitted sub-estimators. ``loss_.K`` is 1 for binary
        classification, otherwise n_classes.

    Notes
    -----
    The features are always randomly permuted at each split. Therefore,
    the best found split may vary, even with the same training data and
    ``max_features=n_features``, if the improvement of the criterion is
    identical for several splits enumerated during the search of the best
    split. To obtain a deterministic behaviour during fitting,
    ``random_state`` has to be fixed.

    See also
    --------
    sklearn.tree.DecisionTreeClassifier, RandomForestClassifier
    AdaBoostClassifier

    References
    ----------
    J. Friedman, Greedy Function Approximation: A Gradient Boosting
    Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.

    J. Friedman, Stochastic Gradient Boosting, 1999

    T. Hastie, R. Tibshirani and J. Friedman.
    Elements of Statistical Learning Ed. 2, Springer, 2009.
    R   R   g?id   g      ?t   friedman_msei   i   g        i   i    R   g-C6?c      +   C  s   t  t |   j d | d | d | d | d | d | d | d |	 d	 | d
 | d | d | d | d | d |
 d | d | d | d | d | d |  d  S(   NRt   R_   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (   Rh   R-  R#   (   R"   Rt   R_   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#     s    
c         C  sx   t  |  t j | d t \ |  _ } t j t j | |   } | d k  rb t d |   n  t |  j  |  _	 | S(   Nt   return_inversei   su   y contains %d class after sample_weight trimmed classes with zero weights, while a minimum of 2 classes are required.(
   R   R%   RI   R   R   t   count_nonzeroRC   R    R   R   (   R"   R)   R*   t   n_trim_classes(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR    s    
c         C  sN   t  | d t d d d d } |  j |  } | j d d k rJ | j   S| S(   s  Compute the decision function of ``X``.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        score : array, shape (n_samples, n_classes) or (n_samples,)
            The decision function of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
            Regression and binary classification produce an array of shape
            [n_samples].
        R,   R   R   R   R   i   (   R   R   R  R.   Rj   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   decision_function  s
    
c         c  s#   x |  j  |  D] } | Vq Wd S(   s'  Compute decision function of ``X`` for each iteration.

        This method allows monitoring (i.e. determine error on testing set)
        after each stage.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        score : generator of array, shape (n_samples, k)
            The decision function of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
            Regression and binary classification are special cases with
            ``k == 1``, otherwise ``k==n_classes``.
        N(   R  (   R"   R(   t   dec(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   staged_decision_function  s    c         C  s7   |  j  |  } |  j j |  } |  j j | d d S(   s  Predict class for X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        y : array, shape (n_samples,)
            The predicted values.
        RT   i    (   R2  R   R   R   R[   (   R"   R(   R   t	   decisions(    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1     s    c         c  sG   x@ |  j  |  D]/ } |  j j |  } |  j j | d d Vq Wd S(   s;  Predict class at each stage for X.

        This method allows monitoring (i.e. determine error on testing set)
        after each stage.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        y : generator of array of shape (n_samples,)
            The predicted value of the input samples.
        RT   i    N(   R  R   R   R   R[   (   R"   R(   R   R5  (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   staged_predict  s    c         C  sa   |  j  |  } y |  j j |  SWn7 t k
 r9   n$ t k
 r\ t d |  j   n Xd S(   s  Predict class probabilities for X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Raises
        ------
        AttributeError
            If the ``loss`` does not support probabilities.

        Returns
        -------
        p : array, shape (n_samples, n_classes)
            The class probabilities of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
        s&   loss=%r does not support predict_probaN(   R2  R   R   R   t   AttributeErrorRt   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   predict_proba  s    c         C  s   |  j  |  } t j |  S(   s  Predict class log-probabilities for X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Raises
        ------
        AttributeError
            If the ``loss`` does not support probabilities.

        Returns
        -------
        p : array, shape (n_samples, n_classes)
            The class log-probabilities of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
        (   R8  R%   R<   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   predict_log_proba-  s    c         c  sm   y/ x( |  j  |  D] } |  j j |  Vq WWn7 t k
 rE   n$ t k
 rh t d |  j   n Xd S(   sI  Predict class probabilities at each stage for X.

        This method allows monitoring (i.e. determine error on testing set)
        after each stage.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        y : generator of array of shape (n_samples,)
            The predicted value of the input samples.
        s&   loss=%r does not support predict_probaN(   R  R   R   R   R7  Rt   (   R"   R(   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   staged_predict_probaE  s    (   R   R   N(   R2   R3   R4   R   R$   Re   R#   R  R2  R4  R1   R6  R8  R9  R:  (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR-    s&   										t   GradientBoostingRegressorc           B  sz   e  Z d  Z d Z d d d d d d	 d
 d d d d d d d d d d e d d d d d  Z d   Z d   Z d   Z	 RS(   s0*  Gradient Boosting for regression.

    GB builds an additive model in a forward stage-wise fashion;
    it allows for the optimization of arbitrary differentiable loss functions.
    In each stage a regression tree is fit on the negative gradient of the
    given loss function.

    Read more in the :ref:`User Guide <gradient_boosting>`.

    Parameters
    ----------
    loss : {'ls', 'lad', 'huber', 'quantile'}, optional (default='ls')
        loss function to be optimized. 'ls' refers to least squares
        regression. 'lad' (least absolute deviation) is a highly robust
        loss function solely based on order information of the input
        variables. 'huber' is a combination of the two. 'quantile'
        allows quantile regression (use `alpha` to specify the quantile).

    learning_rate : float, optional (default=0.1)
        learning rate shrinks the contribution of each tree by `learning_rate`.
        There is a trade-off between learning_rate and n_estimators.

    n_estimators : int (default=100)
        The number of boosting stages to perform. Gradient boosting
        is fairly robust to over-fitting so a large number usually
        results in better performance.

    subsample : float, optional (default=1.0)
        The fraction of samples to be used for fitting the individual base
        learners. If smaller than 1.0 this results in Stochastic Gradient
        Boosting. `subsample` interacts with the parameter `n_estimators`.
        Choosing `subsample < 1.0` leads to a reduction of variance
        and an increase in bias.

    criterion : string, optional (default="friedman_mse")
        The function to measure the quality of a split. Supported criteria
        are "friedman_mse" for the mean squared error with improvement
        score by Friedman, "mse" for mean squared error, and "mae" for
        the mean absolute error. The default value of "friedman_mse" is
        generally the best as it can provide a better approximation in
        some cases.

        .. versionadded:: 0.18

    min_samples_split : int, float, optional (default=2)
        The minimum number of samples required to split an internal node:

        - If int, then consider `min_samples_split` as the minimum number.
        - If float, then `min_samples_split` is a fraction and
          `ceil(min_samples_split * n_samples)` are the minimum
          number of samples for each split.

        .. versionchanged:: 0.18
           Added float values for fractions.

    min_samples_leaf : int, float, optional (default=1)
        The minimum number of samples required to be at a leaf node.
        A split point at any depth will only be considered if it leaves at
        least ``min_samples_leaf`` training samples in each of the left and
        right branches.  This may have the effect of smoothing the model,
        especially in regression.

        - If int, then consider `min_samples_leaf` as the minimum number.
        - If float, then `min_samples_leaf` is a fraction and
          `ceil(min_samples_leaf * n_samples)` are the minimum
          number of samples for each node.

        .. versionchanged:: 0.18
           Added float values for fractions.

    min_weight_fraction_leaf : float, optional (default=0.)
        The minimum weighted fraction of the sum total of weights (of all
        the input samples) required to be at a leaf node. Samples have
        equal weight when sample_weight is not provided.

    max_depth : integer, optional (default=3)
        maximum depth of the individual regression estimators. The maximum
        depth limits the number of nodes in the tree. Tune this parameter
        for best performance; the best value depends on the interaction
        of the input variables.

    min_impurity_decrease : float, optional (default=0.)
        A node will be split if this split induces a decrease of the impurity
        greater than or equal to this value.

        The weighted impurity decrease equation is the following::

            N_t / N * (impurity - N_t_R / N_t * right_impurity
                                - N_t_L / N_t * left_impurity)

        where ``N`` is the total number of samples, ``N_t`` is the number of
        samples at the current node, ``N_t_L`` is the number of samples in the
        left child, and ``N_t_R`` is the number of samples in the right child.

        ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,
        if ``sample_weight`` is passed.

        .. versionadded:: 0.19

    min_impurity_split : float, (default=1e-7)
        Threshold for early stopping in tree growth. A node will split
        if its impurity is above the threshold, otherwise it is a leaf.

        .. deprecated:: 0.19
           ``min_impurity_split`` has been deprecated in favor of
           ``min_impurity_decrease`` in 0.19. The default value of
           ``min_impurity_split`` will change from 1e-7 to 0 in 0.23 and it
           will be removed in 0.25. Use ``min_impurity_decrease`` instead.

    init : estimator, optional (default=None)
        An estimator object that is used to compute the initial
        predictions. ``init`` has to provide ``fit`` and ``predict``.
        If None it uses ``loss.init_estimator``.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    max_features : int, float, string or None, optional (default=None)
        The number of features to consider when looking for the best split:

        - If int, then consider `max_features` features at each split.
        - If float, then `max_features` is a fraction and
          `int(max_features * n_features)` features are considered at each
          split.
        - If "auto", then `max_features=n_features`.
        - If "sqrt", then `max_features=sqrt(n_features)`.
        - If "log2", then `max_features=log2(n_features)`.
        - If None, then `max_features=n_features`.

        Choosing `max_features < n_features` leads to a reduction of variance
        and an increase in bias.

        Note: the search for a split does not stop until at least one
        valid partition of the node samples is found, even if it requires to
        effectively inspect more than ``max_features`` features.

    alpha : float (default=0.9)
        The alpha-quantile of the huber loss function and the quantile
        loss function. Only if ``loss='huber'`` or ``loss='quantile'``.

    verbose : int, default: 0
        Enable verbose output. If 1 then it prints progress and performance
        once in a while (the more trees the lower the frequency). If greater
        than 1 then it prints progress and performance for every tree.

    max_leaf_nodes : int or None, optional (default=None)
        Grow trees with ``max_leaf_nodes`` in best-first fashion.
        Best nodes are defined as relative reduction in impurity.
        If None then unlimited number of leaf nodes.

    warm_start : bool, default: False
        When set to ``True``, reuse the solution of the previous call to fit
        and add more estimators to the ensemble, otherwise, just erase the
        previous solution. See :term:`the Glossary <warm_start>`.

    presort : bool or 'auto', optional (default='auto')
        Whether to presort the data to speed up the finding of best splits in
        fitting. Auto mode by default will use presorting on dense data and
        default to normal sorting on sparse data. Setting presort to true on
        sparse data will raise an error.

        .. versionadded:: 0.17
           optional parameter *presort*.

    validation_fraction : float, optional, default 0.1
        The proportion of training data to set aside as validation set for
        early stopping. Must be between 0 and 1.
        Only used if ``n_iter_no_change`` is set to an integer.

        .. versionadded:: 0.20

    n_iter_no_change : int, default None
        ``n_iter_no_change`` is used to decide if early stopping will be used
        to terminate training when validation score is not improving. By
        default it is set to None to disable early stopping. If set to a
        number, it will set aside ``validation_fraction`` size of the training
        data as validation and terminate training when validation score is not
        improving in all of the previous ``n_iter_no_change`` numbers of
        iterations.

        .. versionadded:: 0.20

    tol : float, optional, default 1e-4
        Tolerance for the early stopping. When the loss is not improving
        by at least tol for ``n_iter_no_change`` iterations (if set to a
        number), the training stops.

        .. versionadded:: 0.20


    Attributes
    ----------
    feature_importances_ : array, shape (n_features,)
        The feature importances (the higher, the more important the feature).

    oob_improvement_ : array, shape (n_estimators,)
        The improvement in loss (= deviance) on the out-of-bag samples
        relative to the previous iteration.
        ``oob_improvement_[0]`` is the improvement in
        loss of the first stage over the ``init`` estimator.

    train_score_ : array, shape (n_estimators,)
        The i-th score ``train_score_[i]`` is the deviance (= loss) of the
        model at iteration ``i`` on the in-bag sample.
        If ``subsample == 1`` this is the deviance on the training data.

    loss_ : LossFunction
        The concrete ``LossFunction`` object.

    init_ : estimator
        The estimator that provides the initial predictions.
        Set via the ``init`` argument or ``loss.init_estimator``.

    estimators_ : array of DecisionTreeRegressor, shape (n_estimators, 1)
        The collection of fitted sub-estimators.

    Notes
    -----
    The features are always randomly permuted at each split. Therefore,
    the best found split may vary, even with the same training data and
    ``max_features=n_features``, if the improvement of the criterion is
    identical for several splits enumerated during the search of the best
    split. To obtain a deterministic behaviour during fitting,
    ``random_state`` has to be fixed.

    See also
    --------
    DecisionTreeRegressor, RandomForestRegressor

    References
    ----------
    J. Friedman, Greedy Function Approximation: A Gradient Boosting
    Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.

    J. Friedman, Stochastic Gradient Boosting, 1999

    T. Hastie, R. Tibshirani and J. Friedman.
    Elements of Statistical Learning Ed. 2, Springer, 2009.
    R   R   R   R'   g?id   g      ?R.  i   i   g        i   g?i    R   g-C6?c      -   C  s   t  t |   j d | d | d | d | d | d | d | d |	 d	 | d
 | d | d |
 d | d | d | d | d | d | d | d | d | d |  d  S(   NRt   R_   R   R   R   R   R   R   R   R   R   R   R   R   R!   R   R   R   R   R   R   R   (   Rh   R;  R#   (   R"   Rt   R_   R   R   R   R   R   R   R   R   R   R   R   R   R!   R   R   R   R   R   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR#   W	  s    	c         C  s1   t  | d t d d d d } |  j |  j   S(   s  Predict regression target for X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        y : array, shape (n_samples,)
            The predicted values.
        R,   R   R   R   R   (   R   R   R  Rj   (   R"   R(   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR1   n	  s    c         c  s)   x" |  j  |  D] } | j   Vq Wd S(   sG  Predict regression target at each stage for X.

        This method allows monitoring (i.e. determine error on testing set)
        after each stage.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, it will be converted to
            ``dtype=np.float32`` and if a sparse matrix is provided
            to a sparse ``csr_matrix``.

        Returns
        -------
        y : generator of array of shape (n_samples,)
            The predicted value of the input samples.
        N(   R  Rj   (   R"   R(   R)   (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR6  	  s    c         C  s?   t  t |   j |  } | j | j d |  j j d  } | S(   s  Apply trees in the ensemble to X, return leaf indices.

        .. versionadded:: 0.17

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input samples. Internally, its dtype will be converted to
            ``dtype=np.float32``. If a sparse matrix is provided, it will
            be converted to a sparse ``csr_matrix``.

        Returns
        -------
        X_leaves : array-like, shape (n_samples, n_estimators)
            For each datapoint x in X and for each tree in the ensemble,
            return the index of the leaf x ends up in each estimator.
        i    (   Rh   R;  RU   t   reshapeR.   R   (   R"   R(   R*  (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyRU   	  s    #(   R   R   R   R'   N(
   R2   R3   R4   R   R$   Re   R#   R1   R6  RU   (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyR;  a  s   						(L   R4   t
   __future__R    R   t   abcR   R   t   baseR   R   R   t	   externalsR   t   _gradient_boostingR   R	   R
   R   t   numpyR%   t   scipy.sparseR   R   R   t   scipy.specialR   R   t   model_selectionR   t	   tree.treeR   t
   tree._treeR   R   t   utilsR   R   R   R   R   R   t   utils.fixesR   t   utils.statsR   t   utils.validationR   t   utils.multiclassR   t
   exceptionsR   R   R   R5   R9   R@   RA   RF   t   with_metaclassRK   Rg   Ri   Rk   Ro   Rz   R}   R   R   R   R$   R   R   R   R   R-  R;  (    (    (    sA   lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyt   <module>   s~   5)2+."k"S8gM"[_W

K"  4 