CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

2�Yi�"�@s�dZddlZddlmZddlmZmZddlm	Z	ee3edh�dgejgdgddgd	�d4d�dd�d
d��Zee5e
edh�dgddgddgd�d6d�dd�dd��ZdS)z5Utilities for handling weights based on class labels.�N)�sparse�)�7StrOptions�validate_params)�_check_sample_weight�balancedz8array-like)�class_weight�classes�y�
sample_weightT)Zprefer_skip_nested_validation)rcCsVddlm}t|�t|�rtd��|dust|�dkr*tj|jdtjdd�}|S|dkrc|�}|�	|�}t9t�||j��sCtd	��t
||�}tj||d10�}|��t|j�|}	|	|�|�}|Stj|jdtjdd�}g}11t|�D]\}}||vr�||||<qu|12�|�qut|�t|13�}
|14r�|
t|�kr�t�|15���}td|�d���|S)
a�Estimate class weights for unbalanced datasets.16 17    Parameters18    ----------19    class_weight : dict, "balanced" or None20        If "balanced", class weights will be given by21        `n_samples / (n_classes * np.bincount(y))` or their weighted equivalent if22        `sample_weight` is provided.23        If a dictionary is given, keys are classes and values are corresponding class24        weights.25        If `None` is given, the class weights will be uniform.26 27    classes : ndarray28        Array of the classes occurring in the data, as given by29        `np.unique(y_org)` with `y_org` the original class labels.30 31    y : array-like of shape (n_samples,)32        Array of original class labels per sample.33 34    sample_weight : array-like of shape (n_samples,), default=None35        Array of weights that are assigned to individual samples. Only used when36        `class_weight='balanced'`.37 38    Returns39    -------40    class_weight_vect : ndarray of shape (n_classes,)41        Array with `class_weight_vect[i]` the weight for i-th class.42 43    References44    ----------45    The "balanced" heuristic is inspired by46    Logistic Regression in Rare Events Data, King, Zen, 2001.47 48    Examples49    --------50    >>> import numpy as np51    >>> from sklearn.utils.class_weight import compute_class_weight52    >>> y = [1, 1, 1, 1, 0, 0]53    >>> compute_class_weight(class_weight="balanced", classes=np.unique(y), y=y)54    array([1.5 , 0.75])55    �)�LabelEncoderz8classes should include all valid labels that can be in yNr�C)�dtype�orderrz.classes should have valid labels that are in y)�weightsz
The classes, z, are not in class_weight)Z
preprocessingr
�set�56ValueError�len�npZones�shape�float64Z
fit_transform�all�isinZclasses_rZbincount�sum�	transform�	enumerate�append�array�tolist)rr	r57rr
�weight�leZy_indZweighted_class_countsZ58recip_freqZunweighted_classes�i�cZn_weighted_classesZ$unweighted_classes_user_friendly_str�r$��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\sklearn/utils/class_weight.py�compute_class_weight
s>4 �5960��61�r&z
sparse matrix)rr62�indices)r'c
Cs�t�|�st�|�}|jdkrt�|d�}|jd}|dur*|dkr*td|�d���|dkrS|dus7t|t	�r;td��t|t63�rSt|�|krStdt|��d	|�d64���g}t|�D]z}t�|�rn|dd�|gf�
���}n|dd�|f}t�|�}d}|dks�|dkr�|}	n||}	|dur�||}65t�|66�}tjt|	||67d�t�||�dd
�}t|�t|�}nt|	||d�}|t�||�}|r�d|t�|t68|��<|�|�qYtj|dtjd�}|S)a�Estimate sample weights by class for unbalanced datasets.69 70    Parameters71    ----------72    class_weight : dict, list of dicts, "balanced", or None73        Weights associated with classes in the form `{class_label: weight}`.74        If not given, all classes are supposed to have weight one. For75        multi-output problems, a list of dicts can be provided in the same76        order as the columns of y.77 78        Note that for multioutput (including multilabel) weights should be79        defined for each class of every column in its own dict. For example,80        for four-class multilabel classification weights should be81        `[{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}]` instead of82        `[{1:1}, {2:5}, {3:1}, {4:1}]`.83 84        The `"balanced"` mode uses the values of y to automatically adjust85        weights inversely proportional to class frequencies in the input data:86        `n_samples / (n_classes * np.bincount(y))`.87 88        For multi-output, the weights of each column of y will be multiplied.89 90    y : {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)91        Array of original class labels per sample.92 93    indices : array-like of shape (n_subsample,), default=None94        Array of indices to be used in a subsample. Can be of length less than95        `n_samples` in the case of a subsample, or equal to `n_samples` in the96        case of a bootstrap subsample with repeated indices. If `None`, the97        sample weight will be calculated over the full sample. Only `"balanced"`98        is supported for `class_weight` if this is provided.99 100    Returns101    -------102    sample_weight_vect : ndarray of shape (n_samples,)103        Array with sample weights as applied to the original `y`.104 105    Examples106    --------107    >>> from sklearn.utils.class_weight import compute_sample_weight108    >>> y = [1, 1, 1, 1, 0, 0]109    >>> compute_sample_weight(class_weight="balanced", y=y)110    array([0.75, 0.75, 0.75, 0.75, 1.5 , 1.5 ])111    r)�����rNrzAThe only valid class_weight for subsampling is 'balanced'. Given �.zSFor multi-output, class_weight should be a list of dicts, or the string 'balanced'.zYFor multi-output, number of elements in class_weight should match number of outputs. Got z element(s) while having z	 outputs.)r	r112Zclip)�modegr)Zaxisr)r�issparserZ113atleast_1d�ndimZreshaperr�114isinstance�dict�listr�rangeZtoarray�flatten�uniqueZtaker&Zsearchsortedrrr�prodr)
rr115r'Z	n_outputsZexpanded_class_weight�kZy_fullZclasses_fullZclasses_missingZclass_weight_kZy_subsampleZclasses_subsampleZweight_kr$r$r%�compute_sample_weightjsl1167117118119������120121122�123��r5)�__doc__�numpyrZscipyrZ_param_validationrrZ124validationrr.Zndarrayr&r/r5r$r$r$r%�<module>s,��	T��
Aluode/PerceptionLabPortable · CoolFace