我正在使用Python和Keras库构建分类神经网络。 我正在使用三个不同类别的不平衡数据集来训练NN。 类1的普及率大约是类2和类3的7.5倍。 为了解决这个问题,我采取了这个stackoverflow答案的建议,并设置了我的类权重如下:
class_weight = {0 : 1,
1 : 6.5,
2: 7.5}
然而,这里有一个问题:ANN以相等的频率预测3个类!这是没有用的,因为数据集不平衡,在预测结果每个类的概率都是33%时是不准确的。问题是:如何处理不平衡的数据集,使得ANN不会每次都预测类别1,但也不会以相等的概率预测其他类别?以下是我正在使用的代码:
class_weight = {0 : 1,
1 : 6.5,
2: 7.5}
# Making the ANN
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
classifier = Sequential()
# Adding the input layer and the first hidden layer with dropout
classifier.add(Dense(activation = 'relu',
input_dim = 5,
units = 3,
kernel_initializer = 'uniform'))
#Randomly drops 0.1, 10% of the neurons in the layer.
classifier.add(Dropout(rate= 0.1))
#Adding the second hidden layer
classifier.add(Dense(activation = 'relu',
units = 3,
kernel_initializer = 'uniform'))
#Randomly drops 0.1, 10% of the neurons in the layer.
classifier.add(Dropout(rate = 0.1))
# Adding the output layer
classifier.add(Dense(activation = 'sigmoid',
units = 2,
kernel_initializer = 'uniform'))
# Compiling the ANN
classifier.compile(optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = ['accuracy'])
# Fitting the ANN to the training set
classifier.fit(X_train, y_train, batch_size = 100, epochs = 100, class_weight = class_weight)