Reputation: 2299
The classifier script I wrote is working fine and recently added weight balancing to the fitting. Since I added the weight estimate function using 'sklearn' library I get the following error :
compute_class_weight() takes 1 positional argument but 3 were given
This error does not make sense per documentation. The script should have three inputs but not sure why it says expecting only one variable. Full error and code information is shown below. Apparently, this is failing only in VS code. I tested in the Jupyter notebook and working fine. So it seems an issue with VS code compiler. Any one notice? ( I am using Python 3.8 with other latest other libraries)
from sklearn.utils import compute_class_weight
train_classes = train_generator.classes
class_weights = compute_class_weight(
"balanced",
np.unique(train_classes),
train_classes
)
class_weights = dict(zip(np.unique(train_classes), class_weights)),
class_weights
In Jupyter Notebook,
Upvotes: 35
Views: 51885
Reputation: 119
I know this is too late, but for me, none of the suggestions worked. However, after a quick look at the documentation, I think this is a good way to get balanced class weights.
unique_classes = np.unique(labels)
class_counts = np.bincount(labels)
total_samples = len(labels)
class_weights = {}
for cls in unique_classes:
class_weight = total_samples / (len(unique_classes) * class_counts[cls])
class_weights[cls] = class_weight
class_weights = [class_weights[i] for i in range(len(class_weights))]
class_weights = torch.FloatTensor(class_weights).cuda() if torch.cuda.is_available() else torch.FloatTensor(class_weights)
criterion = nn.CrossEntropyLoss(weight=class_weights)
Hope this helps!
Upvotes: 0
Reputation: 175
Just follow this: Why doesn't class_weight.compute_weight() work?
You just need to use class_weight, classes, y terms when you assign the related values.
Upvotes: 1
Reputation: 131
I solved this problem with recode configuraiton.
from sklearn.utils.class_weight import compute_class_weight
class_weights = compute_class_weight(class_weight = "balanced", classes= np.unique(train_labels), y= train_labels)
Upvotes: 12
Reputation: 2299
After spending a lot of time, this is how I fixed it. I still don't know why but when the code is modified as follows, it works fine. I got the idea after seeing this solution for a similar but slightly different issue.
class_weights = compute_class_weight(
class_weight = "balanced",
classes = np.unique(train_classes),
y = train_classes
)
class_weights = dict(zip(np.unique(train_classes), class_weights))
class_weights
Upvotes: 92
Reputation: 11
You need to use older version of sklearn than you have. for me it works fine with scikit-learn version 0.24.2.
Upvotes: 0