不平衡学习“ balanced_batch_generator”以获得>二维数据

时间:2020-04-10 19:47:54

标签: keras imblearn

我正在使用不平衡学习的“ balanced_batch_generator”来尝试对具有4维的图像阵列执行欠采样。我运行了以下代码:

training_generator, steps_per_epoch = balanced_batch_generator(x_train, y_train, sampler=NearMiss(), batch_size=10)

,并出现以下错误:

ValueError: Found array with dim 4. Estimator expected <= 2.

我知道此函数不接受2维数据,但是我想知道是否可以解决此问题。我可以通过手动分割数据来进行欠采样/过采样,但是我想利用keras很好实现的功能,例如NearMiss来智能地采样数据。

1 个答案:

答案 0 :(得分:0)

您需要为x_train重塑数组:

x.train.shape #So you know the values for all 4 dims (1st dim, 2nd dim, 3rd dm, 4th dm)
x_train = x_train.reshape(x_train.shape[0],-1)

然后使用balanced_batch_generator。然后:

x_train = x_train.reshape(x_train.shape[0], initial Value for the 2nd dim, initial Value for the 3rd, initial Value for the 4th)

for x_train, y_train in training_generator:
    break

您的x_train包含具有初始尺寸的平衡批次。您无需重塑y_train的形状,因为它仅由标签组成。 (dim <=2

如果您不想一直输入昏暗的值,我会使用保存值的​​变量。