ó
cÂY]c           @   s	  d  d l  m Z d  d l Z d  d l Z d  d l j Z d d l m	 Z	 m
 Z
 m Z d d l m Z d d l m Z d d l m Z 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 e	 e
 f d „  ƒ  YZ d e	 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionNi   (   t   BaseEstimatort   ClassifierMixint   RegressorMixin(   t   check_random_state(   t   _num_samples(   t   check_array(   t   check_consistent_length(   t   check_is_fitted(   t   random_choice_csc(   t   _weighted_percentile(   t   class_distributiont   DummyClassifierc           B   sS   e  Z d  Z d d d d „ Z d d „ Z d „  Z d „  Z d „  Z d d „ Z	 RS(	   sË  
    DummyClassifier is a classifier that makes predictions using simple rules.

    This classifier is useful as a simple baseline to compare with other
    (real) classifiers. Do not use it for real problems.

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

    Parameters
    ----------
    strategy : str, default="stratified"
        Strategy to use to generate predictions.

        * "stratified": generates predictions by respecting the training
          set's class distribution.
        * "most_frequent": always predicts the most frequent label in the
          training set.
        * "prior": always predicts the class that maximizes the class prior
          (like "most_frequent") and ``predict_proba`` returns the class prior.
        * "uniform": generates predictions uniformly at random.
        * "constant": always predicts a constant label that is provided by
          the user. This is useful for metrics that evaluate a non-majority
          class

          .. versionadded:: 0.17
             Dummy Classifier now supports prior fitting strategy using
             parameter *prior*.

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

    constant : int or str or array of shape = [n_outputs]
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    Attributes
    ----------
    classes_ : array or list of array of shape = [n_classes]
        Class labels for each output.

    n_classes_ : array or list of array of shape = [n_classes]
        Number of label for each output.

    class_prior_ : array or list of array of shape = [n_classes]
        Probability of each class for each output.

    n_outputs_ : int,
        Number of outputs.

    sparse_output_ : bool,
        True if the array returned from predict is to be in sparse CSC format.
        Is automatically set to True if the input y is passed in sparse format.
    t
   stratifiedc         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   strategyt   random_statet   constant(   t   selfR   R   R   (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyt   __init__P   s    		c            s"  d } ˆ j  | k r1 t d ˆ j  | f ƒ ‚ n  ˆ j  d k rn t j | ƒ rn | j ƒ  } t j d t ƒ n  t j | ƒ ˆ _ ˆ j s› t	 j
 | ƒ } n  | j d k ˆ _ | j d	 k rÑ t	 j | d ƒ } n  | j d	 ˆ _ t | | | ƒ ˆ j  d k rkˆ j d k rt d ƒ ‚ qkt	 j t	 j
 ˆ j ƒ d ƒ ‰  ˆ  j d ˆ j k rkt d ˆ j ƒ ‚ qkn  t | | ƒ \ ˆ _ ˆ _ ˆ _ ˆ j  d k rÒt ‡  ‡ f d †  t ˆ j ƒ Dƒ ƒ rÒt d ƒ ‚ n  ˆ j d	 k rˆ j rˆ j d ˆ _ ˆ j d ˆ _ ˆ j d ˆ _ n  ˆ S(   s¸  Fit the random classifier.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        y : array-like, shape = [n_samples] or [n_samples, n_outputs]
            Target values.

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

        Returns
        -------
        self : object
        t   most_frequentR   t   uniformR   t   priors.   Unknown strategy type: %s, expected one of %s.sª   A local copy of the target data has been converted to a numpy array. Predicting on sparse target data with the uniform strategy would not save memory and would be slower.i   i   iÿÿÿÿsM   Constant target value has to be specified when the constant strategy is used.i    s0   Constant target value should have shape (%d, 1).c         3   s&   |  ] } ˆ  | ˆ j  | k Vq d  S(   N(   t   classes_(   t   .0t   k(   R   R   (    s,   lib/python2.7/site-packages/sklearn/dummy.pys	   <genexpr>’   s   s:   The constant target value must be present in training data(   R   R   R   R   R   (   iÿÿÿÿi   N(   iÿÿÿÿi   (   R   t
   ValueErrort   spt   issparset   toarrayt   warningst   warnt   UserWarningt   sparse_output_t   npt
   atleast_1dt   ndimt
   output_2d_t   reshapet   shapet
   n_outputs_R   R   t   NoneR   R   t
   n_classes_t   class_prior_t   anyt   range(   R   t   Xt   yt   sample_weightt   allowed_strategies(    (   R   R   s,   lib/python2.7/site-packages/sklearn/dummy.pyt   fitV   sD     	
	!c         C   sÿ  t  |  d ƒ t | ƒ } t |  j ƒ } |  j } |  j } |  j } |  j } |  j d k r‚ | g } | g } | g } | g } n  |  j	 d k r¾ |  j
 | ƒ } |  j d k r¾ | g } q¾ n  |  j r’d }	 |  j	 d k r
g  | D] }
 t j |
 j ƒ  g ƒ ^ qã } nm |  j	 d k r"| }	 nU |  j	 d k r@t d ƒ ‚ n7 |  j	 d k rwg  | D] } t j | g ƒ ^ qV} n  t | | |	 |  j ƒ } ni|  j	 d k rêt j g  t |  j ƒ D] } | | | | j ƒ  ^ q·| d g ƒ } næ |  j	 d k rBt j g  t |  j ƒ D]$ } | | | | j d	 d ƒ ^ qƒ j } nŽ |  j	 d k r£g  t |  j ƒ D]' } | | | j | | d
 | ƒ^ qa} t j | ƒ j } n- |  j	 d k rÐt j |  j | d f ƒ } n  |  j d k rû|  j rût j | ƒ } n  | S(   sZ  Perform classification on test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        Returns
        -------
        y : array, shape = [n_samples] or [n_samples, n_outputs]
            Predicted target values for X.
        R   i   R   R   R   R   sC   Sparse target prediction is not supported with the uniform strategyR   t   axist   sizeN(   R   R   (   R   R   (   R   R   R   R   R)   R   R*   R   R'   R   t   predict_probaR    R(   R!   t   arrayt   argmaxR   R	   t   tileR,   t   vstackt   Tt   randintR$   t   ravel(   R   R-   t	   n_samplest   rsR)   R   R*   R   t   probat
   class_probt   cpt   cR.   R   t   ret(    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyt   predict    sV    								.	(	@	@:c         C   sC  t  |  d ƒ t | ƒ } t |  j ƒ } |  j } |  j } |  j } |  j } |  j d k rŒ |  j	 rŒ | g } | g } | g } | g } n  g  } x„t
 |  j ƒ D]s}	 |  j d k r| |	 j ƒ  }
 t j | | |	 f d t j ƒ} d | d d … |
 f <n|  j d k r1t j | d f ƒ | |	 } n× |  j d k r_| j d | |	 d	 | ƒ} n© |  j d
 k r¡t j | | |	 f d t j ƒ} | | |	 } ng |  j d k rt j | |	 | |	 k ƒ }
 t j | | |	 f d t j ƒ} d | d d … |
 f <n  | j | ƒ q¢ W|  j d k r?|  j	 r?| d } n  | S(   sò  
        Return probability estimates for the test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        Returns
        -------
        P : array-like or list of array-lke of shape = [n_samples, n_classes]
            Returns the probability of the sample for each class in
            the model, where classes are ordered arithmetically, for each
            output.
        R   i   R   t   dtypeg      ð?NR   R   R3   R   R   i    (   R   R   R   R   R)   R   R*   R   R'   R$   R,   R   R6   R!   t   zerost   float64t   onest   multinomialt   wheret   append(   R   R-   R<   R=   R)   R   R*   R   t   PR   t   indt   out(    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyR4   ë   sB    							" ""c         C   sO   |  j  | ƒ } |  j d k r+ t j | ƒ Sg  | D] } t j | ƒ ^ q2 Sd S(   sú  
        Return log probability estimates for the test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        Returns
        -------
        P : array-like or list of array-like of shape = [n_samples, n_classes]
            Returns the log probability of the sample for each class in
            the model, where classes are ordered arithmetically for each
            output.
        i   N(   R4   R'   R!   t   log(   R   R-   R>   t   p(    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyt   predict_log_proba)  s    c         C   sI   | d k r- t j d t | ƒ d f ƒ } n  t t |  ƒ j | | | ƒ S(   s}  Returns the mean accuracy on the given test data and labels.

        In multi-label classification, this is the subset accuracy
        which is a harsh metric since you require for each sample that
        each label set be correctly predicted.

        Parameters
        ----------
        X : {array-like, None}
            Test samples with shape = (n_samples, n_features) or
            None. Passing None as test samples gives the same result
            as passing real test samples, since DummyClassifier
            operates independently of the sampled observations.

        y : array-like, shape = (n_samples) or (n_samples, n_outputs)
            True labels for X.

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

        Returns
        -------
        score : float
            Mean accuracy of self.predict(X) wrt. y.

        R&   i   N(   R(   R!   RE   t   lent   superR   t   score(   R   R-   R.   R/   (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyRS   ?  s    !N(
   t   __name__t
   __module__t   __doc__R(   R   R1   RC   R4   RP   RS   (    (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyR      s   8J	K	>	t   DummyRegressorc           B   sD   e  Z d  Z d d d d „ Z d d „ Z e d „ Z d d „ Z RS(   s]  
    DummyRegressor is a regressor that makes predictions using
    simple rules.

    This regressor is useful as a simple baseline to compare with other
    (real) regressors. Do not use it for real problems.

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

    Parameters
    ----------
    strategy : str
        Strategy to use to generate predictions.

        * "mean": always predicts the mean of the training set
        * "median": always predicts the median of the training set
        * "quantile": always predicts a specified quantile of the training set,
          provided with the quantile parameter.
        * "constant": always predicts a constant value that is provided by
          the user.

    constant : int or float or array of shape = [n_outputs]
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    quantile : float in [0.0, 1.0]
        The quantile to predict using the "quantile" strategy. A quantile of
        0.5 corresponds to the median, while 0.0 to the minimum and 1.0 to the
        maximum.

    Attributes
    ----------
    constant_ : float or array of shape [n_outputs]
        Mean or median or quantile of the training targets or constant value
        given by the user.

    n_outputs_ : int,
        Number of outputs.
    t   meanc         C   s   | |  _  | |  _ | |  _ d  S(   N(   R   R   t   quantile(   R   R   R   RY   (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyR   ˆ  s    		c         C   sô  d } |  j  | k r1 t d |  j  | f ƒ ‚ n  t | d t ƒ} t | ƒ d k rd t d ƒ ‚ n  | j d	 k |  _ | j d
 k rš t j | d ƒ } n  | j	 d
 |  _
 t | | | ƒ |  j  d k rê t j | d d d | ƒ|  _ nî|  j  d k rg| d k r t j | d d ƒ|  _ qØg  t |  j
 ƒ D]+ } t | d d … | f | d d ƒ^ q0|  _ nq|  j  d k r/|  j d k s˜t j |  j ƒ r®t d |  j ƒ ‚ n  |  j d } | d k rèt j | d d d | ƒ|  _ qØg  t |  j
 ƒ D]+ } t | d d … | f | d | ƒ^ qø|  _ n© |  j  d k rØ|  j d k r\t d ƒ ‚ n  t |  j d d d d g d t d d ƒ|  _ |  j rÉ|  j j	 d | j	 d
 k rÉt d | j	 d
 ƒ ‚ n  |  j |  _ n  t j |  j d ƒ |  _ |  S(   s·  Fit the random regressor.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        y : array-like, shape = [n_samples] or [n_samples, n_outputs]
            Target values.

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

        Returns
        -------
        self : object
        RX   t   medianRY   R   s.   Unknown strategy type: %s, expected one of %s.t	   ensure_2di    s   y must not be empty.i   i   iÿÿÿÿR2   t   weightsNt
   percentileg      I@s>   Quantile must be a scalar in the range [0.0, 1.0], but got %s.g      Y@t   qsM   Constant target value has to be specified when the constant strategy is used.t   accept_sparset   csrt   csct   coot   ensure_min_sampless0   Constant target value should have shape (%d, 1).(   RX   RZ   RY   R   (   iÿÿÿÿi   (   i   iÿÿÿÿ(   R   R   R   t   FalseRQ   R#   R$   R!   R%   R&   R'   R   t   averaget	   constant_R(   RZ   R,   R
   RY   t   isscalarR]   R   t	   TypeError(   R   R-   R.   R/   R0   R   R]   (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyR1     sR    !D"!D&c         C   s²   t  |  d ƒ t | ƒ } t j | |  j f |  j d t j |  j ƒ j ƒ} t j | |  j f ƒ } |  j d k rž |  j	 rž t j
 | ƒ } t j
 | ƒ } n  | r® | | f S| S(   s  
        Perform classification on test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        return_std : boolean, optional
            Whether to return the standard deviation of posterior prediction.
            All zeros in this case.

        Returns
        -------
        y : array, shape = [n_samples]  or [n_samples, n_outputs]
            Predicted target values for X.

        y_std : array, shape = [n_samples]  or [n_samples, n_outputs]
            Standard deviation of predictive distribution of query points.
        Rf   RD   i   (   R   R   R!   t   fullR'   Rf   R5   RD   RE   R$   R;   (   R   R-   t
   return_stdR<   R.   t   y_std(    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyRC   Ú  s    c         C   sI   | d k r- t j d t | ƒ d f ƒ } n  t t |  ƒ j | | | ƒ S(   s„  Returns the coefficient of determination R^2 of the prediction.

        The coefficient R^2 is defined as (1 - u/v), where u is the residual
        sum of squares ((y_true - y_pred) ** 2).sum() and v is the total
        sum of squares ((y_true - y_true.mean()) ** 2).sum().
        The best possible score is 1.0 and it can be negative (because the
        model can be arbitrarily worse). A constant model that always
        predicts the expected value of y, disregarding the input features,
        would get a R^2 score of 0.0.

        Parameters
        ----------
        X : {array-like, None}
            Test samples with shape = (n_samples, n_features) or None.
            For some estimators this may be a
            precomputed kernel matrix instead, shape = (n_samples,
            n_samples_fitted], where n_samples_fitted is the number of
            samples used in the fitting for the estimator.
            Passing None as test samples gives the same result
            as passing real test samples, since DummyRegressor
            operates independently of the sampled observations.

        y : array-like, shape = (n_samples) or (n_samples, n_outputs)
            True values for X.

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

        Returns
        -------
        score : float
            R^2 of self.predict(X) wrt. y.
        R&   i   N(   R(   R!   RE   RQ   RR   RW   RS   (   R   R-   R.   R/   (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyRS   ü  s    "!N(	   RT   RU   RV   R(   R   R1   Rd   RC   RS   (    (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyRW   _  s
   'M"(   t
   __future__R    R   t   numpyR!   t   scipy.sparset   sparseR   t   baseR   R   R   t   utilsR   t   utils.validationR   R   R   R   t   utils.randomR	   t   utils.statsR
   t   utils.multiclassR   R   RW   (    (    (    s,   lib/python2.7/site-packages/sklearn/dummy.pyt   <module>   s   ÿ J