o
    Ni	                     @   s&   d dl Zd dlmZ G dd dZdS )    N)sparsec                   @   s"   e Zd ZdZdd ZdddZdS )	OneHotEncodera  Transforms categorical features to continuous numeric features.

    Examples
    --------
    >>> from one_hot_encoder import OneHotEncoder
    >>> enc = OneHotEncoder()
    >>> sequences = [list('bat'), list('cat'), list('rat')]
    >>> enc.fit(sequences)
    <one_hot_encoder.OneHotEncoder instance at 0x7f346d71c200>
    >>> enc.transform(sequences, sparse=False).astype(int)
    array([[0, 1, 0, 1, 1],
           [1, 0, 0, 1, 1],
           [0, 0, 1, 1, 1]])
    >>> enc.transform(list('cat'), sparse=False).astype(int)
    array([[1, 0, 0, 1, 1]])
    >>> enc.transform(list('bat'), sparse=True)
    <1x5 sparse matrix of type '<type 'numpy.float64'>'
        with 3 stored elements in Compressed Sparse Row format>
    c                    sp   t |}g }d t|jd D ]"}t|dd|f } fddt|D }||  t|7  q|| _| S )zFit OneHotEncoder to X.

        Parameters
        ----------

        X : array-like, shape [n_samples, n_feature]
            Input array of type int.

        Returns
        -------

        self
        r      Nc                    s   i | ]	\}}||  qS  r   ).0ivaloffsetr   U/home/ubuntu/.local/lib/python3.10/site-packages/indictrans/_utils/one_hot_encoder.py
<dictcomp>1   s    z%OneHotEncoder.fit.<locals>.<dictcomp>)	npasarrayrangeshapeset	enumerateappendlenunique_feats)selfXdatar   r   
feat_set_idr   r	   r   fit   s   

zOneHotEncoder.fitTc                 C   s   t |}|rtt|tdd | jD f}nt t|tdd | jD ft}t	|D ]\}}t	|D ]\}}|| j| v rMd||| j| | f< q7q/|rVt
|S |S )a  Transform X using one-hot encoding.

        Parameters
        ----------

        X : array-like, shape [n_samples, n_features]
            Input array of categorical features.

        sparse : bool, default: True
            Return sparse matrix if set True else return an array.

        Returns
        -------
        X_out : sparse matrix if sparse=True else a 2-d array, dtype=int
            Transformed input.
        c                 s       | ]}t |V  qd S Nr   r   r   r   r   r   	<genexpr>L       z*OneHotEncoder.transform.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r    O   r!   g      ?)r   
atleast_2dsp
lil_matrixr   sumr   zerosboolr   
csr_matrix)r   r   r   one_hot_matrixr   vecjr   r   r   r   	transform8   s   
zOneHotEncoder.transformN)T)__name__
__module____qualname____doc__r   r,   r   r   r   r   r   
   s    r   )numpyr   scipyr   r#   r   r   r   r   r   <module>   s   