Ë
    ÷Q(hfB  ã                   ó²   — d dl Z d dlm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mZmZmZmZmZmZmZmZ d
„ Z G d„ dee«      Zy)é    N)Úpartialé   )ÚBaseEstimatorÚTransformerMixinÚ_fit_context)Ú_VisualBlock)Ú
StrOptions)Ú_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                 ó   — | S )zThe identity function.© )ÚXs    úi/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sklearn/preprocessing/_function_transformer.pyÚ	_identityr      s   € à€Hó    c            	       ó  ‡ — e Zd ZU dZedgedgdgdgdge edh«      dgedgedgdœZeed<   	 	 dddddddd	œd
„Z	d„ Z
d„ Z ed¬«      dd„«       Zd„ Zd„ Z ed„ «      dd„«       Zdd„Zd„ Zˆ fd„Zddœd„Zd„ Zd„ Zˆ xZS )ÚFunctionTransformeraÔ  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                ót   — || _         || _        || _        || _        || _        || _        || _        || _        y ©Nr    )	Úselfr!   r"   r#   r$   r%   r&   r'   r(   s	            r   Ú__init__zFunctionTransformer.__init__    sA   € ð ˆŒ	Ø(ˆÔØ ˆŒØ*ˆÔØ*ˆÔØ!2ˆÔØˆŒØ&ˆÕr   c                óŒ   — | j                   rt        | || j                  |¬«      S |rt        | ||¬«       t	        | ||¬«       |S )N)r$   Úreset©r/   )r#   r   r$   r   r   )r,   r   r/   s      r   Ú_check_inputz FunctionTransformer._check_inputµ   sC   € Ø�=Š=Ü   q¸×8JÑ8JÐRWÔXÐXÙô ˜d A¨UÕ3Ü   q°Õ6Øˆr   c           	      óŠ  — t        ddt        d|j                  d   dz  «      «      }| j                  | j	                  ||   «      «      }t        |d«      r|j                  g}nt        |d«      r|j                  }t        d„ D «       «      st        d«      ‚t        ||   |«      st        j                  d	t        «       yy)
z1Check that func and inverse_func are the inverse.Né   r   éd   ÚdtypeÚdtypesc              3   ód   K  — | ](  }t        j                  |t         j                  «      –— Œ* y ­wr+   )ÚnpÚ
issubdtypeÚnumber)Ú.0Úds     r   ú	<genexpr>z?FunctionTransformer._check_inverse_transform.<locals>.<genexpr>Ë   s   è ø€ Ò?°1”2—=‘= ¤B§I¡I×.Ñ?ùs   ‚.0zL'check_inverse' is only supported when all the elements in `X` is numerical.z‰The 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Úhasattrr5   r6   ÚallÚ
ValueErrorr   ÚwarningsÚwarnÚUserWarning)r,   r   Úidx_selectedÚX_round_tripr6   s        r   Ú_check_inverse_transformz,FunctionTransformer._check_inverse_transformÀ   sµ   € ä˜T 4¬¨Q°·±¸±
¸cÑ0AÓ)BÓCˆØ×-Ñ-¨d¯n©n¸Q¸|¹_Ó.MÓNˆä�1�gÔØ—g‘g�Y‰FÜ�Q˜Ô!à—X‘XˆFäÑ?¸Ô?Ô?Üðóð ô
 & a¨¡o°|ÔDÜ�M‰Mð.ô
 õð Er   )Úprefer_skip_nested_validationc                 ó–   — | j                  |d¬«      }| j                  r)| j                  �| j                  �| j	                  |«       | S )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.
        Tr0   )r1   r%   r!   r"   rK   )r,   r   Úys      r   ÚfitzFunctionTransformer.fitÜ   sI   € ð( ×Ñ˜a tÐÓ,ˆØ×Ò t§y¡yÐ'8¸D×<MÑ<MÐ<UØ×)Ñ)¨!Ô,Øˆr   c           	      ó’  — | j                  |d¬«      }| j                  || j                  | j                  ¬«      }t	        d| «      d   }t        |d«      rø| j                  �ì| j                  «       }t        |j                  «      t        |«      k7  r»t        |dt        |«      «      }|duxr! t        |«      t        |j                  «      k(  }t        d	„ |j                  D «       «       }|s|r!t        |«      }|j                  |||d¬
