WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Keras · Intermediate · question 24 of 100

How do you handle imbalanced datasets in Keras, and what techniques can you use to address this issue?

📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

Handling imbalanced datasets is a common problem in Machine Learning, and Keras provides several techniques to address this issue. Imbalanced datasets occur when the number of observations in the minority class is significantly lower than the number of observations in the majority class.

Here are some techniques that you can use to handle imbalanced datasets in Keras:

1. Resampling: You can either undersample the majority class or oversample the minority class. Undersampling involves randomly removing observations from the majority class, whereas oversampling involves replicating minority class observations until they are in the same proportion as the majority class. This technique can be performed using Keras ‘fit_generator‘ function’s ‘steps_per_epoch‘ argument and ‘ImageDataGenerator‘ from ‘keras.preprocessing.image‘ function.

2. Class Weights: In Keras, the ‘class_weights‘ argument lets you assign a weight to each class to balance out the imbalanced dataset. This approach gives more importance to the minority class during model training, and can be done with the help of the ‘fit‘ method in Keras.

3. Synthetic data generation: Another approach is to create synthetic data for the minority class by applying data augmentation techniques such as rotation, zooming, cropping, and flipping. Keras provides an ‘ImageDataGenerator‘ class that can be used for data augmentation.

4. Ensemble Methods: Ensemble approaches like Bagging or Boosting techniques can also be used to handle imbalanced datasets in Keras. These methods combine models or predictions from multiple models that were trained on different subsets of the data, in order to increase accuracy and reduce bias.

Example of implementing these techniques in Keras:

# import necessary libraries
import keras
from keras.utils import to_categorical
from imblearn.over_sampling import RandomOverSampler

# load the dataset
(X_train, y_train), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()

# oversample the minority class
ros = RandomOverSampler()
X_train_resampled, y_train_resampled = ros.fit_resample(X_train.reshape(-1, 784), y_train)
X_train_resampled = X_train_resampled.reshape(-1, 28, 28, 1)

# convert class vectors to binary class matrices
num_classes = 10
y_train_binary = to_categorical(y_train_resampled, num_classes)
y_test_binary = to_categorical(y_test, num_classes)

# create data generator object for image augmentation
datagen = keras.preprocessing.image.ImageDataGenerator(rotation_range=20, zoom_range=0.2, 
                                                       width_shift_range=0.1, height_shift_range=0.1, 
                                                       shear_range=0.15, horizontal_flip=True, 
                                                       vertical_flip=True, fill_mode="nearest")

# create Keras model and compile it
model = keras.Sequential([...] )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit the model using class weights
class_weights = {0: 1.0, 1: 3.0, 2:4.0, 3:4.0, 4:4.0, 5:1.0, 6:2.0, 7:4.0, 8:4.0, 9:1.0}
history = model.fit(datagen.flow(X_train_resampled, y_train_binary, batch_size=64),
                    epochs=50, validation_data=(X_test, y_test_binary), class_weight=class_weights)

In the above example, we applied oversampling using ‘RandomOverSampler()‘ to balance the classes, and created a ‘ImageDataGenerator‘ object for data augmentation. We further used class weights dictionary to apply more weight to minority classes during model training.

Moreover, each specific dataset may require a different approach to balance classes, and the above techniques just provide a starting point to deal with imbalanced datasets in Keras.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Keras interview — then scores it.
📞 Practice Keras — free 15 min
📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

All 100 Keras questions · All topics