# How to balance dataset using fit_generator() in Keras?

I am trying to use keras to fit a CNN model to classify 2 classes of data . I have imbalanced dataset I want to balance my data equally. How I can do that ??

Any help would be appreciated

The main code:

def generate_arrays_for_training(indexPat, paths, start=0, end=100):
while True:
from_=int(len(paths)/100*start)
to_=int(len(paths)/100*end)
for i in range(from_, int(to_)):
f=paths[i]
x = np.expand_dims(x, axis=0)

if('P' in f):
y = np.repeat([[0,1]],x.shape[0], axis=0)
else:
y =np.repeat([[1,0]],x.shape[0], axis=0)
yield(x,y)
history=model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75),
validation_data=generate_arrays_for_training(indexPat, filesPath, start=75),
steps_per_epoch=int((len(filesPath)-int(len(filesPath)/100*25))),
validation_steps=int((len(filesPath)-int(len(filesPath)/100*75))),
verbose=2,
epochs=15, max_queue_size=2, shuffle=True, callbacks=[callback])

$$$$

• What you exactly want to do when you said - I want to balance my data equally – 10xAI Oct 1 '20 at 12:12
• @10xAI I have 2 classes of data generated from generate_arrays_for_training` the data of class 1 more than the data of class 2. I want to balance the data of the 2 classes equally. so the data of class 1 equal to the data of class 2. – Edayildiz Oct 1 '20 at 17:39
• Add an OS-level script and put all data into two folders. Then pick X in the 50:50 ratio from each folder. – 10xAI Oct 2 '20 at 13:53