ó
‡ˆ\c           @   s!  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l 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
 l m Z m Z d d l m Z d d l m Z d d l m Z m Z d d l m Z m Z d d l m Z m  Z  m! Z! m" Z" d d l m# Z# m$ Z$ d d l% m& Z& d d l' m( Z( d d l) m* Z* d d l+ m, Z, m- Z- d d g Z. e j/ e j0 ƒ j1 Z2 d „  Z3 d „  Z4 d „  Z5 d „  Z6 d „  Z7 d „  Z8 d „  Z9 d e e e ƒ f d „  ƒ  YZ: d e: e f d  „  ƒ  YZ; d e: e f d! „  ƒ  YZ< d S("   s   Bagging meta-estimator.iÿÿÿÿ(   t   divisionN(   t   ABCMetat   abstractmethod(   t   warni   (   t   BaseEnsemblet   _partition_estimatorsi   (   t   ClassifierMixint   RegressorMixin(   t   Parallelt   delayed(   t   with_metaclass(   t   zip(   t   r2_scoret   accuracy_score(   t   DecisionTreeClassifiert   DecisionTreeRegressor(   t   check_random_statet	   check_X_yt   check_arrayt   column_or_1d(   t   indices_to_maskt   check_consistent_length(   t   if_delegate_has_method(   t   check_classification_targets(   t   sample_without_replacement(   t   has_fit_parametert   check_is_fittedt   BaggingClassifiert   BaggingRegressorc         C   s7   | r |  j  d | | ƒ } n t | | d |  ƒ} | S(   s   Draw randomly sampled indices.i    t   random_state(   t   randintR   (   R   t	   bootstrapt   n_populationt	   n_samplest   indices(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _generate_indices#   s
    	c   	      C   s@   t  |  ƒ }  t |  | | | ƒ } t |  | | | ƒ } | | f S(   s)   Randomly draw feature and sample indices.(   R   R#   (	   R   t   bootstrap_featurest   bootstrap_samplest
   n_featuresR!   t   max_featurest   max_samplest   feature_indicest   sample_indices(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _generate_bagging_indices/   s    		c      	   C   së  | j  \ } }	 | j }
 | j } | j } | j } t | j d ƒ } | rg | d	 k	 rg t d ƒ ‚ n  g  } g  } xkt	 |  ƒ D]]} | d k r« d | d |  | f GHn  t
 j j | | ƒ } | j d t d | ƒ } t | | | |	 | |
 | ƒ \ } } | r›| d	 k r$t
 j | f ƒ } n | j ƒ  } | rXt
 j | d | ƒ} | | 9} n t | | ƒ } d | | <| j | d	 d	 … | f | d | ƒn( | j | | d	 d	 … | f | | ƒ | j | ƒ | j | ƒ q€ W| | f S(
   sB   Private function used to build a batch of estimators within a job.t   sample_weights0   The base estimator doesn't support sample weighti   s?   Building estimator %d of %d for this parallel run (total %d)...t   appendR   t	   minlengthi    N(   t   shapet   _max_featurest   _max_samplesR   R$   R   t   base_estimator_t   Nonet
   ValueErrort   ranget   npt   randomt   RandomStatet   _make_estimatort   FalseR+   t   onest   copyt   bincountR   t   fitR-   (   t   n_estimatorst   ensemblet   Xt   yR,   t   seedst   total_n_estimatorst   verboseR!   R&   R'   R(   R   R$   t   support_sample_weightt
   estimatorst   estimators_featurest   iR   t	   estimatort   featuresR"   t   curr_sample_weightt   sample_countst   not_indices_mask(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _parallel_build_estimators?   sJ    							
)(c      	   C   s'  | j  d } t j | | f ƒ } xþ t |  | ƒ D]í \ } } t | d ƒ rÏ | j | d d … | f ƒ } | t | j ƒ k rŽ | | 7} q| d d … | j f c | d d … t t | j ƒ ƒ f 7<q2 | j	 | d d … | f ƒ }	 x. t | ƒ D]  }
 | |
 |	 |
 f c d 7<qû Wq2 W| S(   sB   Private function used to compute (proba-)predictions within a job.i    t   predict_probaNi   (
   R/   R6   t   zerosR   t   hasattrRP   t   lent   classes_R5   t   predict(   RG   RH   RA   t	   n_classesR!   t   probaRJ   RK   t   proba_estimatort   predictionsRI   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _parallel_predict_probaz   s    ("c         C   sZ  | j  d } t j | | f ƒ } | j t j ƒ t j | d t j ƒ} xt |  | ƒ D]÷ \ } } | j | d d … | f ƒ }	 | t	 | j
 ƒ k r° t j | |	 ƒ } q[ t j | d d … | j
 f |	 d d … t t	 | j
 ƒ ƒ f ƒ | d d … | j
 f <t j | | j
 ƒ }
 t j | d d … |
 f t j ƒ | d d … |
 f <q[ W| S(   s@   Private function used to compute log probabilities within a job.i    t   dtypeN(   R/   R6   t   emptyt   fillt   inft   aranget   intR   t   predict_log_probaRS   RT   t	   logaddexpR5   t	   setdiff1d(   RG   RH   RA   RV   R!   t	   log_probat   all_classesRJ   RK   t   log_proba_estimatort   missing(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _parallel_predict_log_proba”   s    ;!c            s#   t  ‡  f d †  t |  | ƒ Dƒ ƒ S(   s8   Private function used to compute decisions within a job.c         3   s4   |  ]* \ } } | j  ˆ  d  d  … | f ƒ Vq d  S(   N(   t   decision_function(   t   .0RJ   RK   (   RA   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>¯   s   (   t   sumR   (   RG   RH   RA   (    (   RA   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _parallel_decision_function­   s    c            s#   t  ‡  f d †  t |  | ƒ Dƒ ƒ S(   s:   Private function used to compute predictions within a job.c         3   s4   |  ]* \ } } | j  ˆ  d  d  … | f ƒ Vq d  S(   N(   RU   (   Rj   RJ   RK   (   RA   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>¶   s   (   Rk   R   (   RG   RH   RA   (    (   RA   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _parallel_predict_regression´   s    t   BaseBaggingc           B   sŒ   e  Z d  Z e d d d d e e e e d d d d „ ƒ Z d d „ Z d d d d „ Z	 e d „  ƒ Z
 d „  Z d	 „  Z e d
 „  ƒ Z RS(   s‚   Base class for Bagging meta-estimator.

    Warning: This class should not be used directly. Use derived classes
    instead.
    i
   g      ð?i    c         C   st   t  t |  ƒ j d | d | ƒ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _	 |
 |  _
 | |  _ d  S(   Nt   base_estimatorR?   (   t   superRn   t   __init__R(   R'   R   R$   t	   oob_scoret
   warm_startt   n_jobsR   RE   (   t   selfRo   R?   R(   R'   R   R$   Rr   Rs   Rt   R   RE   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRq   Â   s    								c         C   s   |  j  | | |  j d | ƒS(   s  Build a Bagging ensemble of estimators from the training
           set (X, y).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

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

        sample_weight : array-like, shape = [n_samples] or None
            Sample weights. If None, then samples are equally weighted.
            Note that this is supported only if the base estimator supports
            sample weighting.

        Returns
        -------
        self : object
        R,   (   t   _fitR(   (   Ru   RA   RB   R,   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR>   Ý   s    c      
      s#  t  ˆ j ƒ } t ˆ  ˆ d d g d d d t d t ƒ\ ‰  ‰ ˆ d k	 rm t ˆ d t ƒ‰ t ˆ ˆ ƒ n  ˆ  j \ } ˆ _	 | ˆ _
 ˆ j ˆ ƒ ‰ ˆ j ƒ  | d k	 r¼ | ˆ j _ n  | d k rÔ ˆ j } n5 t | t j t j f ƒ s	t | ˆ  j d ƒ } n  d | k  o'ˆ  j d k n s;t d ƒ ‚ n  | ˆ _ t ˆ j t j t j f ƒ rnˆ j } n4 t ˆ j t j ƒ r–ˆ j ˆ j	 } n t d	 ƒ ‚ d | k  o¼ˆ j	 k n sÐt d
 ƒ ‚ n  t d t | ƒ ƒ } | ˆ _ ˆ j rˆ j rt d ƒ ‚ n  ˆ j r1ˆ j r1t d ƒ ‚ n  t ˆ d ƒ rRˆ j rRˆ `  n  ˆ j slt ˆ d ƒ rg  ˆ _! g  ˆ _" n  ˆ j# t$ ˆ j! ƒ }	 |	 d k  rÈt d ˆ j# t$ ˆ j! ƒ f ƒ ‚ n |	 d k rât% d ƒ ˆ St& |	 ˆ j' ƒ \ }
 ‰ ‰ t( ˆ ƒ ‰ ˆ j rFt$ ˆ j! ƒ d k rF| j) t* d t$ ˆ j! ƒ ƒn  | j) t* d |	 ƒ‰ ˆ ˆ _+ t, d |
 d ˆ j- ƒ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  t. |
 ƒ Dƒ ƒ } ˆ j! t/ t0 j1 j2 d „  | Dƒ ƒ ƒ 7_! ˆ j" t/ t0 j1 j2 d „  | Dƒ ƒ ƒ 7_" ˆ j rˆ j3 ˆ  ˆ ƒ n  ˆ S(   sB  Build a Bagging ensemble of estimators from the training
           set (X, y).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

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

        max_samples : int or float, optional (default=None)
            Argument to use instead of self.max_samples.

        max_depth : int, optional (default=None)
            Override value used when constructing base estimator. Only
            supported if the base estimator has a max_depth parameter.

        sample_weight : array-like, shape = [n_samples] or None
            Sample weights. If None, then samples are equally weighted.
            Note that this is supported only if the base estimator supports
            sample weighting.

        Returns
        -------
        self : object
        t   csrt   cscR[   t   force_all_finitet   multi_outputt	   ensure_2di    s%   max_samples must be in (0, n_samples]s!   max_features must be int or floats'   max_features must be in (0, n_features]i   s6   Out of bag estimation only available if bootstrap=Trues6   Out of bag estimate only available if warm_start=Falset
   oob_score_t   estimators_sT   n_estimators=%d must be larger or equal to len(estimators_)=%d when warm_start==TruesJ   Warm-start fitting without increasing n_estimators does not fit new trees.t   sizeRt   RE   c         3   sS   |  ]I } t  t ƒ ˆ | ˆ ˆ  ˆ ˆ ˆ ˆ | ˆ | d  !ˆ d ˆ j ƒVq d S(   i   RE   N(   R	   RO   RE   (   Rj   RI   (   RA   R?   R,   RC   Ru   t   startsRD   RB   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>q  s   	c         s   s   |  ] } | d  Vq d S(   i    N(    (   Rj   t   t(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>~  s    c         s   s   |  ] } | d  Vq d S(   i   N(    (   Rj   R€   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>€  s    N(4   R   R   R   R3   R:   t   TrueR   R   R/   t   n_features_t
   _n_samplest   _validate_yt   _validate_estimatorR2   t	   max_depthR(   t
   isinstancet   numberst   IntegralR6   t   integerR`   R4   R1   R'   t   floatt   maxR0   R   Rr   Rs   RR   R|   R}   t   estimators_features_R?   RS   R   R   Rt   Rk   R   t   MAX_INTt   _seedsR   RE   R5   t   listt	   itertoolst   chaint   from_iterablet   _set_oob_score(   Ru   RA   RB   R(   R†   R,   R   R!   R'   t   n_more_estimatorsRt   t   all_results(    (   RA   R?   R,   RC   Ru   R   RD   RB   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRv   ö   s|    	
#				
	!		c         C   s   d S(   s+   Calculate out of bag predictions and score.N(    (   Ru   RA   RB   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR”   ‡  s    c         C   s@   t  | j ƒ d k s( | j d d k r8 t | d t ƒS| Sd  S(   Ni   R   (   RS   R/   R   R   (   Ru   RB   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR„   ‹  s    (c      	   c   sk   xd |  j  D]Y } t j j | ƒ } t | |  j |  j |  j |  j |  j	 |  j
 ƒ \ } } | | f Vq
 Wd  S(   N(   R   R6   R7   R8   R+   R$   R   R‚   Rƒ   R0   R1   (   Ru   t   seedR   R)   R*   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   _get_estimators_indices‘  s    c         C   s#   g  |  j  ƒ  D] \ } } | ^ q S(   sÂ  The subset of drawn samples for each base estimator.

        Returns a dynamically generated list of indices identifying
        the samples used for fitting each member of the ensemble, i.e.,
        the in-bag samples.

        Note: the list is re-created at each call to the property in order
        to reduce the object memory footprint by not storing the sampling
        data. Thus fetching the property may be slower than expected.
        (   R˜   (   Ru   t   _R*   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   estimators_samples_ž  s    N(   t   __name__t
   __module__t   __doc__R   R3   R   R:   Rq   R>   Rv   R”   R„   R˜   t   propertyRš   (    (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRn   »   s&   ‘		c           B   s†   e  Z d  Z d d d d e e e e d d d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z e d d ƒ d „  ƒ Z RS(   s  A Bagging classifier.

    A Bagging classifier is an ensemble meta-estimator that fits base
    classifiers each on random subsets of the original dataset and then
    aggregate their individual predictions (either by voting or by averaging)
    to form a final prediction. Such a meta-estimator can typically be used as
    a way to reduce the variance of a black-box estimator (e.g., a decision
    tree), by introducing randomization into its construction procedure and
    then making an ensemble out of it.

    This algorithm encompasses several works from the literature. When random
    subsets of the dataset are drawn as random subsets of the samples, then
    this algorithm is known as Pasting [1]_. If samples are drawn with
    replacement, then the method is known as Bagging [2]_. When random subsets
    of the dataset are drawn as random subsets of the features, then the method
    is known as Random Subspaces [3]_. Finally, when base estimators are built
    on subsets of both samples and features, then the method is known as
    Random Patches [4]_.

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

    Parameters
    ----------
    base_estimator : object or None, optional (default=None)
        The base estimator to fit on random subsets of the dataset.
        If None, then the base estimator is a decision tree.

    n_estimators : int, optional (default=10)
        The number of base estimators in the ensemble.

    max_samples : int or float, optional (default=1.0)
        The number of samples to draw from X to train each base estimator.

        - If int, then draw `max_samples` samples.
        - If float, then draw `max_samples * X.shape[0]` samples.

    max_features : int or float, optional (default=1.0)
        The number of features to draw from X to train each base estimator.

        - If int, then draw `max_features` features.
        - If float, then draw `max_features * X.shape[1]` features.

    bootstrap : boolean, optional (default=True)
        Whether samples are drawn with replacement. If False, sampling
        without replacement is performed.

    bootstrap_features : boolean, optional (default=False)
        Whether features are drawn with replacement.

    oob_score : bool, optional (default=False)
        Whether to use out-of-bag samples to estimate
        the generalization error.

    warm_start : bool, optional (default=False)
        When set to True, reuse the solution of the previous call to fit
        and add more estimators to the ensemble, otherwise, just fit
        a whole new ensemble. See :term:`the Glossary <warm_start>`.

        .. versionadded:: 0.17
           *warm_start* constructor parameter.

    n_jobs : int or None, optional (default=None)
        The number of jobs to run in parallel for both `fit` and `predict`.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

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

    verbose : int, optional (default=0)
        Controls the verbosity when fitting and predicting.

    Attributes
    ----------
    base_estimator_ : estimator
        The base estimator from which the ensemble is grown.

    estimators_ : list of estimators
        The collection of fitted base estimators.

    estimators_samples_ : list of arrays
        The subset of drawn samples (i.e., the in-bag samples) for each base
        estimator. Each subset is defined by an array of the indices selected.

    estimators_features_ : list of arrays
        The subset of drawn features for each base estimator.

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

    n_classes_ : int or list
        The number of classes.

    oob_score_ : float
        Score of the training dataset obtained using an out-of-bag estimate.

    oob_decision_function_ : array of shape = [n_samples, n_classes]
        Decision function computed with out-of-bag estimate on the training
        set. If n_estimators is small it might be possible that a data point
        was never left out during the bootstrap. In this case,
        `oob_decision_function_` might contain NaN.

    References
    ----------

    .. [1] L. Breiman, "Pasting small votes for classification in large
           databases and on-line", Machine Learning, 36(1), 85-103, 1999.

    .. [2] L. Breiman, "Bagging predictors", Machine Learning, 24(2), 123-140,
           1996.

    .. [3] T. Ho, "The random subspace method for constructing decision
           forests", Pattern Analysis and Machine Intelligence, 20(8), 832-844,
           1998.

    .. [4] G. Louppe and P. Geurts, "Ensembles on Random Patches", Machine
           Learning and Knowledge Discovery in Databases, 346-361, 2012.
    i
   g      ð?i    c         C   sV   t  t |  ƒ j | d | d | d | d | d | d | d | d |	 d	 |
 d
 | ƒ
d  S(   NR?   R(   R'   R   R$   Rr   Rs   Rt   R   RE   (   Rp   R   Rq   (   Ru   Ro   R?   R(   R'   R   R$   Rr   Rs   Rt   R   RE   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRq   )  s    c         C   s    t  t |  ƒ j d t ƒ  ƒ d S(   s:   Check the estimator and set the base_estimator_ attribute.t   defaultN(   Rp   R   R…   R   (   Ru   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR…   C  s    c      	   C   sÆ  | j  d } |  j } t j | | f ƒ } xt |  j |  j |  j ƒ D]ó \ } } } t | | ƒ }	 t	 | d ƒ r½ | |	 d  d  … f c | j
 | |	 d  d  … f d  d  … | f ƒ 7<qG | j | |	 d  d  … f d  d  … | f ƒ }
 d } xE t | ƒ D]7 } |	 | rÿ | | |
 | f c d 7<| d 7} qÿ qÿ WqG W| j d d ƒ d k j ƒ  rit d ƒ n  | | j d d ƒ d  d  … t j f } t | t j | d d ƒƒ } | |  _ | |  _ d  S(   Ni    RP   i   t   axiss{   Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.(   R/   t
   n_classes_R6   RQ   R   R}   Rš   R   R   RR   RP   RU   R5   Rk   t   anyR   t   newaxisR   t   argmaxt   oob_decision_function_R|   (   Ru   RA   RB   R!   R¡   RY   RJ   t   samplesRK   t   maskt   pt   jRI   t   oob_decision_functionRr   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR”   H  s.    	,/
&	c         C   sP   t  | d t ƒ} t | ƒ t j | d t ƒ\ |  _ } t |  j ƒ |  _ | S(   NR   t   return_inverse(   R   R   R   R6   t   uniqueRT   RS   R¡   (   Ru   RB   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR„   m  s
    
c         C   s4   |  j  | ƒ } |  j j t j | d d ƒd d ƒS(   s`  Predict class for X.

        The predicted class of an input sample is computed as the class with
        the highest mean predicted probability. If base estimators do not
        implement a ``predict_proba`` method, then it resorts to voting.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        y : array of shape = [n_samples]
            The predicted classes.
        R    i   i    (   RP   RT   t   takeR6   R¤   (   Ru   RA   t   predicted_probabilitiy(    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRU   u  s    c            sÛ   t  ˆ d ƒ t ˆ  d d d g d d d t ƒ‰  ˆ j ˆ  j d k rl t d j ˆ j ˆ  j d ƒ ƒ ‚ n  t ˆ j	 ˆ j
 ƒ \ } } ‰ t d	 | d
 ˆ j ƒ ‡  ‡ ‡ f d †  t | ƒ Dƒ ƒ } t | ƒ ˆ j	 } | S(   s›  Predict class probabilities for X.

        The predicted class probabilities of an input sample is computed as
        the mean predicted class probabilities of the base estimators in the
        ensemble. If base estimators do not implement a ``predict_proba``
        method, then it resorts to voting and the predicted class probabilities
        of an input sample represents the proportion of estimators predicting
        each class.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        p : array of shape = [n_samples, n_classes]
            The class probabilities of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.
        RT   t   accept_sparseRw   Rx   R[   Ry   i   sj   Number of features of the model must match the input. Model n_features is {0} and input n_features is {1}.Rt   RE   c         3   sY   |  ]O } t  t ƒ ˆ j ˆ | ˆ | d  !ˆ j ˆ | ˆ | d  !ˆ  ˆ j ƒ Vq d S(   i   N(   R	   RZ   R}   R   R¡   (   Rj   RI   (   RA   Ru   R   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>³  s   N(   R   R   R3   R:   R‚   R/   R4   t   formatR   R?   Rt   R   RE   R5   Rk   (   Ru   RA   Rt   R?   t	   all_probaRW   (    (   RA   Ru   R   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRP   ‹  s    			c            sJ  t  ˆ d ƒ t ˆ j d ƒ r0t ˆ  d d d g d d d t ƒ‰  ˆ j ˆ  j d k r~ t d	 j	 ˆ j ˆ  j d ƒ ƒ ‚ n  t
 ˆ j ˆ j ƒ \ } } ‰ t d
 | d ˆ j ƒ ‡  ‡ ‡ f d †  t | ƒ Dƒ ƒ } | d } x3 t d t | ƒ ƒ D] } t j | | | ƒ } qö W| t j ˆ j ƒ 8} | St j ˆ j ˆ  ƒ ƒ Sd S(   sÈ  Predict class log-probabilities for X.

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

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        p : array of 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_`.
        RT   Ra   R¯   Rw   Rx   R[   Ry   i   sj   Number of features of the model must match the input. Model n_features is {0} and input n_features is {1} Rt   RE   c         3   sY   |  ]O } t  t ƒ ˆ j ˆ | ˆ | d  !ˆ j ˆ | ˆ | d  !ˆ  ˆ j ƒ Vq d S(   i   N(   R	   Rh   R}   R   R¡   (   Rj   RI   (   RA   Ru   R   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>å  s   i    N(   R   RR   R2   R   R3   R:   R‚   R/   R4   R°   R   R?   Rt   R   RE   R5   RS   R6   Rb   t   logRP   (   Ru   RA   Rt   R?   t   all_log_probaRd   R©   (    (   RA   Ru   R   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRa   ¿  s&    		
t   delegateRo   c            sÛ   t  ˆ d ƒ t ˆ  d d d g d d d t ƒ‰  ˆ j ˆ  j d k rl t d j ˆ j ˆ  j d ƒ ƒ ‚ n  t ˆ j	 ˆ j
 ƒ \ } } ‰ t d	 | d
 ˆ j ƒ ‡  ‡ ‡ f d †  t | ƒ Dƒ ƒ } t | ƒ ˆ j	 } | S(   s­  Average of the decision functions of the base classifiers.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        score : array, shape = [n_samples, k]
            The decision function of the input samples. The columns correspond
            to the classes in sorted order, as they appear in the attribute
            ``classes_``. Regression and binary classification are special
            cases with ``k == 1``, otherwise ``k==n_classes``.

        RT   R¯   Rw   Rx   R[   Ry   i   sj   Number of features of the model must match the input. Model n_features is {0} and input n_features is {1} Rt   RE   c         3   sS   |  ]I } t  t ƒ ˆ j ˆ | ˆ | d  !ˆ j ˆ | ˆ | d  !ˆ  ƒ Vq d S(   i   N(   R	   Rl   R}   R   (   Rj   RI   (   RA   Ru   R   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>  s   N(   R   R   R3   R:   R‚   R/   R4   R°   R   R?   Rt   R   RE   R5   Rk   (   Ru   RA   Rt   R?   t   all_decisionst	   decisions(    (   RA   Ru   R   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRi   ù  s    			N(   R›   Rœ   R   R3   R   R:   Rq   R…   R”   R„   RU   RP   Ra   R   Ri   (    (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR   ®  s&   z		%			4	:c           B   sS   e  Z d  Z d d d d e e e e d d d d „ Z d „  Z d „  Z d „  Z	 RS(	   sš  A Bagging regressor.

    A Bagging regressor is an ensemble meta-estimator that fits base
    regressors each on random subsets of the original dataset and then
    aggregate their individual predictions (either by voting or by averaging)
    to form a final prediction. Such a meta-estimator can typically be used as
    a way to reduce the variance of a black-box estimator (e.g., a decision
    tree), by introducing randomization into its construction procedure and
    then making an ensemble out of it.

    This algorithm encompasses several works from the literature. When random
    subsets of the dataset are drawn as random subsets of the samples, then
    this algorithm is known as Pasting [1]_. If samples are drawn with
    replacement, then the method is known as Bagging [2]_. When random subsets
    of the dataset are drawn as random subsets of the features, then the method
    is known as Random Subspaces [3]_. Finally, when base estimators are built
    on subsets of both samples and features, then the method is known as
    Random Patches [4]_.

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

    Parameters
    ----------
    base_estimator : object or None, optional (default=None)
        The base estimator to fit on random subsets of the dataset.
        If None, then the base estimator is a decision tree.

    n_estimators : int, optional (default=10)
        The number of base estimators in the ensemble.

    max_samples : int or float, optional (default=1.0)
        The number of samples to draw from X to train each base estimator.

        - If int, then draw `max_samples` samples.
        - If float, then draw `max_samples * X.shape[0]` samples.

    max_features : int or float, optional (default=1.0)
        The number of features to draw from X to train each base estimator.

        - If int, then draw `max_features` features.
        - If float, then draw `max_features * X.shape[1]` features.

    bootstrap : boolean, optional (default=True)
        Whether samples are drawn with replacement. If False, sampling
        without replacement is performed.

    bootstrap_features : boolean, optional (default=False)
        Whether features are drawn with replacement.

    oob_score : bool
        Whether to use out-of-bag samples to estimate
        the generalization error.

    warm_start : bool, optional (default=False)
        When set to True, reuse the solution of the previous call to fit
        and add more estimators to the ensemble, otherwise, just fit
        a whole new ensemble. See :term:`the Glossary <warm_start>`.

    n_jobs : int or None, optional (default=None)
        The number of jobs to run in parallel for both `fit` and `predict`.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

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

    verbose : int, optional (default=0)
        Controls the verbosity when fitting and predicting.

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

    estimators_samples_ : list of arrays
        The subset of drawn samples (i.e., the in-bag samples) for each base
        estimator. Each subset is defined by an array of the indices selected.

    estimators_features_ : list of arrays
        The subset of drawn features for each base estimator.

    oob_score_ : float
        Score of the training dataset obtained using an out-of-bag estimate.

    oob_prediction_ : array of shape = [n_samples]
        Prediction computed with out-of-bag estimate on the training
        set. If n_estimators is small it might be possible that a data point
        was never left out during the bootstrap. In this case,
        `oob_prediction_` might contain NaN.

    References
    ----------

    .. [1] L. Breiman, "Pasting small votes for classification in large
           databases and on-line", Machine Learning, 36(1), 85-103, 1999.

    .. [2] L. Breiman, "Bagging predictors", Machine Learning, 24(2), 123-140,
           1996.

    .. [3] T. Ho, "The random subspace method for constructing decision
           forests", Pattern Analysis and Machine Intelligence, 20(8), 832-844,
           1998.

    .. [4] G. Louppe and P. Geurts, "Ensembles on Random Patches", Machine
           Learning and Knowledge Discovery in Databases, 346-361, 2012.
    i
   g      ð?i    c         C   sV   t  t |  ƒ j | d | d | d | d | d | d | d | d |	 d	 |
 d
 | ƒ
d  S(   NR?   R(   R'   R   R$   Rr   Rs   Rt   R   RE   (   Rp   R   Rq   (   Ru   Ro   R?   R(   R'   R   R$   Rr   Rs   Rt   R   RE   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRq   ›  s    c            s    t  ˆ d ƒ t ˆ  d d d g d d
 d t ƒ‰  t ˆ j ˆ j ƒ \ } } ‰ t d | d ˆ j ƒ ‡  ‡ ‡ f d	 †  t	 | ƒ Dƒ ƒ } t
 | ƒ ˆ j } | S(   s&  Predict regression target for X.

        The predicted regression target of an input sample is computed as the
        mean predicted regression targets of the estimators in the ensemble.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrices are accepted only if
            they are supported by the base estimator.

        Returns
        -------
        y : array of shape = [n_samples]
            The predicted values.
        R   R¯   Rw   Rx   R[   Ry   Rt   RE   c         3   sS   |  ]I } t  t ƒ ˆ j ˆ | ˆ | d  !ˆ j ˆ | ˆ | d  !ˆ  ƒ Vq d S(   i   N(   R	   Rm   R}   R   (   Rj   RI   (   RA   Ru   R   (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pys	   <genexpr>Ñ  s   N(   R   R   R3   R:   R   R?   Rt   R   RE   R5   Rk   (   Ru   RA   Rt   R?   t	   all_y_hatt   y_hat(    (   RA   Ru   R   s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyRU   ´  s    		c         C   s    t  t |  ƒ j d t ƒ  ƒ d S(   s:   Check the estimator and set the base_estimator_ attribute.RŸ   N(   Rp   R   R…   R   (   Ru   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR…   Ü  s    c   
   	   C   s  | j  d } t j | f ƒ } t j | f ƒ } x… t |  j |  j |  j ƒ D]h \ } } } t | | ƒ }	 | |	 c | j | |	 d  d  … f d  d  … | f ƒ 7<| |	 c d 7<qM W| d k j	 ƒ  rè t
 d ƒ d | | d k <n  | | } | |  _ t | | ƒ |  _ d  S(   Ni    i   s{   Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.(   R/   R6   RQ   R   R}   Rš   R   R   RU   R¢   R   t   oob_prediction_R   R|   (
   Ru   RA   RB   R!   RY   t   n_predictionsRJ   R¦   RK   R§   (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR”   á  s    9

	N(
   R›   Rœ   R   R3   R   R:   Rq   RU   R…   R”   (    (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyR   +  s   n	(	(=   R   t
   __future__R    R‘   Rˆ   t   numpyR6   t   abcR   R   t   warningsR   t   baseR   R   R   R   t   utils._joblibR   R	   t   externals.sixR
   t   externals.six.movesR   t   metricsR   R   t   treeR   R   t   utilsR   R   R   R   R   R   t   utils.metaestimatorsR   t   utils.multiclassR   t   utils.randomR   t   utils.validationR   R   t   __all__t   iinfot   int32RŒ   RŽ   R#   R+   RO   RZ   Rh   Rl   Rm   Rn   R   R   (    (    (    s7   lib/python2.7/site-packages/sklearn/ensemble/bagging.pyt   <module>   sB   "				;				óÿ ~