简体   繁体   中英

class_weight for imbalanced data - Keras

I am trying to perform binary classification with a highly imbalanced dataset. My target values are 0(84%) and 1 (16%). I used class_weight in my model but the precision and recall for the minority class is always 0. I am not sure if i am using class_weights correctly. Would really appreciate any help on this!

Below is my code:

class_weight = {0:1,1:50}
numpy.random.seed(5)

model = Sequential()
model.add(Dense(13,input_dim = 5, activation='relu'))
model.add(Dense(13, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss="binary_crossentropy", optimizer = "adam", metrics = ['accuracy'])
model.fit(X_train,Y_train, epochs = 10, batch_size = 30, class_weight = class_weight, validation_data = (X_test, Y_test))
preds = model.predict_classes(X_test)
print (classification_report(Y_test, preds))

           precision    recall  f1-score   support

      0       0.83      1.00      0.91     24126
      1       0.00      0.00      0.00      4879

Don't have enough reputation to add a comment. Hence writing as an answer.

You say your class imbalance is 84:16 (5:1 approx) but you are sending your Class 2 50 times as Class 1. Try some value between 5-10

As far as I can tell you are using it correctly. I'm not sure however about the imbalance ratio on your dataset. If you want, scikit-learn has a function that computes this for you.

That being said, I personally prefer over/under-sampling to class weights. The algorithm I have had most success with is called SMOTE . You should definitely try this out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM