使用Sklearn进行分层的kfold拆分,当我尝试使用多类拆分时,收到错误消息(请参见下文)。当我尝试使用二进制文件进行拆分时,它没有问题。

num_classes = len(np.unique(y_train))
y_train_categorical = keras.utils.to_categorical(y_train, num_classes)
kf=StratifiedKFold(n_splits=5, shuffle=True, random_state=999)

# splitting data into different folds
for i, (train_index, val_index) in enumerate(kf.split(x_train, y_train_categorical)):
    x_train_kf, x_val_kf = x_train[train_index], x_train[val_index]
    y_train_kf, y_val_kf = y_train[train_index], y_train[val_index]

ValueError: Supported target types are: ('binary', 'multiclass'). Got 'multilabel-indicator' instead.

最佳答案

keras.utils.to_categorical产生一个单编码的类向量,即错误消息中提到的multilabel-indicatorStratifiedKFold不适用于此类输入;从split方法docs:



也就是说,您的y必须是类别标签的一维数组。

本质上,您要做的只是简单地反转操作顺序:首先拆分(使用您的初始y_train),然后再转换to_categorical

关于python - Sklearn StratifiedKFold : ValueError: Supported target types are: ('binary' , 'multiclass')。改为 'multilabel-indicator',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48508036/

10-11 06:52