ó
    Ú£j/=  ã                   óÊ   • S SK r S SKrS SKJrJrJrJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJrJr  S SKJrJrJrJrJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S/r " S S\\5      r g)é    N)ÚBaseEstimatorÚRegressorMixinÚ_fit_contextÚclone)ÚNotFittedError)ÚLinearRegression)ÚFunctionTransformer)Ú_safe_indexingÚcheck_array)ÚMetadataRouterÚMethodMappingÚ_manual_routingÚ_routing_enabledÚprocess_routing)Ú
HasMethods)Ú_VisualBlock)Úget_tags)Úcheck_is_fittedÚTransformedTargetRegressorc                   óÚ   ^ • \ rS rSr% Sr\" SS/5      S/\" S5      S/\S/\S/S/S.r\\	S	'    SSSSS
S.S jjr
S r\" SS9S 5       rS rU 4S jr\S 5       rS rSS jrS rSrU =r$ )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                ó@   • Xl         X l        X0l        X@l        XPl        g ©Nr   )Úselfr   r   r   r    r!   s         Ú¥C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\sklearn/compose/_target.pyÚ__init__Ú#TransformedTargetRegressor.__init__”   s    € ð #ŒØ&ÔØŒ	Ø(ÔØ*Õó    c           	      ó”  • U R                   b%  U R                  c  U R                  b  [        S5      eU R                   b  [	        U R                   5      U l        O¤U R                  b  U R                  b  U R                  c5  U R                  b(  U R                  c  SOSu  p#[        SU SU SU S35      e[        U R                  U R                  S	U R                  S
9U l        U R
                  R                  SS9  U R
                  R                  U5        U R                  (       až  [        SS[        SUR                  S   S-  5      5      n[        X5      nU R
                  R                  U5      n[        R                   " XPR
                  R#                  U5      5      (       d  [$        R&                  " S[(        5        ggg)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_transformerÚ+TransformedTargetRegressor._fit_transformer£   s¶  € ð ×ÑÑ'Ø�I‰IÑ! T×%6Ñ%6Ñ%BäØWóð ð ×ÑÑ)Ü % d×&6Ñ&6Ó 7ˆDÕà—	‘	Ñ%¨$×*;Ñ*;Ñ*CØ—	‘	Ñ! d×&7Ñ&7Ñ&Cð —y‘yÑ(ñ -à1ñ .�ô
 !Ø˜^Ð,Ð,<¸]¸Oð L(Ø(5 ð 7MðMóð ô
 !4Ø—Y‘YØ!×.Ñ.ØØ"×0Ñ0ñ	!ˆDÔð ×Ñ×(Ñ(°9Ð(Ñ=ð
 	×Ñ×Ñ˜aÔ Ø××Ü   t¬S°°A·G±G¸A±JÀ"Ñ4DÓ-EÓFˆLÜ" 1Ó3ˆEØ×'Ñ'×1Ñ1°%Ó8ˆGÜ—;’;˜u×&7Ñ&7×&IÑ&IÈ'Ó&R×SÑSÜ—’ð6ô
  õð Tð	 r)   F)Úprefer_skip_nested_validationc           
      ó  • Uc#  [        SU R                  R                   S35      e[        USSSSSSS9nUR                  U l        UR                  S:X  a  UR                  S	S5      nOUnU R                  U5        U R                  R                  U5      nUR                  S
:X  a2  UR                  S   S:X  a  U R
                  S:X  a  UR                  SS9nU R                  SS9U l        [        5       (       a  [        U S40 UD6nO[!        SSU005      nU R                  R"                  " X40 UR$                  R"                  D6  ['        U R                  S5      (       a  U R                  R(                  U l        U $ )aø  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
    - If `enable_metadata_routing=False` (default): Parameters directly passed
      to the `fit` method of the underlying regressor.

    - If `enable_metadata_routing=True`: Parameters safely routed to the `fit`
      method of the underlying regressor.

    .. versionchanged:: 1.6
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
self : object
    Fitted estimator.
zThis z= estimator requires y to be passed, but the target y is None.r;   FTÚnumeric)Ú
input_nameÚaccept_sparseÚensure_all_finiteÚ	ensure_2dÚdtypeÚallow_ndr-   éÿÿÿÿé   ©Úaxis)Ú	get_cloner   r   Úfeature_names_in_)r/   Ú	__class__Ú__name__r   ÚndimÚ_training_dimÚreshaperA   r0   r   r4   ÚsqueezeÚ_get_regressorÚ
regressor_r   r   r   r   r   ÚhasattrrQ   )r%   ÚXr;   Ú
fit_paramsÚy_2dÚy_transÚrouted_paramss          r&   r   ÚTransformedTargetRegressor.fitÞ   sw  € ð@ ‰9ÜØ˜Ÿ™×/Ñ/Ð0ð 1Eð Eóð ô ØØØØ"ØØØñ
ˆð ŸV™VˆÔð �6‰6�Q‹;Ø—9‘9˜R Ó#‰DàˆDØ×Ñ˜dÔ#ð ×#Ñ#×-Ñ-¨dÓ3ˆð �<‰<˜1Ó §¡¨qÑ!1°QÓ!6¸4×;MÑ;MÐQRÓ;RØ—o‘o¨1�oÐ-ˆGà×-Ñ-¸Ð-Ð=ˆŒÜ×ÑÜ+¨D°%ÑF¸:ÑF‰Mä+¨[¸5À*Ð:MÐ,NÓOˆMà�‰×Ò˜AÑF¨-×*AÑ*A×*EÑ*EÒFä�4—?‘?Ð$7×8Ñ8Ø%)§_¡_×%FÑ%FˆDÔ"àˆr)   c                 ó  • [        U 5        [        5       (       a  [        U S40 UD6nO[        SSU005      nU R                  R
                  " U40 UR                  R
                  D6nUR                  S:X  a,  U R                  R                  UR                  SS5      5      nOU R                  R                  U5      nU R                  S:X  a2  UR                  S:X  a"  UR                  S   S:X  a  UR                  SS9nU$ )a  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
    - If `enable_metadata_routing=False` (default): Parameters directly passed
      to the `predict` method of the underlying regressor.

    - If `enable_metadata_routing=True`: Parameters safely routed to the
      `predict` method of the underlying regressor.

    .. versionchanged:: 1.6
        See :ref:`Metadata Routing User Guide <metadata_routing>`
        for more details.

Returns
-------
y_hat : ndarray of shape (n_samples,)
    Predicted values.
r   r   r-   rL   rM   rN   )r   r   r   r   rY   r   r   rT   r0   r7   rV   rU   r4   rW   )r%   r[   Úpredict_paramsr_   ÚpredÚ
pred_transs         r&   r   Ú"TransformedTargetRegressor.predict.  sç   € ô6 	˜ÔÜ×ÑÜ+¨D°)ÑN¸~ÑN‰Mä+¨[¸9ÀnÐ:UÐ,VÓWˆMà�‰×&Ò& qÑL¨M×,CÑ,C×,KÑ,KÑLˆØ�9‰9˜‹>Ø×*Ñ*×<Ñ<¸T¿\¹\È"ÈaÓ=PÓQ‰Jà×*Ñ*×<Ñ<¸TÓBˆJà×Ñ !Ó#Ø—‘ 1Ó$Ø× Ñ  Ñ# qÓ(à#×+Ñ+°Ð+Ð3ˆJàÐr)   c                 ó  >• U R                  5       n[        TU ]	  5       nSUR                  l        [        U5      R                  R                  UR                  l        [        U5      R                  R                  UR                  l	        U$ )NT)
rX   ÚsuperÚ__sklearn_tags__Úregressor_tagsÚ
poor_scorer   Ú
input_tagsÚsparseÚtarget_tagsÚmulti_output)r%   r   ÚtagsrR   s      €r&   rh   Ú+TransformedTargetRegressor.__sklearn_tags__]  sm   ø€ Ø×'Ñ'Ó)ˆ	Ü‰wÑ'Ó)ˆØ)-ˆ×ÑÔ&Ü!)¨)Ó!4×!?Ñ!?×!FÑ!Fˆ�‰ÔÜ(0°Ó(;×(GÑ(G×(TÑ(Tˆ×ÑÔ%Øˆr)   c                 óÈ   •  [        U 5        U R                  R                  $ ! [         a4  n[        SR                  U R                  R
                  5      5      UeSnAff = f)z+Number of features seen during :term:`fit`.z*{} object has no n_features_in_ attribute.N)r   r   ÚAttributeErrorÚformatrR   rS   rY   Ún_features_in_)r%   Únfes     r&   rt   Ú)TransformedTargetRegressor.n_features_in_e  s`   € ð
	Ü˜DÔ!ð �‰×-Ñ-Ð-øô ó 	Ü Ø<×CÑCØ—N‘N×+Ñ+óóð ð	ûð	ús   ‚# £
A!­/AÁA!c                 ó˜   • [        U S9R                  U R                  5       [        5       R                  SSS9R                  SSS9S9nU$ )a"  Get metadata routing of this object.

Please check :ref:`User Guide <metadata_routing>` on how the routing
mechanism works.

.. versionadded:: 1.6

Returns
-------
routing : MetadataRouter
    A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
    routing information.
)Úownerr   )ÚcallerÚcalleer   )r   Úmethod_mapping)r   ÚaddrX   r   )r%   Úrouters     r&   Úget_metadata_routingÚ/TransformedTargetRegressor.get_metadata_routingu  sR   € ô   dÑ+×/Ñ/Ø×)Ñ)Ó+Ü(›?ß‰S˜ eˆSÐ,ß‰S˜	¨)ˆSÐ4ð	 0ð 
ˆð ˆr)   c                 óÌ   • U R                   c0  [        5       (       a  [        5       R                  SS9$ [        5       $ U(       a  [	        U R                   5      $ U R                   $ )NT)Úsample_weight)r   r   r   Úset_fit_requestr   )r%   rP   s     r&   rX   Ú)TransformedTargetRegressor._get_regressor‹  sO   € Ø�>‰>Ñ!Ü×!Ñ!Ü'Ó)×9Ñ9ÈÐ9ÐMÐMä'Ó)Ð)æ(1Œu�T—^‘^Ó$ÐE°t·~±~ÐEr)   c                 ó¼   • [        U S5      (       a  U R                  OU R                  5       n[        SU/SUR                  R
                   3/[        U5      /SS9$ )NrY   Úserialzregressor: T)ÚnamesÚname_detailsÚdash_wrapped)rZ   rY   rX   r   rR   rS   Ústr)r%   r   s     r&   Ú_sk_visual_block_Ú,TransformedTargetRegressor._sk_visual_block_”  sc   € ä& t¨\×:Ñ:ˆD�OŠOÀ×@SÑ@SÓ@Uð 	ô ØØˆKØ  ×!4Ñ!4×!=Ñ!=Ð >Ð?Ð@Ü˜i›.Ð)Øñ
ð 	
r)   )	rU   r!   rQ   r   r    r   rY   r   r0   r$   )F)rS   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Úcallabler"   ÚdictÚ__annotations__r'   rA   r   r   r   rh   Úpropertyrt   r~   rX   rŠ   Ú__static_attributes__Ú__classcell__)rR   s   @r&   r   r      sÅ   ø‡ ñmñ` ! %¨Ð!3Ó4°dÐ;Ù" ;Ó/°Ð6Ø˜4Ð Ø! 4Ð(Ø#˜ñ$Ð˜Dó ð ð+ð ØØØö+ò9ñv à&+ññJó	ðJòX-õ^ð ñ.ó ð.òô,F÷

ð 

r)   )!r8   Únumpyr5   Úsklearn.baser   r   r   r   Úsklearn.exceptionsr   Úsklearn.linear_modelr   Úsklearn.preprocessingr	   Úsklearn.utilsr
   r   Ú sklearn.utils._metadata_requestsr   r   r   r   r   Úsklearn.utils._param_validationr   Ú"sklearn.utils._repr_html.estimatorr   Úsklearn.utils._tagsr   Úsklearn.utils.validationr   Ú__all__r   © r)   r&   Ú<module>r£      sO   ðó ã ç KÓ KÝ -Ý 1Ý 5ß 5÷õ õ 7Ý ;Ý (Ý 4à'Ð
(€ôB
 °õ B
r)   