CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
_encode.cpython-310.pyc158 linesDownload Raw Back to __pycache__
1o

2�Yi�/�@s�ddlmZddlmZddlmZddlZddlm	Z	m3Z4mZmZm
Z
ddlmZddd	�d5d�Zd"dd
�ZGdd�de�Zdd�ZGdd�de�Zdd�Zdd�Zdd�dd�Zd#dd�ZGdd�de�Zd d!�ZdS)$�)�Counter)�suppress)�6NamedTupleN�)�_isin�
_searchsorted�device�
get_namespace�xpx��
is_scalar_nanF��return_inverse�
return_countscCs&|jtkrt|||d�St|||d�S)a�Helper function to find unique values with support for python objects.7 8    Uses pure python method for object dtype, and numpy method for9    all other dtypes.10 11    Parameters12    ----------13    values : ndarray14        Values to check for unknowns.15 16    return_inverse : bool, default=False17        If True, also return the indices of the unique values.18 19    return_counts : bool, default=False20        If True, also return the number of times each unique item appears in21        values.22 23    Returns24    -------25    unique : ndarray26        The sorted unique values.27 28    unique_inverse : ndarray29        The indices to reconstruct the original array from the unique array.30        Only provided if `return_inverse` is True.31 32    unique_counts : ndarray33        The number of times each of the unique values comes up in the original34        array. Only provided if `return_counts` is True.35    r
)�dtype�object�_unique_python�36_unique_np)�valuesrr�r�E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\sklearn/utils/_encode.py�_uniques37��rc38Cst|�\}}d\}}|r|r|�|�\}}}}n|r"|�|�\}}n|r,|�|�\}}n|�|�}|jrgt|d�rgt||j|d�}|d|d�}|rR||||k<|rg|�	||d��||<|d|d�}|f}	|rq|	|f7}	|rx|	|f7}	t39|	�dkr�|	dS|	S)z�Helper function to find unique values for numpy arrays that correctly40    accounts for nans. See `_unique` documentation for details.)NN�������xpNrr)r	Z41unique_allZunique_inverseZ
unique_counts�
unique_values�sizerr�nan�sum�len)42rrrr�_Zinverse�counts�uniquesZnan_idx�retrrrr=s.434445rc@s*eZdZUdZeed<eed<dd�ZdS)�
MissingValuesz'Data class for missing data informationr�nonecCs*g}|jr46|�d�|jr|�tj�|S)z3Convert tuple to a list where None is always first.N)r%�appendr�np)�self�outputrrr�to_listjs47zMissingValues.to_listN)�__name__�48__module__�__qualname__�__doc__�bool�__annotations__r*rrrrr$ds4950r$cCsndd�|D�}|s|tddd�fSd|vr)t|�dkr"tddd�}n
tddd�}ntddd�}||}||fS)a.Extract missing values from `values`.51 52    Parameters53    ----------54    values: set55        Set of values to extract missing from.56 57    Returns58    -------59    output: set60        Set with missing values extracted.61 62    missing_values: MissingValues63        Object with missing value information.64    cSs h|]}|dust|�r|�qS�Nr��.0�valuerrr�	<setcomp>�sz#_extract_missing.<locals>.<setcomp>F)rr%NrT)r$r)rZmissing_values_setZoutput_missing_valuesr)rrr�_extract_missingts�r6cs(eZdZdZ�fdd�Zdd�Z�ZS)�_nandictz!Dictionary with support for nans.cs6t��|�|��D]\}}t|�r||_dSq65dSr1)�super�__init__�itemsr�	nan_value)r(�mapping�keyr4��	__class__rrr9�s��z_nandict.__init__cC� t|d�rt|�r|jSt|��)Nr;)�hasattrrr;�KeyError�r(r=rrr�__missing__��z_nandict.__missing__)r+r,r-r.r9rD�
__classcell__rrr>rr7�sr7csDt||�\}}tdd�t|�D���|j�fdd�|D�t|�d�S)z,Map values based on its position in uniques.cSsi|]\}}||�qSrr)r3�i�valrrr�66<dictcomp>�sz#_map_to_integer.<locals>.<dictcomp>csg|]}�|�qSrr�r3�v��tablerr�67<listcomp>��z#_map_to_integer.<locals>.<listcomp>)r)r	r7�	enumerateZasarrayr)rr"rr rrLr�_map_to_integer�s rQcCs�zt|�}t|�\}}t|�}|�|���tj||jd�}Wnty=tdd�tdd�|D��D��}td|����w|f}|rK|t	||�f7}|rU|t68||�f7}t|�dkr_|dS|S)N�rcss�|]}|jVqdSr1)r-)r3�trrr�	<genexpr>�s�z!_unique_python.<locals>.<genexpr>css�|]}t|�VqdSr1)�typerJrrrrT�s�zPEncoders require their input argument must be uniformly strings or numbers. Got rr)�setr6�sorted�extendr*r'�arrayr�	TypeErrorrQ�_get_countsr)rrr�uniques_setZmissing_valuesr"�typesr#rrrr�s( ���rT)�
check_unknownc69Cs~t||�\}}|�|jd�s(zt||�WSty'}ztd|����d}~ww|r8t||�}|r8td|����t|||d�S)a�Helper function to encode values into [0, n_uniques - 1].70 71    Uses pure python method for object dtype, and numpy method for72    all other dtypes.73    The numpy method has the limitation that the `uniques` need to74    be sorted. Importantly, this is not checked but assumed to already be75    the case. The calling method needs to ensure this for all non-object76    values.77 78    Parameters79    ----------80    values : ndarray81        Values to encode.82    uniques : ndarray83        The unique values in `values`. If the dtype is not object, then84        `uniques` needs to be sorted.85    check_unknown : bool, default=True86        If True, check for values in `values` that are not in `unique`87        and raise an error. This is ignored for object dtype, and treated as88        True in this case. This parameter is useful for89        _BaseEncoder._transform() to avoid calling _check_unknown()90        twice.91 92    Returns93    -------94    encoded : ndarray95        Encoded values96    �numericz%y contains previously unseen labels: Nr)r	�isdtyperrQrB�97ValueError�_check_unknownr)rr"r^rr �e�diffrrr�_encode�s��98recs�t||�\}}d}|�|jd�spt|�}t|�\}}t|��t��\��|�}|jo.�j}	|jo5�j}99��fdd��|r\|sE|	sE|100rR|��fdd�|D��}n101|jt	|�|j102d�}t|�}|103rg|�d�|	ro|�t
j�nM|�|�}tj||d|d	�}|r�|jr�t|||�}n104|jt	|�|j105d�}|�|�|��r�|�|�}|�|�r�|jr�|r�|�|�}
d106||
<||}t|�}|r�||fS|S)a�107    Helper function to check for unknowns in values to be encoded.108 109    Uses pure python method for object dtype, and numpy method for110    all other dtypes.111 112    Parameters113    ----------114    values : array115        Values to check for unknowns.116    known_values : array117        Known values. Must be unique.118    return_mask : bool, default=False119        If True, return a mask of the same shape as `values` indicating120        the valid values.121 122    Returns123    -------124    diff : list125        The unique values present in `values` and not in `know_values`.126    valid_mask : boolean array127        Additionally returned if ``return_mask=True``.128 129    Nr_cs$|�vp�jo130|dup�jot|�Sr1)r%rr)r4)�missing_in_uniquesr\rr�is_valids131��z _check_unknown.<locals>.is_validcsg|]}�|��qSrrr2)rgrrrN&rOz"_check_unknown.<locals>.<listcomp>rRT)�
assume_uniquerr)r	r`rrVr6rr%rYZonesrr/�listr&r'rr132Z	setdiff1drr�any�isnan)rZknown_valuesZreturn_maskrr Z133valid_maskZ134values_setZmissing_in_valuesrdZnan_in_diffZnone_in_diffrZdiff_is_nan�is_nanr)rgrfr\rrb�sL135�136137138139140141rbcs0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�_NaNCounterz$Counter with support for nan values.cst��|�|��dSr1)r8r9�_generate_items)r(r:r>rrr9Msz_NaNCounter.__init__ccs>�|D]}t|�s
|Vqt|d�sd|_|jd7_qdS)z>Generate items without nans. Stores the nan counts separately.�	nan_countrrN)rrAro)r(r:�itemrrrrnPs�142�z_NaNCounter._generate_itemscCr@)Nro)rArrorBrCrrrrDZrEz_NaNCounter.__missing__)r+r,r-r.r9rnrDrFrrr>rrmJs143144rmc145	Cs�|jjdvr9t|�}tjt|�tjd�}t|�D]\}}tt	��||||<Wd�n1s1wYq|St146|dd�\}}tj||dd�}t�|d�r[t�|d�r[d|d<t�
|||�}	tj|tjd�}||	||<|S)z�Get the count of each of the `uniques` in `values`.147 148    The counts will use the order passed in by `uniques`. For non-object dtypes,149    `uniques` is assumed to be sorted and `np.nan` is at the end.150    ZOUrRNT)r)rhr)r�kindrmr'ZzerosrZint64rPrrBr�isinrkZsearchsortedZ151zeros_like)152rr"Zcounterr)rGrprr!Zuniques_in_valuesZunique_valid_indicesrrrr[`s"153��r[)FF)F)�collectionsr�154contextlibr�typingr�numpyr'Z155_array_apirrrr	r156Z_missingrrrr$r6�dictr7rQrrerbrmr[rrrr�<module>s"157)'&158+T
Aluode/PerceptionLabPortable · CoolFace