Why doesn't class_weight.compute_weight() work?
Asked Answered
L

1

3

Hello I am using the class_wight.compute_class_weight() function from the sklearn utils module.

I have an ImageDataGenerator().flow_from_directory() variable that is train_gen.

here is the code:

from sklearn.utils import class_weight  import numpy as np

class_weights = class_weight.compute_class_weight(
           'balanced',
            np.unique(train_gen.classes), 
            train_gen.classes)

# train_class_weights = dict(enumerate(class_weights))
# model.fit_generator(..., class_weight=train_class_weights)

and i obtain this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-50-d468c4be76b8> in <module>()
      5            'balanced',
      6             np.unique(train_gen.classes),
----> 7             train_gen.classes)
      8 
      9 # train_class_weights = dict(enumerate(class_weights))

TypeError: compute_class_weight() takes 1 positional argument but 3 were given

Does anybody know what the problem could be? thank you

Lafayette answered 18/11, 2021 at 22:4 Comment(1)
What happens if you change the call to class_weights = class_weight.compute_class_weight( 'balanced', classes=np.unique(train_gen.classes), y=train_gen.classes)?Powerful
A
3

You should specify all the arguments in compute_class_weight function:

class_weights = class_weight.compute_class_weight(class_weight='balanced',
                classes=np.unique(train_gen.classes), 
                y=train_gen.classes)
Adolpho answered 20/12, 2022 at 19:6 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Willi
Users find it difficult to understand code only answers with no explanation. Please consider adding some description explaining how it solves the problem.Rameriz

© 2022 - 2024 — McMap. All rights reserved.