Neural Network Model Balanced Weight For Imbalanced Classification In Keras

Amy @GrabNGoInfo
GrabNGoInfo
Published in
6 min readMay 14, 2022

--

Adjusting the balanced weight for the cost function to give more attention to the minority class in a neural network model

Neural Network Model Balanced Weight For Imbalanced Classification In Keras. Adjusting the balanced weight for the cost function to give more attention to the minority class in a neural network model
Photo by JJ Ying on Unsplash

When using a neural network model to classify imbalanced data, we can adjust the balanced weight for the cost function to give more attention to the minority class. Python’s Keras library has a built-in option called class_weight to help us achieve this quickly.

One benefit of using the balanced weight adjustment is that we can use the imbalanced data to build the model directly without oversampling or under-sampling before training the model. To learn about oversampling and under-sampling techniques, please check my previous posts here and here.

In this tutorial, we will go over the following topics:

  • Baseline neural network model for imbalanced classification
  • Calculate class weight using sklearn
  • Apply class weight on a neural network model
  • Apply manual class weight on a neural network model

Resources for this post:

  • Video tutorial on YouTube
  • Python code is at the end of the post. Click here for the notebook
  • More video tutorials on anomaly

--

--