3
$\begingroup$

I have read online that decision trees can solve xOR type problems, as shown in images (xOR problem: 1) and (Possible solution as decision tree: 2).

My question is how can a decision tree learn to solve this problem in this scenario. I just don't see a way for any metric (Information gain, Gini Score, ...) to choose one of the splits in image 2 over any other random split.

Is it possible to solve the presented problem with a decision tree? Would using a random forest solve the problem in any way?

Thank you in advance.

$\endgroup$
1
$\begingroup$

Yes, a decision tree can learn an XOR.

I have read online that decision trees can solve xOR type problems...

Often things are phrased not carefully enough. A neural network can perfectly sort a list of integers, but training one to do that would be quite hard. Your image shows that a tree can easily represent the XOR function, but your question is how to learn such a tree structure.

My question is how can a decision tree learn to solve this problem in this scenario. I just don't see a way for any metric (Information gain, Gini Score, ...) to choose one of the splits in image 2 over any other random split.

Indeed, the first split is probably quite random, or due to noise (if you go for $\operatorname{sign}(x\cdot y)$ with continuous $x,y$ instead of the discrete $x,y$ and XOR). But, as long as your algorithm makes the plunge with one of those first splits, the next splits are obvious and your tree will make them.

Is it possible to solve the presented problem with a decision tree?

Here's a notebook (github/colab, suggestions welcome) demonstrating that yes, a (sklearn) decision tree can learn $\operatorname{sign}(x\cdot y)$ (perhaps with some errors when points are extremely close to 0); but it also goes on to show some of the difficulties, e.g. when variables other than $x,y$ are available to the tree to split on. Up-shot: noise variables can wreck that first split I mentioned above, and even useful variables can make the tree lose track of the XOR.

Would using a random forest solve the problem in any way?

Probably not the basic problem, but it looks like it helps with, e.g., the noise variables above.

| improve this answer | |
$\endgroup$
0
$\begingroup$

Yes, it is possible to implement XOR with decision tree.

the XOR gate:

if x == y
  class = 0
else
  class = 1

A simple discrete decision tree could therefore be:

N1: is x == 1 ? (yes -> N2, no -> N3)
N2: is y == 1 ? (yes -> class=0, no -> class=1)
N3: is y == 1 ? (yes -> class=1, no -> class=0)

You can therefore implement XOR with three decision nodes.

This can be applied to continuous values as well. I will demonstrate with the example you provided. In your example, we need to make a decision tree that considers the following:

if (x < 0.5 AND y > 0.4) OR (x > 0.5 AND y < 0.4)
  class = 1
else
  class = 2

Like the discrete example, this can be solved using three decision nodes:

N1: is x > 0.5 ? (yes -> N2, no -> N3)
N2: is y > 0.4 ? (yes -> class=2, no -> class=1)
N3: is y > 0.4 ? (yes -> class=1, no -> class=2)

Note that you can choose > or >= to skew classification toward your preferred class when a point falls precisely on the boundary.

I have pasted your example image here for reference: Your example

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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