
    -iB                         S SK r S SKJr  S SKrSSKJrJrJr  SSK	J
r
  SSKJr  SSKJrJr  SSKJr  SS	KJrJrJrJrJrJrJrJrJr  S
 r " S S\\5      rg)    N)partial   )BaseEstimatorTransformerMixin_fit_context)
StrOptions)_VisualBlock)_get_adapter_from_container_get_output_config)available_if)	_allclose_dense_sparse_check_feature_names_check_feature_names_in_check_n_features_get_feature_names_is_pandas_df_is_polars_dfcheck_arrayvalidate_datac                     U $ )zThe identity function. )Xs    ^/var/www/html/venv/lib/python3.13/site-packages/sklearn/preprocessing/_function_transformer.py	_identityr      s    H    c            	         ^  \ rS rSr% Sr\S/\S/S/S/S/\\" S15      S/\S/\S/S.r\\	S'     SSSS	SSSS
.S jjr
S rS r\" S	S9SS j5       rS rS r\" S 5      SS j5       rSS jrS rU 4S jrSS.S jrS rS rSrU =r$ )FunctionTransformer#   aw  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, 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, 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, default=False
    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.

    .. versionchanged:: 0.22
       The default of ``validate`` changed from True to False.

accept_sparse : bool, default=False
    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.

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

feature_names_out : callable, 'one-to-one' or None, default=None
    Determines the list of feature names that will be returned by the
    `get_feature_names_out` method. If it is 'one-to-one', then the output
    feature names will be equal to the input feature names. If it is a
    callable, then it must take two positional arguments: this
    `FunctionTransformer` (`self`) and an array-like of input feature names
    (`input_features`). It must return an array-like of output feature
    names. The `get_feature_names_out` method is only defined if
    `feature_names_out` is not None.

    See ``get_feature_names_out`` for more details.

    .. versionadded:: 1.1

kw_args : dict, default=None
    Dictionary of additional keyword arguments to pass to func.

    .. versionadded:: 0.18

inv_kw_args : dict, default=None
    Dictionary of additional keyword arguments to pass to inverse_func.

    .. versionadded:: 0.18

