1
$\begingroup$

I am running a keras model using a fit_generator, with three classes, where each class is of the following:

[1,0,0], [0,1,0], [0,0,1]

Let's say the examples of each class are: 1000, 500 and 500.

Would my class_weight parameter simply be:

class_weight=[1,2,2]
$\endgroup$

1 Answer 1

1
$\begingroup$

One common practice is to use the formula:

class_weight_i = n_samples / (n_classes * n_samples_with_class)
         where class_weight_i= class weight for ith class 
               n_samples = total number of samples 
               n_classes= total number of classes (in this case =3)
               n_samples _with_class = the number of samples in the class

You can use this formula to compute the weight for each class.

Alternatively, You can use the sklearn library to compute the class_weight vector:

from sklearn.utils import class_weight
class_weight = class_weight.compute_class_weight('balanced',
                                             np.unique(y_train),
                                             y_train)
$\endgroup$
2
  • 1
    $\begingroup$ So if my class counts were: 11475, 68719, and 9570, functions would be: (11475+68719+9570)/(3*11475)=2.60752, (11475+68719+9570)/(3*68719)=0.435416, (11475+68719+9570)/(3*9570)=3.12658. Leading to a class_weight = [2.60752,0.435416,3.12658]. Ok that is weird how, in this system, none of them have the weight of 1, but if this is the way it is, thanks. $\endgroup$
    – Never Nor
    Dec 11, 2022 at 5:01
  • $\begingroup$ @NeverNor This method penalizes the classes with less samples. Their mistakes are weighted by this factor, so the higher the weight factor, the "more emphasis" to those classes should be made. $\endgroup$ May 22, 2023 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.