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:
        for i in range(from_, int(to_)):
            x = np.load(PathSpectogramFolder+f) 
            x = np.expand_dims(x, axis=0) 
            if('P' in f):
                y = np.repeat([[0,1]],x.shape[0], axis=0)
                y =np.repeat([[1,0]],x.shape[0], axis=0)
history=model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75), 
                                validation_data=generate_arrays_for_training(indexPat, filesPath, start=75),
                                epochs=15, max_queue_size=2, shuffle=True, callbacks=[callback])

  • $\begingroup$ What you exactly want to do when you said - I want to balance my data equally $\endgroup$ – 10xAI Oct 1 '20 at 12:12
  • $\begingroup$ @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. $\endgroup$ – Edayildiz Oct 1 '20 at 17:39
  • $\begingroup$ Add an OS-level script and put all data into two folders. Then pick X in the 50:50 ratio from each folder. $\endgroup$ – 10xAI Oct 2 '20 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.