ó
‡ˆ\c           @   s   d  d l  Z  d  d l 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 d g Z d e e f d „  ƒ  YZ d S(	   iÿÿÿÿNi   (   t   BaseEstimatort   RegressorMixint   clone(   t   check_is_fitted(   t   check_arrayt   safe_indexing(   t   FunctionTransformert   TransformedTargetRegressorc           B   sD   e  Z d  Z d d d d e d „ Z d „  Z d d „ Z d „  Z RS(   sµ  Meta-estimator to regress on a transformed target.

    Useful for applying a non-linear transformation in regression
    problems. This transformation can be given as a Transformer such as the
    QuantileTransformer or as a function and its inverse such as ``log`` and
    ``exp``.

    The computation during ``fit`` is::

        regressor.fit(X, func(y))

    or::

        regressor.fit(X, transformer.transform(y))

    The computation during ``predict`` is::

        inverse_func(regressor.predict(X))

    or::

        transformer.inverse_transform(regressor.predict(X))

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

    Parameters
    ----------
    regressor : object, default=LinearRegression()
        Regressor object such as derived from ``RegressorMixin``. This
        regressor will automatically be cloned each time prior to fitting.

    transformer : object, default=None
        Estimator object such as derived from ``TransformerMixin``. Cannot be
        set at the same time as ``func`` and ``inverse_func``. If
        ``transformer`` is ``None`` as well as ``func`` and ``inverse_func``,
        the transformer will be an identity transformer. Note that the
        transformer will be cloned during fitting. Also, the transformer is
        restricting ``y`` to be a numpy array.

    func : function, optional
        Function to apply to ``y`` before passing to ``fit``. Cannot be set at
        the same time as ``transformer``. The function needs to return a
        2-dimensional array. If ``func`` is ``None``, the function used will be
        the identity function.

    inverse_func : function, optional
        Function to apply to the prediction of the regressor. Cannot be set at
        the same time as ``transformer`` as well. The function needs to return
        a 2-dimensional array. The inverse function is used to return
        predictions to the same space of the original training labels.

    check_inverse : bool, default=True
        Whether to check that ``transform`` followed by ``inverse_transform``
        or ``func`` followed by ``inverse_func`` leads to the original targets.

    Attributes
    ----------
    regressor_ : object
        Fitted regressor.

    transformer_ : object
        Transformer used in ``fit`` and ``predict``.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.linear_model import LinearRegression
    >>> from sklearn.compose import TransformedTargetRegressor
    >>> tt = TransformedTargetRegressor(regressor=LinearRegression(),
    ...                                 func=np.log, inverse_func=np.exp)
    >>> X = np.arange(4).reshape(-1, 1)
    >>> y = np.exp(2 * X).ravel()
    >>> tt.fit(X, y) # doctest: +ELLIPSIS
    TransformedTargetRegressor(...)
    >>> tt.score(X, y)
    1.0
    >>> tt.regressor_.coef_
    array([2.])

    Notes
    -----
    Internally, the target ``y`` is always converted into a 2-dimensional array
    to be used by scikit-learn transformers. At the time of prediction, the
    output will be reshaped to a have the same number of dimensions as ``y``.

    See :ref:`examples/compose/plot_transformed_target.py
    <sphx_glr_auto_examples_compose_plot_transformed_target.py>`.

    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t	   regressort   transformert   funct   inverse_funct   check_inverse(   t   selfR   R	   R
   R   R   (    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyt   __init__k   s
    				c      	   C   sR  |  j  d k	 r< |  j d k	 s- |  j d k	 r< t d ƒ ‚ n~ |  j  d k	 r` t |  j  ƒ |  _ nZ |  j d k	 r |  j d k r t d ƒ ‚ n  t d |  j d |  j d t d |  j	 ƒ |  _ |  j j
 | ƒ |  j	 rNt d d t d | j d d	 ƒ ƒ } t | | ƒ } |  j j | ƒ } t j | |  j j | ƒ ƒ sNt j d
 t ƒ qNn  d S(   s¢   Check transformer and fit transformer.

        Create the default transformer, fit it and make additional inverse
        check on a subset (optional).

        sE   'transformer' and functions 'func'/'inverse_func' cannot both be set.s=   When 'func' is provided, 'inverse_func' must also be providedR
   R   t   validateR   i   i    i
   s—   The provided functions or transformer are not strictly inverse of each other. If you are sure you want to proceed regardless, set 'check_inverse=False'N(   R	   t   NoneR
   R   t
   ValueErrorR   t   transformer_R   t   TrueR   t   fitt   slicet   maxt   shapeR   t	   transformt   npt   allcloset   inverse_transformt   warningst   warnt   UserWarning(   R   t   yt   idx_selectedt   y_selt   y_sel_t(    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyt   _fit_transformers   s&    	&		c      
   C   s/  t  | d t d t d t d d ƒ} | j |  _ | j d k rT | j d d ƒ } n | } |  j | ƒ |  j j | ƒ } | j d k r° | j	 d d k r° | j
 d	 d ƒ } n  |  j d k rÞ d d
 l m } | ƒ  |  _ n t |  j ƒ |  _ | d k r|  j j | | ƒ n |  j j | | d | ƒ|  S(   so  Fit the model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training vector, where n_samples is the number of samples and
            n_features is the number of features.

        y : array-like, shape (n_samples,)
            Target values.

        sample_weight : array-like, shape (n_samples,) optional
            Array of weights that are assigned to individual samples.
            If not provided, then each sample is given unit weight.

        Returns
        -------
        self : object
        t   accept_sparset   force_all_finitet	   ensure_2dt   dtypet   numerici   iÿÿÿÿi   t   axis(   t   LinearRegressiont   sample_weightN(   R   t   FalseR   t   ndimt   _training_dimt   reshapeR#   R   R   R   t   squeezeR   R   t   linear_modelR*   t
   regressor_R   R   (   R   t   XR   R+   t   y_2dt   y_transR*   (    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyR   —   s$    "c         C   s«   t  |  d ƒ |  j j | ƒ } | j d k rO |  j j | j d d ƒ ƒ } n |  j j | ƒ } |  j d k r§ | j d k r§ | j d d k r§ | j	 d d ƒ } n  | S(   s½  Predict using the base regressor, applying inverse.

        The regressor is used to predict and the ``inverse_func`` or
        ``inverse_transform`` is applied before returning the prediction.

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

        Returns
        -------
        y_hat : array, shape = (n_samples,)
            Predicted values.

        R2   i   iÿÿÿÿi   R)   (
   R   R2   t   predictR-   R   R   R/   R.   R   R0   (   R   R3   t   predt
   pred_trans(    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyR6   Ï   s    	"N(	   t   __name__t
   __module__t   __doc__R   R   R   R#   R   R6   (    (    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyR      s   Y	$8(   R   t   numpyR   t   baseR    R   R   t   utils.validationR   t   utilsR   R   t   preprocessingR   t   __all__R   (    (    (    s6   lib/python2.7/site-packages/sklearn/compose/_target.pyt   <module>   s   	