```
import numpy as np
from sklearn.utils import class_weight
from sklearn.preprocessing import LabelEncoder
```

# Imbalanced classes in classification problem in deep learning with keras

Carvia Tech | August 31, 2019 | 1 min read | 0 views

While working on various problems in real world, we usually face problem with imbalanced classes in the data we have collected. A while ago, i also faced a similar problem while working on mood detection model. Problems that we face while working with imbalanced classes in data is that trained model usually gives biased results.

Here we will see how we can overcome this problem when we are building classification model with deep learning in keras.

There is a parameter named as `class_weight`

in `model.fit`

which can be used to balance the weights.

Here we will be using `class_weight`

from `sklearn`

(scikit-learn)

`label`

is the `pandas Series`

extracted from the data we have by choosing only label column

```
le = LabelEncoder()
labels = data[label_column]
```

Create a dict with weights of classes and use it in `model.fit`

```
class_weights = class_weight.compute_class_weight('balanced',
np.unique(labels),
labels)
class_weights_dict = dict(zip(le.transform(list(le.classes_)),
class_weights))
model.fit(x_train, y_train, validation_split, class_weight=class_weights_dict)
```

Full code will go something like below:

```
import numpy as np
from sklearn.utils import class_weight
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
labels = data[label_column]
class_weights = class_weight.compute_class_weight('balanced',
np.unique(labels),
labels)
class_weights_dict = dict(zip(le.transform(list(le.classes_)),
class_weights))
model.fit(x_train, y_train, validation_split, class_weight=class_weights_dict)
```

Hope this works for your problem. Thanks for reading.

###### Top articles in this category:

- Top 100 interview questions on Data Science & Machine Learning
- Google Data Scientist interview questions with answers
- Python Flask Interview Questions
- Python coding challenges for interviews
- Creating custom Keras callbacks in python
- Deploying Keras Model in Production using Flask
- Difference between Loss, Accuracy, Validation loss, Validation accuracy when training deep learning model with Keras

###### Find more on this topic:

##### Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

###### Recommended books for interview preparation:

##### Similar Posts

- Python coding challenges for interviews
- Python Flask Interview Questions
- Machine Learning based Multiple choice questions
- Sequence of Differences in Python
- Spaced Out?
- Find extra long factorials in python
- Find if credit card number is valid or not
- Imbalanced classes in classification problem in deep learning with keras
- Derivative of 1/x & Tossing a coin problem
- Introduction to SVM, hyperplane, TF-IDF and BoW

Enter your email address to subscribe to this blog and receive notifications of new posts by email.