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

James__pxlwk
  • 71
  • 1
  • 7
  • 3
    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)`? – rickhg12hs Nov 19 '21 at 02:37

1 Answers1

2

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)
  • 1
    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](/help/how-to-answer). – Community Dec 21 '22 at 18:21
  • 1
    Users find it difficult to understand code only answers with no explanation. Please consider adding some description explaining how it solves the problem. – Azhar Khan Dec 25 '22 at 14:29