I'm building a binary classification model using a neural network, with python and the libraries tensorflow and keras.

For that I have an unequal amount of labeled data: Around 2'000'000 labeled with 1 and only 25'000 with 0.

In order to train the model meaningfully, a balanced amount of data would be advantageous (as far as I understand).

However, I would also like to run the model with different ratios of the labeled data (3/1, 1/1, 1/3) in order to gain insights there too.

How is that possible? First of all to balance the imbalanced dataset, and then run the model with a given ratio (3/1, 1/1, 1/3) of labeled data.


1 Answer 1


Instead of subsampling the majority class, you can simply use class weights to give more importance to the minority class samples. For this, you can simply pass a dictionary with the weights to model.fit:

class_weight = {0: 1., 1: 50.}
model.fit(X_train, Y_train, nb_epoch=10, batch_size=255, class_weight=class_weight)

This way, instead of having different subsampling rations (75/25, 50/50, 25/75), you can train with different values of class_weight and evaluate their impact on the end result.

I suggest you check out the official Tensorflow tutorial on imbalanced data, where they discuss this approach.

  • $\begingroup$ Thanks for this hint. I followed the instructions on TensorFlow tutorial about imbalanced datas. The class_weights are now set, but the output of the confusions matrix is very one-sided, 99% are in the confusions matrix in the positive/positive field, and 1% distributed on the other fields. Accuracy (0.98) and f1-score also. But it looks like something is wrong with the confusions matrix. How can I fix this? $\endgroup$
    – user155518
    Feb 2 at 6:08
  • 1
    $\begingroup$ Please, create a new question with the source code so that the community and I can help with that. $\endgroup$
    – noe
    Feb 2 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.