TypeError: compute_class_weight() takes 1 positional argument but 3 were given
调用sklearn的compute_class_weight提示错误”compute_class_weight() takes 1 positional argument but 3 were given“,解决办法为函数里加上参数名:
from sklearn.utils.class_weight import compute_class_weight
label = [0] * 9 + [1] * 1 + [2, 2]
classes = [0, 1, 2]
weight = compute_class_weight(class_weight='balanced', classes=classes, y=label)
print(weight)
=============================================
为函数里加上参数名【可行】
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
=========================
后面发现是传入y的参数的时候,label是2维的,label的维度是(1000,1)要把它变成(1000,)就可以。
labels = np.zeros((200,1))
labels[0:2][0] = 1
classes = [0, 1]
weight = compute_class_weight(class_weight='balanced', classes=classes, y=label.reshape(-1)
print(weight)
————————————————
链接:https://blog.csdn.net/qq_34845880/article/details/122318233