§
    ¨éUgü1  ã                   ó°   — d dl Z d dlZddlmZ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mZ dd
lmZ dgZ G d„ deee¦  «        ZdS )é    Né   )ÚBaseEstimatorÚRegressorMixinÚ_fit_contextÚclone)ÚNotFittedError)ÚFunctionTransformer)Ú_safe_indexingÚcheck_array)Ú
HasMethods)Ú
_safe_tags)Ú_raise_for_unsupported_routingÚ_RoutingNotSupportedMixin)Úcheck_is_fittedÚ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
 ed¬¦  «        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`. If `func is None`, the function used will be
        the identity function. If `func` is set, `inverse_func` also needs to be
        provided. The function needs to return a 2-dimensional array.

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

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

    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.])

    For a more detailed example use case refer to
    :ref:`sphx_glr_auto_examples_compose_plot_transformed_target.py`.
    Ú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         úV/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/compose/_target.pyÚ__init__z#TransformedTargetRegressor.__init__’   s0   € ð #ˆŒØ&ˆÔØˆŒ	Ø(ˆÔØ*ˆÔÐÐó    c           	      ó   — | j         �| j        €| j        �t          d¦  «        ‚| j         �t	          | j         ¦  «        | _        n…| j        �| j        �| j        €.| j        �'| j        €dnd\  }}t          d|› d|› d|› d�¦  «        ‚t          | j        | j        d	| j        ¬
¦  «        | _        | j                             d¬¦  «         | j         	                    |¦  «         | j        r t          ddt          d|j        d         dz  ¦  «        ¦  «        }t          ||¦  «        }| j                             |¦  «        }t          j        || j                             |¦  «        ¦  «        st%          j        dt(          ¦  «         dS dS dS )z¢Check 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.)r   r   )r   r   zWhen 'z' is provided, 'z' must also be provided. If zU is supposed to be the default, you need to explicitly pass it the identity function.T)r   r   Úvalidater   Údefault)r   é   r   é
   z—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')r   r   r   Ú
ValueErrorr   Útransformer_r	   r   Ú
set_outputr   ÚsliceÚmaxÚshaper
   r   ÚnpÚallcloseÚinverse_transformÚwarningsÚwarnÚUserWarning)r    ÚyÚlacking_paramÚexisting_paramÚidx_selectedÚy_selÚy_sel_ts          r!   Ú_fit_transformerz+TransformedTargetRegressor._fit_transformer¡   sõ  € ð ÔÐ'ØŒIÐ! TÔ%6Ð%BåØWñô ð ð ÔÐ)Ý % dÔ&6Ñ 7Ô 7ˆDÔÐà”	Ð%¨$Ô*;Ð*CØ”	Ð! dÔ&7Ð&Cð ”yÐ(ð -Ð,à1ñ .�˜~õ
 !ðM˜^ð Mð M¸]ð Mð MØ(5ðMð Mð Mñô ð õ
 !4Ø”YØ!Ô.ØØ"Ô0ð	!ñ !ô !ˆDÔð Ô×(Ò(°9Ð(Ñ=Ô=Ð=ð
 	Ô×Ò˜aÑ Ô Ð ØÔð 	Ý   t­S°°A´G¸A´JÀ"Ñ4DÑ-EÔ-EÑFÔFˆLÝ" 1 lÑ3Ô3ˆEØÔ'×1Ò1°%Ñ8Ô8ˆGÝ”;˜u dÔ&7×&IÒ&IÈ'Ñ&RÔ&RÑSÔSð 	Ý”ð6õ
  ñô ð ð ð ð	ð 	ð	ð 	r#   F)Úprefer_skip_nested_validationc           	      óŽ  — t          | dfi |¤Ž |€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.
        r   NzThis z= estimator requires y to be passed, but the target y is None.r5   FTÚnumeric)Ú
input_nameÚaccept_sparseÚforce_all_finiteÚ	ensure_2dÚdtypeÚallow_ndr'   éÿÿÿÿr   ©Úaxis©ÚLinearRegressionÚfeature_names_in_)r   r)   Ú	__class__Ú__name__r   ÚndimÚ_training_dimÚreshaper;   r*   r   r.   Úsqueezer   Úlinear_modelrI   Ú
regressor_r   r   ÚhasattrrJ   )r    ÚXr5   Ú
fit_paramsÚy_2dÚy_transrI   s          r!   r   zTransformedTargetRegressor.fitÜ   s“  € õ2 	' t¨UÐAÐA°jÐAÐAÐAØˆ9ÝðE˜œÔ/ð Eð Eð Eñô ð õ ØØØØ!ØØØð
ñ 
ô 
ˆð œVˆÔð Œ6�QŠ;ˆ;Ø—9’9˜R Ñ#Ô#ˆDˆDàˆDØ×Ò˜dÑ#Ô#Ð#ð Ô#×-Ò-¨dÑ3Ô3ˆð Œ<˜1ÒÐ ¤¨qÔ!1°QÒ!6Ð!6Ø—o’o¨1�oÑ-Ô-ˆGàŒ>Ð!Ø7Ð7Ð7Ð7Ð7Ð7à.Ð.Ñ0Ô0ˆDŒOˆOå# D¤NÑ3Ô3ˆDŒOàˆŒÔ˜A˜wÐ5Ð5¨*Ð5Ð5Ð5å�4”?Ð$7Ñ8Ô8ð 	GØ%)¤_Ô%FˆDÔ"àˆ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'   rE   r   rF   )
r   rR   r   rM   r*   r1   rO   rN   r.   rP   )r    rT   Úpredict_paramsÚpredÚ
pred_transs        r!   r   z"TransformedTargetRegressor.predict'  s¿   € õ( 	˜ÑÔÐØ&ˆtŒÔ& qÐ;Ð;¨NÐ;Ð;ˆØŒ9˜Š>ˆ>ØÔ*×<Ò<¸T¿\º\È"ÈaÑ=PÔ=PÑQÔQˆJˆJàÔ*×<Ò<¸TÑBÔBˆJàÔ !Ò#Ð#Ø” 1Ò$Ð$ØÔ  Ô# qÒ(Ð(à#×+Ò+°Ð+Ñ3Ô3ˆJàÐr#   c                 ó\   — | j         }|€ddlm}  |¦   «         }dt          |d¬¦  «        dœS )Nr   rH   TÚmultioutput)Úkey)Ú
poor_scorer]   )r   rQ   rI   r   )r    r   rI   s      r!   Ú
_more_tagsz%TransformedTargetRegressor._more_tagsJ  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ÚformatrK   rL   rR   Ún_features_in_)r    Únfes     r!   rd   z)TransformedTargetRegressor.n_features_in_V  sv   € ð
	Ý˜DÑ!Ô!Ð!Ð!øÝð 	ð 	ð 	Ý Ø<×CÒCØ”NÔ+ñô ñô ð ð	øøøøð	øøøð ŒÔ-Ð-s   ‚ ’
Aœ-A	Á	Ar   )rL   Ú
__module__Ú__qualname__Ú__doc__r   Úcallabler   ÚdictÚ__annotations__r"   r;   r   r   r   r`   Úpropertyrd   © r#   r!   r   r      s2  € € € € € € ðmð mð` !�j %¨Ð!3Ñ4Ô4°dÐ;Ø"˜
 ;Ñ/Ô/°Ð6Ø˜4Ð Ø! 4Ð(Ø#˜ð$ð $Ð˜Dð ð ñ ð ð+ð ØØØð+ð +ð +ð +ð +ð9ð 9ð 9ðv €\à&+ðñ ô ðEð Eñ	ô ðEðN!ð !ð !ðF

ð 

ð 

ð ð.ð .ñ „Xð.ð .ð .r#   )r2   Únumpyr/   Úbaser   r   r   r   Ú
exceptionsr   Úpreprocessingr	   Úutilsr
   r   Úutils._param_validationr   Úutils._tagsr   Úutils.metadata_routingr   r   Úutils.validationr   Ú__all__r   rm   r#   r!   ú<module>rx      s'  ðð
 €€€à Ð Ð Ð à EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EØ 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø /Ð /Ð /Ð /Ð /Ð /Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø $Ð $Ð $Ð $Ð $Ð $ðð ð ð ð ð ð ð ð /Ð .Ð .Ð .Ð .Ð .à'Ð
(€ðL.ð L.ð L.ð L.ð L.Ø˜~¨}ñL.ô L.ð L.ð L.ð L.r#   