
     h,                         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
 ddlmZmZ ddlmZ ddlmZ dd	lmZ d
gZ G d d
ee          ZdS )    N   )BaseEstimatorRegressorMixinclone)check_is_fitted)
_safe_tags)check_array_safe_indexing)
HasMethods)FunctionTransformer)NotFittedErrorTransformedTargetRegressorc                       e Zd ZU dZ eddg          dg ed          dgedgedgdgdZeed<   	 ddddd	d
dZ	d Z
d Zd Zd Zed             ZdS )r   a  Meta-estimator to regress on a transformed target.

    Useful for applying a non-linear transformation to the target `y` in
    regression problems. This transformation can be given as a Transformer
    such as the :class:`~sklearn.preprocessing.QuantileTransformer` or as a
    function and its inverse such as `np.log` and `np.exp`.

    The computation during :meth:`fit` is::

        regressor.fit(X, func(y))

    or::

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

    The computation during :meth:`predict` is::

        inverse_func(regressor.predict(X))

    or::

        transformer.inverse_transform(regressor.predict(X))

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

    .. versionadded:: 0.20

    Parameters
    ----------
    regressor : object, default=None
        Regressor object such as derived from
        :class:`~sklearn.base.RegressorMixin`. This regressor will
        automatically be cloned each time prior to fitting. If `regressor is
        None`, :class:`~sklearn.linear_model.LinearRegression` is created and used.

    transformer : object, default=None
        Estimator object such as derived from
        :class:`~sklearn.base.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, default=None
        Function to apply to `y` before passing to :meth:`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, default=None
        Function to apply to the prediction of the regressor. Cannot be set at
        the same time as `transformer`. 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 :meth:`fit` and :meth:`predict`.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying regressor exposes such an attribute when fit.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    sklearn.preprocessing.FunctionTransformer : Construct a transformer from an
        arbitrary callable.

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

    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)
    TransformedTargetRegressor(...)
    >>> tt.score(X, y)
    1.0
    >>> tt.regressor_.coef_
    array([2.])
    fitpredictN	transformboolean	regressortransformerfuncinverse_funccheck_inverse_parameter_constraintsT)r   r   r   r   c                L    || _         || _        || _        || _        || _        d S Nr   )selfr   r   r   r   r   s         S/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/compose/_target.py__init__z#TransformedTargetRegressor.__init__   s0     #&	(*    c           	         | j         | j        | j        t          d          | j         t	          | j                   | _        nD| j        | j        t          d          t          | j        | j        d| j                  | _        | j                            |           | j        rt          ddt          d|j        d         dz                      }t          ||          }| j                            |          }t          j        || j                            |                    st#          j        d	t&                     dS dS dS )
zCheck transformer and fit transformer.

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

        NzE'transformer' and functions 'func'/'inverse_func' cannot both be set.z=When 'func' is provided, 'inverse_func' must also be providedT)r   r   validater      r   
   zThe provided functions or transformer are not strictly inverse of each other. If you are sure you want to proceed regardless, set 'check_inverse=False')r   r   r   
ValueErrorr   transformer_r   r   r   slicemaxshaper
   r   npallcloseinverse_transformwarningswarnUserWarning)r   yidx_selectedy_sely_sel_ts        r   _fit_transformerz+TransformedTargetRegressor._fit_transformer   su    'I!T%6%BW   ) %d&6 7 7Dy$):)B S   !4Y!."0	! ! !D 	a    	 tSAGAJ"4D-E-EFFL"1l33E'11%88G;ud&7&I&I'&R&RSS 2      	 	 r    c           	         |                                   |t          d| j        j         d          t	          |dddddd          }|j        | _        |j        d	k    r|                    d
d	          }n|}|                     |           | j	        
                    |          }|j        dk    r'|j        d	         d	k    r|                    d	          }| j        ddlm}  |            | _        nt#          | j                  | _         | j        j        ||fi | t'          | j        d          r| j        j        | _        | S )aB  Fit the model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix} of 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 of shape (n_samples,)
            Target values.

        **fit_params : dict
            Parameters passed to the `fit` method of the underlying
            regressor.

        Returns
        -------
        self : object
            Fitted estimator.
        NzThis z= estimator requires y to be passed, but the target y is None.r0   FTnumeric)
input_nameaccept_sparseforce_all_finite	ensure_2ddtypeallow_ndr#   r   axisLinearRegressionfeature_names_in_)_validate_paramsr%   	__class____name__r	   ndim_training_dimreshaper4   r&   r   r)   squeezer   linear_modelrA   
regressor_r   r   hasattrrB   )r   Xr0   
fit_paramsy_2dy_transrA   s          r   r   zTransformedTargetRegressor.fit   s   * 	9E/ E E E   !
 
 
 V 6Q;;99R##DDDd### #--d33 <1q!1Q!6!6oo1o--G>!777777..00DOO#DN33DOAw55*5554?$788 	G%)_%FD"r    c                 l   t          |             | j        j        |fi |}|j        dk    r/| j                            |                    dd                    }n| j                            |          }| j        dk    r2|j        dk    r'|j        d         dk    r|	                    d          }|S )aK  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} of shape (n_samples, n_features)
            Samples.

        **predict_params : dict of str -> object
            Parameters passed to the `predict` method of the underlying
            regressor.

        Returns
        -------
        y_hat : ndarray of shape (n_samples,)
            Predicted values.
        r#   r=   r   r>   )
r   rK   r   rF   r&   r,   rH   rG   r)   rI   )r   rM   predict_paramspred
pred_transs        r   r   z"TransformedTargetRegressor.predict  s    ( 	&t&q;;N;;9>>*<<T\\"a=P=PQQJJ*<<TBBJ!##1$$ #q((#+++33Jr    c                 \    | j         }|ddlm}  |            }dt          |d          dS )Nr   r@   Tmultioutput)key)
poor_scorerV   )r   rJ   rA   r   )r   r   rA   s      r   
_more_tagsz%TransformedTargetRegressor._more_tags0  sS    N	777777((**I %i]CCC
 
 	
r    c                     	 t          |            n?# t          $ r2}t          d                    | j        j                            |d}~ww xY w| j        j        S )z+Number of features seen during :term:`fit`.z*{} object has no n_features_in_ attribute.N)r   r   AttributeErrorformatrD   rE   rK   n_features_in_)r   nfes     r   r]   z)TransformedTargetRegressor.n_features_in_<  sv    
	D!!!! 	 	 	 <CCN+   		 --s    
A-A		Ar   )rE   
__module____qualname____doc__r   callabler   dict__annotations__r   r4   r   r   rY   propertyr]    r    r   r   r      s        l l^ !j%!344d;"
;//64 !4(#$ $D    + + + + + +* * *XE E EN! ! !F

 

 

 . . X. . .r    )r-   numpyr*   baser   r   r   utils.validationr   utils._tagsr   utilsr	   r
   utils._param_validationr   preprocessingr   
exceptionsr   __all__r   rf   r    r   <module>rp      s   
      7 7 7 7 7 7 7 7 7 7 . . . . . . $ $ $ $ $ $ / / / / / / / / 0 0 0 0 0 0 / / / / / / ' ' ' ' ' ''
(v. v. v. v. v. v. v. v. v. v.r    