«      }n<t        dt        |j                  «      › dt        | j                  «       «      › d�«      ‚| j                  €ld}	|dk(  r1t!        |«      s&t#        j$                  |	j'                  d«      «       |S |dk(  r/t)        |«      s$t#        j$                  |	j'                  d«      «       |S )a}  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.
        Fr0   ©r!   r'   rB   ÚdenseÚcolumnsNÚfeature_names_in_c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr+   )Ú
isinstanceÚstr)r;   Úcols     r   r=   z0FunctionTransformer.transform.<locals>.<genexpr>  s   è ø€ ò .Ø-0”J˜s¤C×(ñ.ùs   ‚)ÚX_outputÚ
X_originalrS   Úinplacez�The 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`.z›When `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)r1   Ú
_transformr!   r'   r   rC   r&   Úget_feature_names_outÚlistrS   Úgetattrr   rD   r
   Úcreate_containerrE   r   rF   rG   Úformatr   )
r,   r   ÚoutÚoutput_configr&   Úfeature_names_inÚsame_feature_names_in_outÚnot_all_str_columnsÚadapterÚwarn_msgs
             r   rB   zFunctionTransformer.transformõ   sÜ  € ð ×Ñ˜a uÐÓ-ˆØ�o‰o˜a d§i¡i¸¿¹ˆoÓFˆÜ*¨;¸Ó=¸gÑFˆä�3˜	Ô" t×'=Ñ'=Ð'Ið !%× :Ñ :Ó <ÐÜ�C—K‘KÓ ¤DÐ):Ó$;Ò;ô $+ØÐ*Ô,>¸qÓ,Aó$Ð ð -=ÀDÐ,Hò -'ÌTØ$óNä˜#Ÿ+™+Ó&ñN'Ð)ô +.ñ .Ø47·K±Kô.ó +ð 'Ð#ñ -Ñ0CÜ9¸#Ó>�GØ!×2Ñ2Ø!$Ø#&Ø 1Ø %ð	 3ó ‘Cô %ð:ä:>¸s¿{¹{Ó:KÐ9Lð M=ä × :Ñ :Ó <Ó=Ð>ð ?Tð	Tó
ð 
ð ×!Ñ!Ð)ð&ð ð
  Ò(´¸sÔ1CÜ—‘˜hŸo™o¨hÓ7Ô8ð ˆ
ð  (Ò*´=ÀÔ3EÜ—‘˜hŸo™o¨hÓ7Ô8àˆ
r   c                 ó˜   — | j                   rt        || j                  ¬«      }| j                  || j                  | j
                  ¬«      S )a…  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_out : array-like, shape (n_samples, n_features)
            Transformed input.
        )r$   rQ   )r#   r   r$   r^   r"   r(   )r,   r   s     r   rA   z%FunctionTransformer.inverse_transform=  s>   € ð �=Š=Ü˜A¨T×-?Ñ-?Ô@ˆAØ�‰˜q t×'8Ñ'8À$×BRÑBRˆÓSÐSr   c                 ó   — | j                   d uS r+   )r&   ©r,   s    r   ú<lambda>zFunctionTransformer.<lambda>O  s   € ˜t×5Ñ5¸TÐA€ r   c                 ó  — t        | d«      s|�t        | |«      }| j                  dk(  r|}nAt        | j                  «      r| j                  | |«      }nt	        d| j                  ›d�«      ‚t        j                  |t        ¬«      S )a/  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.)r5   )rC   r   r&   ÚcallablerE   r8   ÚasarrayÚobject)r,   Úinput_featuresÚ	names_outs      r   r_   z)FunctionTransformer.get_feature_names_outO  sŒ   € ô@ �4Ð)Ô*¨nÐ.HÜ4°T¸>ÓJˆNØ×!Ñ! \Ò1Ø&‰IÜ�d×,Ñ,Ô-Ø×.Ñ.¨t°^ÓD‰IäØ$ T×%;Ñ%;Ð$>ð ?+ð +óð ô �z‰z˜)¬6Ô2Ð2r   c                 ó0   — |€t         } ||fi |r|¤ŽS i ¤ŽS r+   )r   )r,   r   r!   r'   s       r   r^   zFunctionTransformer._transform  s&   € Øˆ<ÜˆDá�AÑ6¡W˜'Ñ6Ð6°"Ñ6Ð6r   c                  ó   — y)z2Return True since FunctionTransfomer is stateless.Tr   rm   s    r   Ú__sklearn_is_fitted__z)FunctionTransformer.__sklearn_is_fitted__…  s   € àr   c                 óª   •— t         ‰| �  «       }| j                   |_        d|_        | j                   xs | j
                  |j                  _        |S )NF)ÚsuperÚ__sklearn_tags__r#   Úno_validationÚrequires_fitr$   Ú
