# Does class_weight solve unbalanced input for Decision Tree?

I've read in sklearn's documentation that we have to take special care in balancing the input for a decision tree, but it doesn't tell you what function to use. However, I've found the parameter class_weight. If I use class_weight: balanced as a parameter, will that mean that I can omit balancing the input by hand?

Yes you don't need to balance your train data by hand. But your test data can still be (truly) unbalanced.

• But the test data can be unbalanced, right? That wouldn't make the algorithm perform less accurately, if the tree is accounting for the input's unbalances. – lte__ Jan 17 '18 at 14:34
• you are correct – Dirk Nachbar Jan 17 '18 at 14:37

For classification problems, not just decision trees, it isn't uncommon for unbalanced classes to give overly optimistic accuracy scores. There's a few common ways to handle this.

1. Resamble your data. You can oversample the minority class or undersample the majority class. The end goal is to balance out the data more or less.

2. Give your model a prior to help inform frequency.

3. Similar you can pass a weight argument. The weight will penalize the classification function for misclassification of a rare positive cases.

4. Lastly you can modify the accuracy measurement. A common measurement would be to use F1 statistic instead of just accuracy. Maximize the F1 statistic by cross validation and see if it's stable during testing.

I also advise you to combine these techniques. For example, give your model a prior and use the F1 score. I think you'll find good results going this route.

• can class_weight="balanced" in sklearn used with cross validation? Looking forward to hearing from you. Thank you :) – EmJ Mar 30 '19 at 21:06
• yes, you pass class_weight to the model function, and then pass the model to cross_val_score or similar – Paul Aug 23 '19 at 0:14