ó
‡ˆ\c           @   s•  d  Z  d d l m Z m Z d d l Z d d l m Z d d l m Z m	 Z	 m
 Z
 m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z d d l m Z m Z m Z d d l m  Z  d d l! m" Z" m# Z# d d l$ m% Z% m& Z& d d g Z' d e j( e e ƒ f d „  ƒ  YZ) d „  Z* d e) e f d „  ƒ  YZ+ d e) e	 f d „  ƒ  YZ, d S(   s/  Weight Boosting

This module contains weight boosting estimators for both classification and
regression.

The module structure is the following:

- The ``BaseWeightBoosting`` base class implements a common ``fit`` method
  for all the estimators in the module. Regression and classification
  only differ from each other in the loss function that is optimized.

- ``AdaBoostClassifier`` implements adaptive boosting (AdaBoost-SAMME) for
  classification problems.

- ``AdaBoostRegressor`` implements adaptive boosting (AdaBoost.R2) for
  regression problems.
iÿÿÿÿ(   t   ABCMetat   abstractmethodNi   (   t   BaseEnsemblei   (   t   ClassifierMixint   RegressorMixint   is_regressort   is_classifier(   t   six(   t   zip(   t   xrange(   t
   BaseForest(   t   DecisionTreeClassifiert   DecisionTreeRegressor(   t   BaseDecisionTree(   t   DTYPE(   t   check_arrayt	   check_X_yt   check_random_state(   t   stable_cumsum(   t   accuracy_scoret   r2_score(   t   has_fit_parametert   check_is_fittedt   AdaBoostClassifiert   AdaBoostRegressort   BaseWeightBoostingc           B   sn   e  Z d  Z e d	 d e ƒ  d d	 d „ ƒ Z d	 d „ Z e d „  ƒ Z d	 d „ Z	 e
 d „  ƒ Z d „  Z RS(
   s   Base class for AdaBoost estimators.

    Warning: This class should not be used directly. Use derived classes
    instead.
    i2   g      ð?c         C   s;   t  t |  ƒ j d | d | d | ƒ | |  _ | |  _ d  S(   Nt   base_estimatort   n_estimatorst   estimator_params(   t   superR   t   __init__t   learning_ratet   random_state(   t   selfR   R   R   R   R    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   9   s    	c      
   C   s8  |  j  d k r t d ƒ ‚ n  |  j d k sE t |  j t t f ƒ rT t } d } n d } d d g } t | | d | d | d t	 |  ƒ ƒ\ } } | d k rÓ t
 j | j d d t
 j ƒ} d | j d | (nL t | d	 t ƒ} | | j d t
 j ƒ } | j ƒ  d k rt d
 ƒ ‚ n  |  j ƒ  g  |  _ t
 j |  j d t
 j ƒ|  _ t
 j |  j d t
 j ƒ|  _ t |  j ƒ } x´ t |  j ƒ D]£ } |  j | | | | | ƒ \ } } }	 | d k rÇPn  | |  j | <|	 |  j | <|	 d k rñPn  t
 j | ƒ }
 |
 d k rPn  | |  j d k  r| |
 :} qqW|  S(   sb  Build a boosted classifier/regressor from the training set (X, y).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. COO, DOK, and LIL are converted to CSR. The dtype is
            forced to DTYPE from tree._tree if the base classifier of this
            ensemble weighted boosting classifier is a tree or forest.

        y : array-like of shape = [n_samples]
            The target values (class labels in classification, real numbers in
            regression).

        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            1 / n_samples.

        Returns
        -------
        self : object
        i    s'   learning_rate must be greater than zerot   csct   csrt   accept_sparset   dtypet	   y_numericg      ð?t	   ensure_2dsA   Attempting to fit with a non-positive weighted number of samples.i   N(   R   t
   ValueErrorR   t   Nonet
   isinstanceR   R
   R   R   R   t   npt   emptyt   shapet   float64R   t   Falset   sumt   _validate_estimatort   estimators_t   zerosR   t   estimator_weights_t   onest   estimator_errors_R   R    t   ranget   _boost(   R!   t   Xt   yt   sample_weightR%   R$   R    t   iboostt   estimator_weightt   estimator_errort   sample_weight_sum(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   fitI   sT    	
	c         C   s   d S(   sž  Implement a single boost.

        Warning: This method needs to be overridden by subclasses.

        Parameters
        ----------
        iboost : int
            The index of the current boost iteration.

        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. COO, DOK, and LIL are converted to CSR.

        y : array-like of shape = [n_samples]
            The target values (class labels).

        sample_weight : array-like of shape = [n_samples]
            The current sample weights.

        random_state : RandomState
            The current random number generator

        Returns
        -------
        sample_weight : array-like of shape = [n_samples] or None
            The reweighted sample weights.
            If None then boosting has terminated early.

        estimator_weight : float
            The weight for the current boost.
            If None then boosting has terminated early.

        error : float
            The classification error for the current boost.
            If None then boosting has terminated early.
        N(    (   R!   R<   R9   R:   R;   R    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR8   ¨   s    &c         c   sU   xN |  j  | ƒ D]= } t |  ƒ r9 t | | d | ƒVq t | | d | ƒVq Wd S(   sÃ  Return staged scores for X, y.

        This generator method yields the ensemble score after each iteration of
        boosting and therefore allows monitoring, such as to determine the
        score on a test set after each boost.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        y : array-like, shape = [n_samples]
            Labels for X.

        sample_weight : array-like, shape = [n_samples], optional
            Sample weights.

        Returns
        -------
        z : float
        R;   N(   t   staged_predictR   R   R   (   R!   R9   R:   R;   t   y_pred(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   staged_scoreÐ   s    c         C   s‘   |  j  d k s$ t |  j  ƒ d k r3 t d ƒ ‚ n  y: |  j j ƒ  } t d „  t |  j |  j  ƒ Dƒ ƒ | SWn t k
 rŒ t d ƒ ‚ n Xd S(   s¼   Return the feature importances (the higher, the more important the
           feature).

        Returns
        -------
        feature_importances_ : array, shape = [n_features]
        i    s?   Estimator not fitted, call `fit` before `feature_importances_`.c         s   s"   |  ] \ } } | | j  Vq d  S(   N(   t   feature_importances_(   t   .0t   weightt   clf(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pys	   <genexpr>ü   s    si   Unable to compute feature importances since base_estimator does not have a feature_importances_ attributeN(   R2   R)   t   lenR(   R4   R0   R   t   AttributeError(   R!   t   norm(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRD   í   s    	$		c         C   sa   |  j  d k s' t |  j  t t f ƒ rB t | d d d t ƒ} n t | d d d d g ƒ} | S(   s%   Ensure that X is in the proper formatR$   R#   R%   R"   t   cooN(   R   R)   R*   R   R
   R   R   (   R!   R9   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   _validate_X_predict  s    	N(   t   __name__t
   __module__t   __doc__R   R)   t   tupleR   R@   R8   RC   t   propertyRD   RL   (    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   2   s   
_(c         C   s}   |  j  | ƒ } t j | t j | j ƒ j d d | ƒt j | ƒ } | d | d | | j d d ƒ d d … t j	 f S(   s°   Calculate algorithm 4, step 2, equation c) of Zhu et al [1].

    References
    ----------
    .. [1] J. Zhu, H. Zou, S. Rosset, T. Hastie, "Multi-class AdaBoost", 2009.

    t   outi   g      ð?t   axisN(
   t   predict_probaR+   t   clipt   finfoR%   t   epsR)   t   logR0   t   newaxis(   t	   estimatort	   n_classesR9   t   probat	   log_proba(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   _samme_proba  s
    (c           B   s•   e  Z d  Z d d d d d d „ Z d d „ Z d „  Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s¬  An AdaBoost classifier.

    An AdaBoost [1] classifier is a meta-estimator that begins by fitting a
    classifier on the original dataset and then fits additional copies of the
    classifier on the same dataset but where the weights of incorrectly
    classified instances are adjusted such that subsequent classifiers focus
    more on difficult cases.

    This class implements the algorithm known as AdaBoost-SAMME [2].

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

    Parameters
    ----------
    base_estimator : object, optional (default=None)
        The base estimator from which the boosted ensemble is built.
        Support for sample weighting is required, as well as proper
        ``classes_`` and ``n_classes_`` attributes. If ``None``, then
        the base estimator is ``DecisionTreeClassifier(max_depth=1)``

    n_estimators : integer, optional (default=50)
        The maximum number of estimators at which boosting is terminated.
        In case of perfect fit, the learning procedure is stopped early.

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

    algorithm : {'SAMME', 'SAMME.R'}, optional (default='SAMME.R')
        If 'SAMME.R' then use the SAMME.R real boosting algorithm.
        ``base_estimator`` must support calculation of class probabilities.
        If 'SAMME' then use the SAMME discrete boosting algorithm.
        The SAMME.R algorithm typically converges faster than SAMME,
        achieving a lower test error with fewer boosting iterations.

    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`.

    Attributes
    ----------
    estimators_ : list of classifiers
        The collection of fitted sub-estimators.

    classes_ : array of shape = [n_classes]
        The classes labels.

    n_classes_ : int
        The number of classes.

    estimator_weights_ : array of floats
        Weights for each estimator in the boosted ensemble.

    estimator_errors_ : array of floats
        Classification error for each estimator in the boosted
        ensemble.

    feature_importances_ : array of shape = [n_features]
        The feature importances if supported by the ``base_estimator``.

    See also
    --------
    AdaBoostRegressor, GradientBoostingClassifier,
    sklearn.tree.DecisionTreeClassifier

    References
    ----------
    .. [1] Y. Freund, R. Schapire, "A Decision-Theoretic Generalization of
           on-Line Learning and an Application to Boosting", 1995.

    .. [2] J. Zhu, H. Zou, S. Rosset, T. Hastie, "Multi-class AdaBoost", 2009.

    i2   g      ð?s   SAMME.Rc      	   C   s8   t  t |  ƒ j d | d | d | d | ƒ | |  _ d  S(   NR   R   R   R    (   R   R   R   t	   algorithm(   R!   R   R   R   R_   R    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   t  s    c         C   sA   |  j  d k r% t d |  j  ƒ ‚ n  t t |  ƒ j | | | ƒ S(   s}  Build a boosted classifier from the training set (X, y).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        y : array-like of shape = [n_samples]
            The target values (class labels).

        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            ``1 / n_samples``.

        Returns
        -------
        self : object
        t   SAMMEs   SAMME.Rs   algorithm %s is not supported(   R`   s   SAMME.R(   R_   R(   R   R   R@   (   R!   R9   R:   R;   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR@   ƒ  s    c         C   s‡   t  t |  ƒ j d t d d ƒ ƒ |  j d k rU t |  j d ƒ sU t d ƒ ‚ qU n  t |  j d ƒ sƒ t	 d |  j j
 j ƒ ‚ n  d	 S(
   s:   Check the estimator and set the base_estimator_ attribute.t   defaultt	   max_depthi   s   SAMME.RRT   sÚ   AdaBoostClassifier with algorithm='SAMME.R' requires that the weak learner supports the calculation of class probabilities with a predict_proba method.
Please change the base estimator or set algorithm='SAMME' instead.R;   s!   %s doesn't support sample_weight.N(   R   R   R1   R   R_   t   hasattrt   base_estimator_t	   TypeErrorR   R(   t	   __class__RM   (   R!   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR1   ž  s    c         C   sE   |  j  d k r( |  j | | | | | ƒ S|  j | | | | | ƒ Sd S(   s  Implement a single boost.

        Perform a single boost according to the real multi-class SAMME.R
        algorithm or to the discrete SAMME algorithm and return the updated
        sample weights.

        Parameters
        ----------
        iboost : int
            The index of the current boost iteration.

        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        y : array-like of shape = [n_samples]
            The target values (class labels).

        sample_weight : array-like of shape = [n_samples]
            The current sample weights.

        random_state : RandomState
            The current random number generator

        Returns
        -------
        sample_weight : array-like of shape = [n_samples] or None
            The reweighted sample weights.
            If None then boosting has terminated early.

        estimator_weight : float
            The weight for the current boost.
            If None then boosting has terminated early.

        estimator_error : float
            The classification error for the current boost.
            If None then boosting has terminated early.
        s   SAMME.RN(   R_   t   _boost_realt   _boost_discrete(   R!   R<   R9   R:   R;   R    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR8   °  s    'c         C   sâ  |  j  d | ƒ } | j | | d | ƒ| j | ƒ } | d k rm t | d d ƒ |  _ t |  j ƒ |  _ n  |  j j t	 j
 | d d ƒd d ƒ} | | k }	 t	 j t	 j |	 d | d d ƒƒ }
 |
 d k rÝ | d d	 f S|  j } |  j } t	 j d
 | d d g ƒ } | j | | d d … t	 j f k ƒ } | } t	 j | t	 j | j ƒ j d d | ƒd
 |  j | d | | t	 j | ƒ j d d ƒ } | |  j d k sÕ| t	 j | | d k | d k  Bƒ 9} n  | d |
 f S(   s:   Implement a single boost using the SAMME.R real algorithm.R    R;   i    t   classes_RS   i   t   weightsg      ð?g        g      ð¿NRR   (   t   _make_estimatorR@   RT   t   getattrR)   Ri   RH   t
   n_classes_t   takeR+   t   argmaxt   meant   averaget   arrayRY   RU   RV   R%   RW   R   RX   R0   R   t   exp(   R!   R<   R9   R:   R;   R    RZ   t   y_predict_probat	   y_predictt	   incorrectR>   R[   t   classest   y_codest   y_codingR\   R=   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRg   Þ  s2    				((6	c         C   sˆ  |  j  d | ƒ } | j | | d | ƒ| j | ƒ } | d k rm t | d d ƒ |  _ t |  j ƒ |  _ n  | | k } t j	 t j
 | d | d d ƒƒ }	 |	 d k r¶ | d d f S|  j }
 |	 d d |
 k r|  j j d	 ƒ t |  j ƒ d k rt d
 ƒ ‚ n  d S|  j t j d |	 |	 ƒ t j |
 d ƒ } | |  j d k s{| t j | | | d k | d k  Bƒ 9} n  | | |	 f S(   s<   Implement a single boost using the SAMME discrete algorithm.R    R;   i    Ri   Rj   RS   g      ð?g        iÿÿÿÿs\   BaseClassifier in AdaBoostClassifier ensemble is worse than random, ensemble can not be fit.i   N(   NNN(   Rk   R@   t   predictRl   R)   Ri   RH   Rm   R+   Rp   Rq   R2   t   popR(   R   RX   R   Rs   (   R!   R<   R9   R:   R;   R    RZ   Ru   Rv   R>   R[   R=   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRh     s2    		c         C   s_   |  j  | ƒ } |  j d k r: |  j j | d k d d ƒS|  j j t j | d d ƒd d ƒS(   s  Predict classes for X.

        The predicted class of an input sample is computed as the weighted mean
        prediction of the classifiers in the ensemble.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        y : array of shape = [n_samples]
            The predicted classes.
        i   i    RS   i   (   t   decision_functionRm   Ri   Rn   R+   Ro   (   R!   R9   t   pred(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRz   H  s    c         c   s¨   |  j  } |  j } | d k r^ xƒ |  j | ƒ D]) } t j | j | d k d d ƒƒ Vq. WnF xC |  j | ƒ D]2 } t j | j t j | d d ƒd d ƒƒ Vqn Wd S(   s}  Return staged predictions for X.

        The predicted class of an input sample is computed as the weighted mean
        prediction of the classifiers in the ensemble.

        This generator method yields the ensemble prediction after each
        iteration of boosting and therefore allows monitoring, such as to
        determine the prediction on a test set after each boost.

        Parameters
        ----------
        X : array-like of shape = [n_samples, n_features]
            The input samples.

        Returns
        -------
        y : generator of array, shape = [n_samples]
            The predicted classes.
        i   i    RS   i   N(   Rm   Ri   t   staged_decision_functionR+   Rr   Rn   Ro   (   R!   R9   R[   Rw   R}   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRA   `  s    		*c            sø   t  |  d ƒ |  j ˆ  ƒ ‰  |  j ‰ |  j d d … t j f ‰ d } |  j d k r{ t ‡  ‡ f d †  |  j	 Dƒ ƒ } n. t ‡  ‡ f d †  t
 |  j	 |  j ƒ Dƒ ƒ } | |  j j ƒ  :} ˆ d k rô | d d … d f c d 9<| j d	 d
 ƒ S| S(   s  Compute the decision function of ``X``.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        score : array, shape = [n_samples, k]
            The decision function of the input samples. The order of
            outputs is the same of that of the `classes_` attribute.
            Binary classification is a special cases with ``k == 1``,
            otherwise ``k==n_classes``. For binary classification,
            values closer to -1 or 1 mean more like the first or second
            class in ``classes_``, respectively.
        Rm   Ns   SAMME.Rc         3   s!   |  ] } t  | ˆ ˆ  ƒ Vq d  S(   N(   R^   (   RE   RZ   (   R9   R[   (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pys	   <genexpr>œ  s   c         3   s1   |  ]' \ } } | j  ˆ  ƒ ˆ k j | Vq d  S(   N(   Rz   t   T(   RE   RZ   t   w(   R9   Rw   (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pys	   <genexpr>Ÿ  s   i   i    iÿÿÿÿRS   i   (   R   RL   Rm   Ri   R+   RY   R)   R_   R0   R2   R   R4   (   R!   R9   R}   (    (   R9   Rw   R[   s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR|   €  s     		c   
      c   s>  t  |  d ƒ |  j | ƒ } |  j } |  j d d … t j f } d } d } xê t |  j |  j	 ƒ D]Ó \ } } | | 7} |  j
 d k r t | | | ƒ } n" | j | ƒ } | | k j | } | d k rÔ | } n
 | | 7} | d k r-t j | ƒ }	 |	 d d … d f c d 9<|	 | j d d	 ƒ Vqc | | Vqc Wd S(
   s  Compute decision function of ``X`` for each boosting iteration.

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

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        score : generator of array, shape = [n_samples, k]
            The decision function of the input samples. The order of
            outputs is the same of that of the `classes_` attribute.
            Binary classification is a special cases with ``k == 1``,
            otherwise ``k==n_classes``. For binary classification,
            values closer to -1 or 1 mean more like the first or second
            class in ``classes_``, respectively.
        Rm   Ng        s   SAMME.Ri   i    iÿÿÿÿRS   i   (   R   RL   Rm   Ri   R+   RY   R)   R   R4   R2   R_   R^   Rz   R   t   copyR0   (
   R!   R9   R[   Rw   R}   RJ   RF   RZ   t   current_predt   tmp_pred(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR~   ©  s*    	
	
c            s  t  |  d ƒ |  j ‰ |  j ˆ  ƒ ‰  ˆ d k rK t j ˆ  j d d f ƒ S|  j d k r t ‡  ‡ f d †  |  j Dƒ ƒ } n+ t ‡  f d †  t	 |  j |  j
 ƒ Dƒ ƒ } | |  j
 j ƒ  :} t j d ˆ d | ƒ } | j d d ƒ d	 d	 … t j f } d | | d
 k <| | :} | S(   s¶  Predict class probabilities for X.

        The predicted class probabilities of an input sample is computed as
        the weighted mean predicted class probabilities of the classifiers
        in the ensemble.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        p : array of shape = [n_samples, n_classes]
            The class probabilities of the input samples. The order of
            outputs is the same of that of the `classes_` attribute.
        Rm   i   i    s   SAMME.Rc         3   s!   |  ] } t  | ˆ ˆ  ƒ Vq d  S(   N(   R^   (   RE   RZ   (   R9   R[   (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pys	   <genexpr>û  s   c         3   s(   |  ] \ } } | j  ˆ  ƒ | Vq d  S(   N(   RT   (   RE   RZ   R€   (   R9   (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pys	   <genexpr>þ  s   g      ð?RS   Ng        (   R   Rm   RL   R+   R5   R-   R_   R0   R2   R   R4   Rs   RY   (   R!   R9   R\   t
   normalizer(    (   R9   R[   s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRT   Þ  s"    		%
c   
      c   s  |  j  | ƒ } |  j } d } d } xæ t |  j |  j ƒ D]Ï \ } } | | 7} |  j d k rt t | | | ƒ } n | j | ƒ | } | d k rœ | } n
 | | 7} t	 j
 d | d | | ƒ } | j d d ƒ d d … t	 j f }	 d |	 |	 d k <| |	 :} | Vq: Wd S(   s¸  Predict class probabilities for X.

        The predicted class probabilities of an input sample is computed as
        the weighted mean predicted class probabilities of the classifiers
        in the ensemble.

        This generator method yields the ensemble predicted class probabilities
        after each iteration of boosting and therefore allows monitoring, such
        as to determine the predicted class probabilities on a test set after
        each boost.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        p : generator of array, shape = [n_samples]
            The class probabilities of the input samples. The order of
            outputs is the same of that of the `classes_` attribute.
        g        s   SAMME.Rg      ð?i   RS   N(   RL   Rm   R)   R   R4   R2   R_   R^   RT   R+   Rs   R0   RY   (
   R!   R9   R[   R\   RJ   RF   RZ   t   current_probat
   real_probaR„   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   staged_predict_proba
  s$    	
	
%
c         C   s   t  j |  j | ƒ ƒ S(   sÂ  Predict class log-probabilities for X.

        The predicted class log-probabilities of an input sample is computed as
        the weighted mean predicted class log-probabilities of the classifiers
        in the ensemble.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        p : array of shape = [n_samples, n_classes]
            The class probabilities of the input samples. The order of
            outputs is the same of that of the `classes_` attribute.
        (   R+   RX   RT   (   R!   R9   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   predict_log_proba>  s    N(   RM   RN   RO   R)   R   R@   R1   R8   Rg   Rh   Rz   RA   R|   R~   RT   R‡   Rˆ   (    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   '  s$   L
		.	:	0		 	)	5	,	4c           B   s_   e  Z d  Z d d d d d d „ Z d d „ Z d „  Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 RS(   sú	  An AdaBoost regressor.

    An AdaBoost [1] regressor is a meta-estimator that begins by fitting a
    regressor on the original dataset and then fits additional copies of the
    regressor on the same dataset but where the weights of instances are
    adjusted according to the error of the current prediction. As such,
    subsequent regressors focus more on difficult cases.

    This class implements the algorithm known as AdaBoost.R2 [2].

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

    Parameters
    ----------
    base_estimator : object, optional (default=None)
        The base estimator from which the boosted ensemble is built.
        Support for sample weighting is required. If ``None``, then
        the base estimator is ``DecisionTreeRegressor(max_depth=3)``

    n_estimators : integer, optional (default=50)
        The maximum number of estimators at which boosting is terminated.
        In case of perfect fit, the learning procedure is stopped early.

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

    loss : {'linear', 'square', 'exponential'}, optional (default='linear')
        The loss function to use when updating the weights after each
        boosting iteration.

    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`.

    Attributes
    ----------
    estimators_ : list of classifiers
        The collection of fitted sub-estimators.

    estimator_weights_ : array of floats
        Weights for each estimator in the boosted ensemble.

    estimator_errors_ : array of floats
        Regression error for each estimator in the boosted ensemble.

    feature_importances_ : array of shape = [n_features]
        The feature importances if supported by the ``base_estimator``.

    See also
    --------
    AdaBoostClassifier, GradientBoostingRegressor,
    sklearn.tree.DecisionTreeRegressor

    References
    ----------
    .. [1] Y. Freund, R. Schapire, "A Decision-Theoretic Generalization of
           on-Line Learning and an Application to Boosting", 1995.

    .. [2] H. Drucker, "Improving Regressors using Boosting Techniques", 1997.

    i2   g      ð?t   linearc      	   C   sA   t  t |  ƒ j d | d | d | d | ƒ | |  _ | |  _ d  S(   NR   R   R   R    (   R   R   R   t   lossR    (   R!   R   R   R   RŠ   R    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   –  s    	c         C   s:   |  j  d k r t d ƒ ‚ n  t t |  ƒ j | | | ƒ S(   sx  Build a boosted regressor from the training set (X, y).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        y : array-like of shape = [n_samples]
            The target values (real numbers).

        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            1 / n_samples.

        Returns
        -------
        self : object
        R‰   t   squaret   exponentials1   loss must be 'linear', 'square', or 'exponential'(   R‰   R‹   RŒ   (   RŠ   R(   R   R   R@   (   R!   R9   R:   R;   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR@   ¦  s    c         C   s&   t  t |  ƒ j d t d d ƒ ƒ d S(   s:   Check the estimator and set the base_estimator_ attribute.Ra   Rb   i   N(   R   R   R1   R   (   R!   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR1   Â  s    c         C   sÚ  |  j  d | ƒ } t | ƒ } | | d :} | j | j d ƒ } | j | d d ƒ}	 t j |	 d t ƒ}	 | j | |	 | |	 ƒ | j	 | ƒ }
 t j
 |
 | ƒ } | j ƒ  } | d k rË | | :} n  |  j d k rç | d	 C} n& |  j d
 k rd t j | ƒ } n  | | j ƒ  } | d k r6| d d f S| d k rnt |  j ƒ d k rj|  j j d ƒ n  d S| d | } |  j t j d | ƒ } | |  j d k sÍ| t j | d | |  j ƒ 9} n  | | | f S(   s  Implement a single boost for regression

        Perform a single boost according to the AdaBoost.R2 algorithm and
        return the updated sample weights.

        Parameters
        ----------
        iboost : int
            The index of the current boost iteration.

        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        y : array-like of shape = [n_samples]
            The target values (class labels in classification, real numbers in
            regression).

        sample_weight : array-like of shape = [n_samples]
            The current sample weights.

        random_state : RandomState
            The current random number generator

        Returns
        -------
        sample_weight : array-like of shape = [n_samples] or None
            The reweighted sample weights.
            If None then boosting has terminated early.

        estimator_weight : float
            The weight for the current boost.
            If None then boosting has terminated early.

        estimator_error : float
            The regression error for the current boost.
            If None then boosting has terminated early.
        R    iÿÿÿÿi    t   sidet   rightR   g        R‹   i   RŒ   g      ð?g      à?i   N(   NNN(   Rk   R   t   random_sampleR-   t   searchsortedR+   Rr   R/   R@   Rz   t   abst   maxRŠ   Rs   R0   RH   R2   R{   R)   R   RX   R   t   power(   R!   R<   R9   R:   R;   R    RZ   t   cdft   uniform_samplest   bootstrap_idxRu   t
   error_vectt	   error_maxR>   t   betaR=   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR8   Ç  s<    '	c   
      C   sæ   t  j g  |  j |  D] } | j | ƒ ^ q ƒ j } t  j | d d ƒ} t |  j | d d ƒ} | d | d  d  … d f d  d  … t  j f k } | j	 d d ƒ } | t  j
 | j d ƒ | f }	 | t  j
 | j d ƒ |	 f S(   NRS   i   g      à?iÿÿÿÿi    (   R+   Rr   R2   Rz   R   t   argsortR   R4   RY   Ro   t   arangeR-   (
   R!   R9   t   limitt   estt   predictionst
   sorted_idxt
   weight_cdft   median_or_abovet
   median_idxt   median_estimators(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   _get_median_predict"  s    /3 c         C   s5   t  |  d ƒ |  j | ƒ } |  j | t |  j ƒ ƒ S(   s-  Predict regression value for X.

        The predicted regression value of an input sample is computed
        as the weighted median prediction of the classifiers in the ensemble.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        y : array of shape = [n_samples]
            The predicted regression values.
        R4   (   R   RL   R¤   RH   R2   (   R!   R9   (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRz   4  s    c         c   sW   t  |  d ƒ |  j | ƒ } x4 t |  j d ƒ D]  \ } } |  j | d | ƒVq/ Wd S(   s  Return staged predictions for X.

        The predicted regression value of an input sample is computed
        as the weighted median prediction of the classifiers in the ensemble.

        This generator method yields the ensemble prediction after each
        iteration of boosting and therefore allows monitoring, such as to
        determine the prediction on a test set after each boost.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.

        Returns
        -------
        y : generator of array, shape = [n_samples]
            The predicted regression values.
        R4   i   Rœ   N(   R   RL   t	   enumerateR2   R¤   (   R!   R9   t   it   _(    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyRA   J  s    N(   RM   RN   RO   R)   R   R@   R1   R8   R¤   Rz   RA   (    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyR   T  s   A		[		(-   RO   t   abcR    R   t   numpyR+   t   baseR   R   R   R   R   t	   externalsR   t   externals.six.movesR   R	   R7   t   forestR
   t   treeR   R   t	   tree.treeR   t
   tree._treeR   t   utilsR   R   R   t   utils.extmathR   t   metricsR   R   t   sklearn.utils.validationR   R   t   __all__t   with_metaclassR   R^   R   R   (    (    (    s?   lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.pyt   <module>   s.   	"	"á	ÿ ÿ /