2
$\begingroup$

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?

$\endgroup$

2 Answers 2

4
$\begingroup$

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

$\endgroup$
2
  • 2
    $\begingroup$ 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. $\endgroup$
    – lte__
    Jan 17, 2018 at 14:34
  • $\begingroup$ you are correct $\endgroup$
    – Dirk N
    Jan 17, 2018 at 14:37
4
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.