Package parsimony :: Package utils :: Module classif_label
[hide private]
[frames] | no frames]

Module classif_label

source code

Created on Mon Mar 10 13:52:23 2014

Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved.


Author: Edouard Duchesnay

License: BSD 3-clause.

Functions [hide private]
 
class_weight_to_sample_weight(class_weight, y)
Estimate class weights for unbalanced datasets.
source code
 
check_labels(y)
ensure binary classification with 0, 1 labels
source code
Variables [hide private]
  __package__ = 'parsimony.utils'
Function Details [hide private]

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