class_weight_to_sample_weight(class_weight,
y)
| source code
|
Estimate class weights for unbalanced datasets.
Parameters
----------
class_weight : dict, 'auto' or None
If 'auto', class weights will be given inverse proportional
to the frequency of the class in the data. sample_weight will sum
to n_sample.
If a dictionary is given, keys are classes and values
are corresponding class weights. With two classes in {1, 0},
class_weight = {0:0.5, 1:0.5} is equivalent to class_weight == "auto"
If None is given, the class weights will be uniform sample_weight==1.
y : array-like, shape (n_samples,)
Array of original class labels per sample;
Returns
-------
weight_vect : ndarray, shape (n_samples,)
Array with weight_vect[i] the weight for i-th sample
Example
-------
>>> y = [1, 1, 1, 0, 0, 2]
>>> w = class_weight_to_sample_weight("auto", y)
>>> print w.sum() == len(y)
True
>>> print ["%i:%.2f" % (l, np.sum(w[y==l])) for l in np.unique(y)]
['0:2.00', '1:2.00', '2:2.00']
>>> y = [1, 1, 1, 0, 0, 2]
>>> w2 = class_weight_to_sample_weight({0:1./3, 1:1./3, 2:1./3}, y)
>>> np.all(w2 == w)
True
|