
    -i9                         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J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/r " S S\\5      rg)    N   )BaseEstimatorRegressorMixin_fit_contextclone)NotFittedError)LinearRegression)FunctionTransformer)Bunch_safe_indexingcheck_array)MetadataRouterMethodMapping_routing_enabledprocess_routing)
HasMethods)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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         J/var/www/html/venv/lib/python3.13/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        OU 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)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.)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   
   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!   
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    'II!T%6%6%BW  ) %d&6&6 7D		%$*;*;*C		!d&7&7&C yy( -1 .
 !^,,<]O L((5 7MM 
 !4YY!.."00	!D ((9(=
 	a  tSAGGAJ"4D-EFL"13E''11%8G;;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[!        [!        US9S9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-   r   axis)	get_cloner   )r   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   sr   @ 9//0 1E E  "
 VV 66Q;99R#DDd# ##--d3 <<1q!1Q!64;M;MQR;Roo1o-G---=+D%F:FM!Ej,ABMAF-*A*A*E*EF4??$788%)__%F%FD"r)   c                 
   [        U 5        [        5       (       a  [        U S40 UD6nO[        [        US9S9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   rP   r-   rL   r   rM   )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~NM!E.,IJM&&qLM,C,C,K,KL99>**<<T\\"a=PQJ**<<TBJ!#1$  #q(#+++3J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_c  s`    
	D! ---  	 <CCNN++ 		s   # 
A!/AA!c                     [        U R                  R                  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   rR   rS   addrX   r   )r%   routers     r&   get_metadata_routing/TransformedTargetRegressor.get_metadata_routings  s]      dnn&=&=>BB))+(?SeS,S	)S4	 C 
 r)   c                     U R                   c
  [        5       $ U(       a  [        U R                   5      $ U R                   $ r$   )r   r	   r   )r%   rO   s     r&   rX   )TransformedTargetRegressor._get_regressor  s/    >>!#%%(1uT^^$Et~~Er)   )	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   __static_attributes____classcell__)rR   s   @r&   r   r      s    m` !%!34d;";/64 !4(#$D  + +9v &+J	JX-^ . .,F Fr)   )r8   numpyr5   baser   r   r   r   
exceptionsr   linear_modelr	   preprocessingr
   utilsr   r   r   utils._metadata_requestsr   r   r   r   utils._param_validationr   utils._tagsr   utils.validationr   __all__r    r)   r&   <module>r      sQ      E E ' + / 6 6  1 " .'
(sF sFr)   