0
$\begingroup$

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.load(PathSpectogramFolder+f) 
            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])

```
$\endgroup$
3
  • $\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.