【问题标题】:ValueError: `class_weight` must contain all classes in the data. The classes {1, 2, 3} exist in the data but not in `class_weight`ValueError:`class_weight` 必须包含数据中的所有类。类 {1, 2, 3} 存在于数据中,但不存在于 `class_weight`
【发布时间】:2018-08-08 10:28:04
【问题描述】:

ValueError: class_weight 必须包含数据中的所有类。类 {1, 2, 3} 存在于数据中,但不存在于 class_weight

我正在尝试将类权重分配给我的不平衡类,但在 model.fit() 之后它会生成此错误,尽管我已经看到其他解决方案已针对此问题提供但仍无法解决。

test_split=round(n*2/3)
x_train=x[:test_split]
y_train=y[:test_split]
x_test=x[test_split:]
y_test=y[test_split:]

class_weight_list = compute_class_weight('balanced', numpy.unique(y_train), y_train)
class_weight = dict(zip(numpy.unique(y_train), class_weight_list))
x_train=x_train.astype('float64')
x_test=x_test.astype('float64')

x_train/=255
x_test/=255

y_train=keras.utils.to_categorical(y_train, num_classes)
y_test=keras.utils.to_categorical(y_test, num_classes)
hist=model.fit(x_train, y_train, 
               batch_size=batch_size,
               epochs=epochs,
               validation_data=(x_test, y_test),
               callbacks=[checkpoint],
               class_weight=class_weight
               )

【问题讨论】:

    标签: python numpy tensorflow deep-learning keras


    【解决方案1】:

    先尝试标签编码

    编辑

    encoder = LabelEncoder()
    encoder.fit(y_train)
    y_train= encoder.transform(y_train)
    y_test= encoder.transform(y_test)
    
    class_weight_list = compute_class_weight('balanced', numpy.unique(y_train), y_train)
    class_weight = dict(zip(numpy.unique(y_train), class_weight_list))
    
    y_train=keras.utils.to_categorical(y_train, num_classes)  
    

    【讨论】:

    • 它抛出错误 unhashable type: 'numpy.ndarray'
    • ValueError: bad input shape (11750, 4)
    • 可以打印原始 y_train 输入的 head 吗?
    • this (11750, 100, 100, 1) (11750,) (5875, 100, 100, 1) (5875,) #x=x.reshape((-1,100,100,1)) #我重塑为 4
    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 2018-09-06
    • 2018-09-23
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2021-12-07
    相关资源
    最近更新 更多