sklearn.utils.class_weight.compute_class_weight(class_weight, *, classes, y)[source]

Estimate class weights for unbalanced datasets.

class_weightdict, ‘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.


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

yarray-like, shape (n_samples,)

Array of original class labels per sample;

class_weight_vectndarray, shape (n_classes,)

Array with class_weight_vect[i] the weight for i-th class


The “balanced” heuristic is inspired by Logistic Regression in Rare Events Data, King, Zen, 2001.