使用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.
Run Code Online (Sandbox Code Playgroud) 我有一个 pandas 数据框,如下所示:
| Cliid | Segment | Insert |
|-------|---------|--------|
| 001 | A | 0 |
| 002 | A | 0 |
| 003 | C | 0 |
| 004 | B | 1 |
| 005 | A | 0 |
| 006 | B | 0 |
Run Code Online (Sandbox Code Playgroud)
我想将其分成 2 组,每组的 [Segment, Insert] 中每个变量的组成都相同。例如,每个组有 1/2 的观测值属于 A 段,1/6 的插入 = 1,依此类推。
我已经检查过这个答案,但它只对一个变量进行分层,它不适用于多个变量。
R 有这个函数可以做到这一点,但使用 R 不是一个选择。
顺便说一下,我使用的是Python 3。