笔者在运行下面这段代码时:
class_weights = compute_class_weight(class_weight = 'balanced',
classes = np.unique(train_label_), y = train_label_)
遇到报错:
TypeError: unhashable type: 'numpy.ndarray'
此时:train_label.shape : (sample ,2):此时用了
train_label =np_utils.to_categorical(train_label,num_classes=2)
后将shape改为(sample ,1)问题即解决。
此时又出现新的报错:
ValueError: classes should include all valid labels that can be in y
将代码做出如下更改即可:
ss_weights = compute_class_weight(class_weight = 'balanced',
classes = np.unique(train_label), y = np.ravel(train_label))
这里要注意的是,class_weight参数的接收形式:clases=[0, 1];y=[0,0,0,0,1,1,1,1]。shape:(1,sample)。