# How does class_weights work in RandomForestClassifier

I'm facing a problem with unbalanced classes, and have tried out a couple of methods like over and under sampling. However, my cross validation mean comes out to be only 0.4 and my confusion matrix shows that the prediction and recall is completely incorrect.

Ive read that the next step is to add weights to my class; I have two of them "Won" and "Lost". In Pandas, how am I supposed to assign weights to them? I know that there is a "class_weights" attribute, but I have no clue on how to use it.

Thanks

PS. My "Won" class is unbalanced, very small compared to the "Lost" one. I train by repeating the set of "Won"s twice and randomly sample an almost equal amount of "Lost"s. I've tried all sorts of combinations of the classes.

• The easiest way (and first thing to try) is to set class_weight="balanced". See if that improves your score... – stmax May 3 '16 at 14:04
• Thanks, but I tried that and the O/P wasn't any better. Is 'auto' also an option for class_weight? I tried using a dict like {'Lost':0.5,'Won':1}, but that just threw an error – TdBm May 3 '16 at 14:12
• But why didn't stmax suggestion work: > The easiest way (and first thing to try) is to set > class_weight="balanced". See if that improves your score... – stmax > May 3 '16 at 14:04 – Pobo Jul 5 '18 at 17:23

Maybe try to encode your target values as binary. Then, this class_weight={0:1,1:2} should do the job. Now, class 0 has weight 1 and class 1 has weight 2.