
    &`i                     ~    d dl mZmZ d dlZd dlZd dlmZ d dl	m
Z
  e
d           G d de                      ZdS )	    )ListOptionalN)Preprocessor)	PublicAPIalpha)	stabilityc                        e Zd ZdZd d d dZdZ	 ddd	d
ee         deee                  f fdZ	de
j        fdZd Z xZS )
Normalizera
  Scales each sample to have unit norm.

    This preprocessor works by dividing each sample (i.e., row) by the sample's norm.
    The general formula is given by

    .. math::

        s' = \frac{s}{\lVert s \rVert_p}

    where :math:`s` is the sample, :math:`s'` is the transformed sample,
    :math:\lVert s \rVert`, and :math:`p` is the norm type.

    The following norms are supported:

    * `"l1"` (:math:`L^1`): Sum of the absolute values.
    * `"l2"` (:math:`L^2`): Square root of the sum of the squared values.
    * `"max"` (:math:`L^\infty`): Maximum value.

    Examples:
        >>> import pandas as pd
        >>> import ray
        >>> from ray.data.preprocessors import Normalizer
        >>>
        >>> df = pd.DataFrame({"X1": [1, 1], "X2": [1, 0], "X3": [0, 1]})
        >>> ds = ray.data.from_pandas(df)  # doctest: +SKIP
        >>> ds.to_pandas()  # doctest: +SKIP
           X1  X2  X3
        0   1   1   0
        1   1   0   1

        The :math:`L^2`-norm of the first sample is :math:`\sqrt{2}`, and the
        :math:`L^2`-norm of the second sample is :math:`1`.

        >>> preprocessor = Normalizer(columns=["X1", "X2"])
        >>> preprocessor.fit_transform(ds).to_pandas()  # doctest: +SKIP
                 X1        X2  X3
        0  0.707107  0.707107   0
        1  1.000000  0.000000   1

        The :math:`L^1`-norm of the first sample is :math:`2`, and the
        :math:`L^1`-norm of the second sample is :math:`1`.

        >>> preprocessor = Normalizer(columns=["X1", "X2"], norm="l1")
        >>> preprocessor.fit_transform(ds).to_pandas()  # doctest: +SKIP
            X1   X2  X3
        0  0.5  0.5   0
        1  1.0  0.0   1

        The :math:`L^\infty`-norm of the both samples is :math:`1`.

        >>> preprocessor = Normalizer(columns=["X1", "X2"], norm="max")
        >>> preprocessor.fit_transform(ds).to_pandas()  # doctest: +SKIP
            X1   X2  X3
        0  1.0  1.0   0
        1  1.0  0.0   1

        :class:`Normalizer` can also be used in append mode by providing the
        name of the output_columns that should hold the normalized values.

        >>> preprocessor = Normalizer(columns=["X1", "X2"], output_columns=["X1_normalized", "X2_normalized"])
        >>> preprocessor.fit_transform(ds).to_pandas()  # doctest: +SKIP
           X1  X2  X3  X1_normalized  X2_normalized
        0   1   1   0       0.707107       0.707107
        1   1   0   1       1.000000       0.000000

    Args:
        columns: The columns to scale. For each row, these colmumns are scaled to
            unit-norm.
        norm: The norm to use. The supported values are ``"l1"``, ``"l2"``, or
            ``"max"``. Defaults to ``"l2"``.
        output_columns: The names of the transformed columns. If None, the transformed
            columns will be the same as the input columns. If not None, the length of
            ``output_columns`` must match the length of ``columns``, othwerwise an error
            will be raised.

    Raises:
        ValueError: if ``norm`` is not ``"l1"``, ``"l2"``, or ``"max"``.
    c                 R    t          j        |                               d          S N   axis)npabssumcolss    u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/preprocessors/normalizer.py<lambda>zNormalizer.<lambda>\   s    26$<<+++33     c                 x    t          j        t          j        | d                              d                    S )N   r   r   )r   sqrtpowerr   r   s    r   r   zNormalizer.<lambda>]   s-    2728D!#4#4#8#8a#8#@#@AA r   c                 H    t          j        t          |           d          S r   )r   maxr   r   s    r   r   zNormalizer.<lambda>^   s    BF3t991555 r   )l1l2r   Fr   N)output_columnscolumnsr    c                   t                                                       || _        || _        || j        vr,t          d| d| j                                                   t          j        ||          | _	        d S )NzNorm z( is not supported.Supported values are: )
super__init__r!   norm	_norm_fns
ValueErrorkeysr   #_derive_and_validate_output_columnsr    )selfr!   r%   r    	__class__s       r   r$   zNormalizer.__init__c   s     		t~%%A A A)-)<)<)>)>A A  
 +N^
 
r   dfc                     |j         d d | j        f         } | j        | j                 |          }|                    |d          || j        <   |S )Nr   r   )locr!   r&   r%   divr    )r*   r,   r!   column_normss       r   _transform_pandaszNormalizer._transform_pandasx   sR    &DL)0t~di099")++l+"C"C4	r   c                 P    | j         j         d| j        d| j        d| j        dS )Nz	(columns=z, norm=z, output_columns=))r+   __name__r!   r%   r    )r*   s    r   __repr__zNormalizer.__repr__   sK    ~& 7 7 7 7I7 7"17 7 7	
r   )r   )r4   
__module____qualname____doc__r&   _is_fittabler   strr   r$   pd	DataFramer1   r5   __classcell__)r+   s   @r   r
   r
   
   s        M M` 43AA55 I L
 

 /3
 
 
c

 !c+
 
 
 
 
 
*BL    
 
 
 
 
 
 
r   r
   )typingr   r   numpyr   pandasr;   ray.data.preprocessorr   ray.util.annotationsr   r
    r   r   <module>rD      s    ! ! ! ! ! ! ! !         . . . . . . * * * * * * Wy
 y
 y
 y
 y
 y
 y
 y
 y
 y
r   