input_tagsÚsparse)r,   ÚtagsÚ	__class__s     €r   r{   z$FunctionTransformer.__sklearn_tags__‰  sJ   ø€ Ü‰wÑ'Ó)ˆØ!%§¡Ð.ˆÔØ!ˆÔØ%)§]¡]Ð!2Ò!H°d×6HÑ6Hˆ�‰ÔØˆr   )rB   c                óJ   — t        | d«      si | _        || j                  d<   | S )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_configrB   )rC   rƒ   )r,   rB   s     r   Ú
set_outputzFunctionTransformer.set_output�  s,   € ô0 �tÐ5Ô6Ø*,ˆDÔ'à3<ˆ×#Ñ# KÑ0Øˆr   c                 ó  — t        | j                  d«      r| j                  j                  S t        | j                  t        «      r | j                  j                  j                  S | j                  j
                  j                  › d�S )z?Get the name display of the `func` used in HTML representation.Ú__name__z(...))rC   r!   r†   rV   r   r�   rm   s    r   Ú_get_function_namez&FunctionTransformer._get_function_name®  s`   € ä�4—9‘9˜jÔ)Ø—9‘9×%Ñ%Ð%Ü�d—i‘i¤Ô)Ø—9‘9—>‘>×*Ñ*Ð*Ø—)‘)×%Ñ%×.Ñ.Ð/¨uÐ5Ð5r   c                 óR   — t        d| | j                  «       t        | «      dd¬«      S )NÚsingler   )ÚnamesÚname_detailsÚname_captionÚdoc_link_label)r   r‡   rW   rm   s    r   Ú_sk_visual_block_z%FunctionTransformer._sk_visual_block_¶  s.   € ÜØØØ×)Ñ)Ó+Ü˜T›Ø.Ø0ô
ð 	
r   )NNr+   )r†   Ú
__module__Ú__qualname__Ú__doc__rq   r	   Údictr)   Ú__annotations__r-   r1   rK   r   rO   rB   rA   r   r_   r^   rx   r{   r„   r‡   rŽ   Ú__classcell__)r�   s   @r   r   r   #   sî   ø… ñoðd ˜4Ð Ø! 4Ð(Ø�KØ#˜Ø#˜Ø&©
°L°>Ó(BÀDÐIØ˜$�<Ø˜d�|ñ	$Ð˜Dó 	ð Øð'ð
 ØØØØØô'ò*	òñ8 °Ô5òó 6ðò0FòPTñ$ ÑAÓBò-3ó Cð-3ó^7òôð '+ô ò<6ö
r   r   )rF   Ú	functoolsr   Únumpyr8   Úbaser   r   r   Úutils._estimator_html_reprr   Úutils._param_validationr	   Úutils._set_outputr
   r   Úutils.metaestimatorsr   Úutils.validationr   r   r   r   r   r   r   r   r   r   r   r   r   r   ú<module>r�      sK   ðó Ý ã ç @Ñ @Ý 5Ý 0÷õ 0÷
÷ 
õ 
òô
[
Ð*¨Mõ [
r   