Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

python - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...