ó
‡ˆ\c           @   sx   d  d l  Z  d d l m Z m Z d d l m Z d d l m Z d d l m	 Z	 d „  Z
 d e e f d	 „  ƒ  YZ d S(
   iÿÿÿÿNi   (   t   BaseEstimatort   TransformerMixin(   t   check_array(   t   assert_allclose_dense_sparse(   t   string_typesc         C   s   |  S(   s   The identity function.
    (    (   t   X(    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt	   _identity	   s    t   FunctionTransformerc        	   B   sw   e  Z d  Z d	 d	 d	 e d e d	 d	 d „ Z d „  Z d „  Z d	 d „ Z	 d d „ Z
 d d „ Z d	 d	 d	 d „ Z RS(
   sê	  Constructs a transformer from an arbitrary callable.

    A FunctionTransformer forwards its X (and optionally y) arguments to a
    user-defined function or function object and returns the result of this
    function. This is useful for stateless transformations such as taking the
    log of frequencies, doing custom scaling, etc.

    Note: If a lambda is used as the function, then the resulting
    transformer will not be pickleable.

    .. versionadded:: 0.17

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

    Parameters
    ----------
    func : callable, optional default=None
        The callable to use for the transformation. This will be passed
        the same arguments as transform, with args and kwargs forwarded.
        If func is None, then func will be the identity function.

    inverse_func : callable, optional default=None
        The callable to use for the inverse transformation. This will be
        passed the same arguments as inverse transform, with args and
        kwargs forwarded. If inverse_func is None, then inverse_func
        will be the identity function.

    validate : bool, optional default=True
        Indicate that the input X array should be checked before calling
        ``func``. The possibilities are:

        - If False, there is no input validation.
        - If True, then X will be converted to a 2-dimensional NumPy array or
          sparse matrix. If the conversion is not possible an exception is
          raised.

        .. deprecated:: 0.20
           ``validate=True`` as default will be replaced by
           ``validate=False`` in 0.22.

    accept_sparse : boolean, optional
        Indicate that func accepts a sparse matrix as input. If validate is
        False, this has no effect. Otherwise, if accept_sparse is false,
        sparse matrix inputs will cause an exception to be raised.

    pass_y : bool, optional default=False
        Indicate that transform should forward the y argument to the
        inner callable.

        .. deprecated::0.19

    check_inverse : bool, default=True
       Whether to check that or ``func`` followed by ``inverse_func`` leads to
       the original inputs. It can be used for a sanity check, raising a
       warning when the condition is not fulfilled.

       .. versionadded:: 0.20

    kw_args : dict, optional
        Dictionary of additional keyword arguments to pass to func.

    inv_kw_args : dict, optional
        Dictionary of additional keyword arguments to pass to inverse_func.

    t
   deprecatedc	   	      C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   funct   inverse_funct   validatet   accept_sparset   pass_yt   check_inverset   kw_argst   inv_kw_args(	   t   selfR	   R
   R   R   R   R   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt   __init__Q   s    							c         C   sW   |  j  d  k r+ t |  _ t j d t ƒ n |  j  |  _ |  j rS t | d |  j ƒS| S(   NsE   The default validate=True will be replaced by validate=False in 0.22.R   (	   R   t   Nonet   Truet	   _validatet   warningst   warnt   FutureWarningR   R   (   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt   _check_input]   s    		
	c         C   sy   t  d d t d | j d d ƒ ƒ } y+ t | | |  j |  j | | ƒ ƒ ƒ Wn! t k
 rt t j	 d t
 ƒ n Xd S(   s1   Check that func and inverse_func are the inverse.i   i    id   s‰   The provided functions are not strictly inverse of each other. If you are sure you want to proceed regardless, set 'check_inverse=False'.N(   t   sliceR   t   maxt   shapeR   t   inverse_transformt	   transformt   AssertionErrorR   R   t   UserWarning(   R   R   t   idx_selected(    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt   _check_inverse_transformj   s    &!	c         C   sK   |  j  | ƒ } |  j rG |  j d k p3 |  j d k rG |  j | ƒ n  |  S(   s  Fit transformer by checking X.

        If ``validate`` is ``True``, ``X`` will be checked.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Input array.

        Returns
        -------
        self
        N(   R   R   R	   R   R
   R"   (   R   R   t   y(    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt   fitw   s
    c         C   sT   t  | t ƒ s | d k r/ t j d t ƒ n  |  j | d | d |  j d |  j ƒS(   sW  Transform X using the forward function.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Input array.

        y : (ignored)
            .. deprecated::0.19

        Returns
        -------
        X_out : array-like, shape (n_samples, n_features)
            Transformed input.
        R   sS   The parameter y on transform() is deprecated since 0.19 and will be removed in 0.21R#   R	   R   (   t
   isinstanceR   R   R   t   DeprecationWarningt
   _transformR	   R   (   R   R   R#   (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyR   ‹   s    	
c         C   sT   t  | t ƒ s | d k r/ t j d t ƒ n  |  j | d | d |  j d |  j ƒS(   sW  Transform X using the inverse function.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Input array.

        y : (ignored)
            .. deprecated::0.19

        Returns
        -------
        X_out : array-like, shape (n_samples, n_features)
            Transformed input.
        R   s[   The parameter y on inverse_transform() is deprecated since 0.19 and will be removed in 0.21R#   R	   R   (   R%   R   R   R   R&   R'   R
   R   (   R   R   R#   (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyR   ¢   s
    	
c         C   s“   |  j  | ƒ } | d  k r$ t } n  t |  j t ƒ sF |  j d k rb |  j } t j d t ƒ n t	 } | | | r} | f n d | rŒ | n i  Ž S(   NR   sI   The parameter pass_y is deprecated since 0.19 and will be removed in 0.21(    (
   R   R   R   R%   R   R   R   R   R&   t   False(   R   R   R#   R	   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyR'   ¹   s    			
N(   t   __name__t
   __module__t   __doc__R   R(   R   R   R   R"   R$   R   R   R'   (    (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyR      s   A		
		(   R   t   baseR    R   t   utilsR   t   utils.testingR   t   externals.sixR   R   R   (    (    (    sJ   lib/python2.7/site-packages/sklearn/preprocessing/_function_transformer.pyt   <module>   s   	