
    0Ph                          d Z ddlZddlmZ ddlmZmZ  ee edh          dgej	        gdgdd	
          d             Z
 eee edh          dgddgddgdd	
          ddd            ZdS )z5Utilities for handling weights based on class labels.    N)sparse   )
StrOptionsvalidate_paramsbalancedz
array-like)class_weightclassesyT)prefer_skip_nested_validationc                8   ddl m} t          |          t          |          z
  rt          d          | t	          |           dk    r.t          j        |j        d         t
          j        d          }n| dk    r |            }|	                    |          }t          t          j        ||j                            st          d	          t	          |          t	          |j                  t          j        |                              t
          j                  z  z  }||                    |                   }nt          j        |j        d         t
          j        d          }g }t!          |          D ]*\  }	}
|
| v r| |
         ||	<   |                    |
           +t	          |          t	          |          z
  }|rL|t	          |           k    r9t          j        |                                          }t          d
| d          |S )a  Estimate class weights for unbalanced datasets.

    Parameters
    ----------
    class_weight : dict, "balanced" or None
        If "balanced", class weights will be given by
        `n_samples / (n_classes * np.bincount(y))`.
        If a dictionary is given, keys are classes and values are corresponding class
        weights.
        If `None` is given, the class weights will be uniform.

    classes : ndarray
        Array of the classes occurring in the data, as given by
        `np.unique(y_org)` with `y_org` the original class labels.

    y : array-like of shape (n_samples,)
        Array of original class labels per sample.

    Returns
    -------
    class_weight_vect : ndarray of shape (n_classes,)
        Array with `class_weight_vect[i]` the weight for i-th class.

    References
    ----------
    The "balanced" heuristic is inspired by
    Logistic Regression in Rare Events Data, King, Zen, 2001.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.utils.class_weight import compute_class_weight
    >>> y = [1, 1, 1, 1, 0, 0]
    >>> compute_class_weight(class_weight="balanced", classes=np.unique(y), y=y)
    array([1.5 , 0.75])
       )LabelEncoderz8classes should include all valid labels that can be in yNr   C)dtypeorderr   z.classes should have valid labels that are in yzThe classes, z, are not in class_weight)preprocessingr   set
ValueErrorlennponesshapefloat64fit_transformallisinclasses_bincountastype	transform	enumerateappendarraytolist)r   r	   r
   r   weightley_ind
recip_frequnweighted_classesicn_weighted_classes$unweighted_classes_user_friendly_strs                Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/utils/class_weight.pycompute_class_weightr/      s   \ -,,,,,
1vvG USTTTs<00A55q)3GGG		#	#\^^  ##277BK0011 	OMNNNVVs2;//"+e2D2D2K2KBJ2W2WWX
BLL112 q)3GGGg&& 	- 	-DAqL  (Oq		"))!,,,, \\C0B,C,CC 	"4L8I8I"I"I358<N3O3O3V3V3X3X0  D        
 M    zsparse matrix)r   r
   indices)r1   c          	         t          j        |          s4t          j        |          }|j        dk    rt          j        |d          }|j        d         }|| dk    rt          d|  d          |dk    rq| t          | t                    rt          d          t          | t                    r6t          |           |k    r#t          dt          |            d	| d
          g }t          |          D ]c}t          j        |          r2|dd|gf                                                                         }n|dd|f         }t          j        |          }d}| dk    s|dk    r| }	n| |         }	|v||         }
t          j        |
          }t          j        t#          |	||
          t          j        ||          d          }t'          |          t'          |          z
  }nt#          |	||          }|t          j        ||                   }|r%d|t          j        |t          |                    <   |                    |           et          j        |dt          j                  }|S )a  Estimate sample weights by class for unbalanced datasets.

    Parameters
    ----------
    class_weight : dict, list of dicts, "balanced", or None
        Weights associated with classes in the form `{class_label: weight}`.
        If not given, all classes are supposed to have weight one. For
        multi-output problems, a list of dicts can be provided in the same
        order as the columns of y.

        Note that for multioutput (including multilabel) weights should be
        defined for each class of every column in its own dict. For example,
        for four-class multilabel classification weights should be
        `[{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}]` instead of
        `[{1:1}, {2:5}, {3:1}, {4:1}]`.

        The `"balanced"` mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data:
        `n_samples / (n_classes * np.bincount(y))`.

        For multi-output, the weights of each column of y will be multiplied.

    y : {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)
        Array of original class labels per sample.

    indices : array-like of shape (n_subsample,), default=None
        Array of indices to be used in a subsample. Can be of length less than
        `n_samples` in the case of a subsample, or equal to `n_samples` in the
        case of a bootstrap subsample with repeated indices. If `None`, the
        sample weight will be calculated over the full sample. Only `"balanced"`
        is supported for `class_weight` if this is provided.

    Returns
    -------
    sample_weight_vect : ndarray of shape (n_samples,)
        Array with sample weights as applied to the original `y`.

    Examples
    --------
    >>> from sklearn.utils.class_weight import compute_sample_weight
    >>> y = [1, 1, 1, 1, 0, 0]
    >>> compute_sample_weight(class_weight="balanced", y=y)
    array([0.75, 0.75, 0.75, 0.75, 1.5 , 1.5 ])
    r   )r   Nr   zAThe 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	   r
   clip)modeg        r   )axisr   )r   issparser   
atleast_1dndimreshaper   r   
isinstancedictlistr   rangetoarrayflattenuniquetaker/   searchsortedr   r   r"   prodr   )r   r
   r1   	n_outputsexpanded_class_weightky_fullclasses_fullclasses_missingclass_weight_ky_subsampleclasses_subsampleweight_ks                r.   compute_sample_weightrP   _   s   n ?1 'M!6Q;;
1g&&A
I|z99%!% % %
 
 	
 
Q:lD#A#A%   d++ 	L0A0AY0N0N(*-l*;*;( (( ( (   9 )/ )/?1 	qqq1#vY&&((0022FFqqq!tWFy((:%%a)NN)!_N !/K "	+ 6 6w$",=    1<@@  H ",//#6G2H2HHOO+  H BOL&AAB 	C?BHRWVT/%:%:;;<$$X....G$9TTT  r0   )__doc__numpyr   scipyr   _param_validationr   r   r=   ndarrayr/   r>   rP    r0   r.   <module>rW      s-   ; ;
           : : : : : : : : zz:,77>J<^ 
 #'  H H HV tZZ%=%=tDO, $' 
 #'   7; u! u! u! u! u! u! u!r0   