Attributes
----------
n_features_in_ : int
    Number of features seen during :term:`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
--------
MaxAbsScaler : Scale each feature by its maximum absolute value.
StandardScaler : Standardize features by removing the mean and
    scaling to unit variance.
LabelBinarizer : Binarize labels in a one-vs-all fashion.
MultiLabelBinarizer : Transform between iterable of iterables
    and a multilabel format.

Notes
-----
If `func` returns an output with a `columns` attribute, then the columns is enforced
to be consistent with the output of `get_feature_names_out`.

Examples
--------
>>> import numpy as np
>>> from sklearn.preprocessing import FunctionTransformer
>>> transformer = FunctionTransformer(np.log1p)
>>> X = np.array([[0, 1], [2, 3]])
>>> transformer.transform(X)
array([[0.       , 0.6931],
       [1.0986, 1.3862]])
Nboolean
one-to-onefuncinverse_funcvalidateaccept_sparsecheck_inversefeature_names_outkw_argsinv_kw_args_parameter_constraintsFT)r$   r%   r&   r'   r(   r)   c                d    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        g Nr!   )	selfr"   r#   r$   r%   r&   r'   r(   r)   s	            r   __init__FunctionTransformer.__init__   s1     	( **!2&r   c                    U R                   (       a  [        XU R                  US9$ U(       a  [        XUS9  [	        XUS9  U$ )N)r%   resetr1   )r$   r   r%   r   r   )r-   r   r1   s      r   _check_input FunctionTransformer._check_input   s=    == 8J8JRWXX dU3 6r   c           	         [        SS[        SUR                  S   S-  5      5      nU R                  U R	                  X   5      5      n[        US5      (       a  UR                  /nO[        US5      (       a  UR                  n[        S W 5       5      (       d  [        S5      e[        X   U5      (       d  [        R                  " S	[        5        gg)
z1Check that func and inverse_func are the inverse.N   r   d   dtypedtypesc              3      #    U  HK  n[        U[        R                  5      =(       a%    [        R                  " U[        R                  5      v   MM     g 7fr,   )
isinstancenpr8   
issubdtypenumber).0ds     r   	<genexpr>?FunctionTransformer._check_inverse_transform.<locals>.<genexpr>   s5      
MSJq"((#Ca(CCVs   AAzL'check_inverse' is only supported when all the elements in `X` is numerical.zThe provided functions are not strictly inverse of each other. If you are sure you want to proceed regardless, set 'check_inverse=False'.)slicemaxshapeinverse_transform	transformhasattrr8   r9   all
ValueErrorr   warningswarnUserWarning)r-   r   idx_selectedX_round_tripr9   s        r   _check_inverse_transform,FunctionTransformer._check_inverse_transform   s    T4Q
c0A)BC--dnnQ_.MN1gggYFQ!!XXF  
MS
 
 
  
 &ao|DDMM.
  Er   )prefer_skip_nested_validationc                     U R                  USS9nU R                  (       a+  U R                  b  U R                  b  U R	                  U5        U $ )a  Fit transformer by checking X.

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

Parameters
----------
X : {array-like, sparse-matrix} of shape (n_samples, n_features)                 if `validate=True` else any object that `func` can handle
    Input array.

y : Ignored
    Not used, present here for API consistency by convention.

Returns
-------
self : object
    FunctionTransformer class instance.
Tr2   )r3   r&   r"   r#   rP   )r-   r   ys      r   fitFunctionTransformer.fit   sI    ( at,tyy'8D<M<M<U))!,r   c           	         U R                  USS9nU R                  XR                  U R                  S9n[	        SU 5      S   n[        US5      (       Ga
  U R                  b  U R                  5       n[        UR                  5      [        U5      :w  a  [        US[        U5      5      nUSL=(       a!    [        U5      [        UR                  5      :H  n[        S	 UR                   5       5      (       + nU(       d  U(       a  [        U5      nUR                  UUUSS
9nO<[        S[        UR                  5       S[        U R                  5       5       S35      eU R                  cz  Sn	US:X  a7  [!        U5      (       d'  ["        R$                  " U	R'                  S5      5        U$ US:X  a5  [)        U5      (       d%  ["        R$                  " U	R'                  S5      5        U$ )a5  Transform X using the forward function.

Parameters
----------
X : {array-like, sparse-matrix} of shape (n_samples, n_features)                 if `validate=True` else any object that `func` can handle
    Input array.

Returns
-------
X_out : array-like, shape (n_samples, n_features)
    Transformed input.
Fr2   r"   r(   rG   densecolumnsNfeature_names_in_c              3   B   #    U  H  n[        U[        5      v   M     g 7fr,   )r;   str)r?   cols     r   rA   0FunctionTransformer.transform.<locals>.<genexpr>  s      .4?SJsC((Ks   )X_output
X_originalrZ   inplacezThe output generated by `func` have different column names than the ones provided by `get_feature_names_out`. Got output with columns names: z' and `get_feature_names_out` returned: z. The column names can be overridden by setting `set_output(transform='pandas')` or `set_output(transform='polars')` such that the column names are set to the names provided by `get_feature_names_out`.zWhen `set_output` is configured to be '{0}', `func` should return a {0} DataFrame to follow the `set_output` API  or `feature_names_out` should be defined.pandaspolars)r3   
_transformr"   r(   r   rH   r'   get_feature_names_outlistrZ   getattrr   rI   r
   create_containerrJ   r   rK   rL   formatr   )
r-   r   outoutput_configr'   feature_names_insame_feature_names_in_outnot_all_str_columnsadapterwarn_msgs
             r   rG   FunctionTransformer.transform   s    au-ooaiioF*;=gF3	""t'='='I !% : : <CKK D):$;; $+*,>q,A$  -=D,H -'T$N#++&N') +. .47KK. + '# -0C9#>G!22!$#& 1 %	 3 C %::>s{{:K9L M= : : <=> ?T	T
 
 !!)& 
 (s1C1Chooh78 
 (*=3E3Ehooh78
r   c                     U R                   (       a  [        XR                  S9nU R                  XR                  U R
                  S9$ )aB  Transform X using the inverse function.

Parameters
----------
X : {array-like, sparse-matrix} of shape (n_samples, n_features)                 if `validate=True` else any object that `inverse_func` can handle
    Input array.

Returns
-------
X_original : array-like, shape (n_samples, n_features)
    Transformed input.
)r%   rX   )r$   r   r%   re   r#   r)   )r-   r   s     r   rF   %FunctionTransformer.inverse_transform@  s:     ==A-?-?@Aq'8'8$BRBRSSr   c                     U R                   S L$ r,   )r'   r-   s    r   <lambda>FunctionTransformer.<lambda>R  s    t55TAr   c                 $   [        U S5      (       d  Ub  [        X5      nU R                  S:X  a  UnOF[        U R                  5      (       a  U R                  X5      nO[	        SU R                  < S35      e[
        R                  " U[        S9$ )ao  Get output feature names for transformation.

This method is only defined if `feature_names_out` is not None.

Parameters
----------
input_features : array-like of str or None, default=None
    Input feature names.

    - If `input_features` is None, then `feature_names_in_` is
      used as the input feature names. If `feature_names_in_` is not
      defined, then names are generated:
      `[x0, x1, ..., x(n_features_in_ - 1)]`.
    - If `input_features` is array-like, then `input_features` must
      match `feature_names_in_` if `feature_names_in_` is defined.

Returns
-------
feature_names_out : ndarray of str objects
    Transformed feature names.

    - If `feature_names_out` is 'one-to-one', the input feature names
      are returned (see `input_features` above). This requires
      `feature_names_in_` and/or `n_features_in_` to be defined, which
      is done automatically if `validate=True`. Alternatively, you can
      set them in `func`.
    - If `feature_names_out` is a callable, then it is called with two
      arguments, `self` and `input_features`, and its return value is
      returned by this method.
n_features_in_r    zfeature_names_out=z is invalid. It must either be "one-to-one" or a callable with two arguments: the function transformer and an array-like of input feature names. The callable must return an array-like of output feature names.)r8   )rH   r   r'   callablerJ   r<   asarrayobject)r-   input_features	names_outs      r   rf   )FunctionTransformer.get_feature_names_outR  s    @ 4)**n.H4TJN!!\1&Id,,--..tDI$T%;%;$> ?+ +  zz)622r   c                 <    Uc  [         nU" U40 U(       a  UD6$ 0 D6$ r,   )r   )r-   r   r"   r(   s       r   re   FunctionTransformer._transform  s&    <DA6W'66"66r   c                     g)z2Return True since FunctionTransfomer is stateless.Tr   rv   s    r   __sklearn_is_fitted__)FunctionTransformer.__sklearn_is_fitted__  s    r   c                    > [         TU ]  5       nU R                  (       + Ul        SUl        U R                  (       + =(       d    U R
                  UR                  l        U$ )NF)super__sklearn_tags__r$   no_validationrequires_fitr%   
input_tagssparse)r-   tags	__class__s     r   r   $FunctionTransformer.__sklearn_tags__  sJ    w')!%.!%)]]!2!Hd6H6Hr   )rG   c                R    [        U S5      (       d  0 U l        XR                  S'   U $ )a>  Set output container.

See :ref:`sphx_glr_auto_examples_miscellaneous_plot_set_output.py`
for an example on how to use the API.

Parameters
----------
transform : {"default", "pandas", "polars"}, default=None
    Configure output of `transform` and `fit_transform`.

    - `"default"`: Default output format of a transformer
    - `"pandas"`: DataFrame output
    - `"polars"`: Polars output
    - `None`: Transform configuration is unchanged

    .. versionadded:: 1.4
        `"polars"` option was added.

Returns
-------
self : estimator instance
    Estimator instance.
_sklearn_output_configrG   )rH   r   )r-   rG   s     r   
set_outputFunctionTransformer.set_output  s-    0 t566*,D'3<##K0r   c                 (   [        U R                  S5      (       a  U R                  R                  $ [        U R                  [        5      (       a   U R                  R                  R                  $ U R                  R
                  R                   S3$ )z?Get the name display of the `func` used in HTML representation.__name__z(...))rH   r"   r   r;   r   r   rv   s    r   _get_function_name&FunctionTransformer._get_function_name  sf    499j))99%%%dii))99>>***))%%../u55r   c           	      L    [        SU U R                  5       [        U 5      SSS9$ )Nsingler   )namesname_detailsname_captiondoc_link_label)r	   r   r]   rv   s    r   _sk_visual_block_%FunctionTransformer._sk_visual_block_  s.    ))+T.0
 	
r   )	r   r%   r&   r'   r"   r)   r#   r(   r$   )NNr,   )r   
__module____qualname____firstlineno____doc__r{   r   dictr*   __annotations__r.   r3   rP   r   rU   rG   rF   r   rf   re   r   r   r   r   r   __static_attributes____classcell__)r   s   @r   r   r   #   s    od 4 !4(K##&
L>(BDI$<d|	$D 	 '
 '*	> 5 60FPT$ AB-3 C-3^7 '+ <6
 
r   r   )rK   	functoolsr   numpyr<   baser   r   r   utils._param_validationr   utils._repr_html.estimatorr	   utils._set_outputr
   r   utils.metaestimatorsr   utils.validationr   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      sK       @ @ 0 5 0
 
 

^
*M ^
r   