|
| 1 | +############################### |
| 2 | +Feature Interaction Constraints |
| 3 | +############################### |
| 4 | + |
| 5 | +The decision tree is a powerful tool to discover interaction among independent |
| 6 | +variables (features). Variables that appear together in a traversal path |
| 7 | +are interacting with one another, since the condition of a child node is |
| 8 | +predicated on the condition of the parent node. For example, the highlighted |
| 9 | +red path in the diagram below contains three variables: :math:`x_1`, :math:`x_7`, |
| 10 | +and :math:`x_{10}`, so the highlighted prediction (at the highlighted leaf node) |
| 11 | +is the product of interaction between :math:`x_1`, :math:`x_7`, and |
| 12 | +:math:`x_{10}`. |
| 13 | + |
| 14 | +.. plot:: |
| 15 | + :nofigs: |
| 16 | + |
| 17 | + from graphviz import Source |
| 18 | + source = r""" |
| 19 | + digraph feature_interaction_illustration1 { |
| 20 | + graph [fontname = "helvetica"]; |
| 21 | + node [fontname = "helvetica"]; |
| 22 | + edge [fontname = "helvetica"]; |
| 23 | + 0 [label=<x<SUB><FONT POINT-SIZE="11">10</FONT></SUB> < -1.5 ?>, shape=box, color=red, fontcolor=red]; |
| 24 | + 1 [label=<x<SUB><FONT POINT-SIZE="11">2</FONT></SUB> < 2 ?>, shape=box]; |
| 25 | + 2 [label=<x<SUB><FONT POINT-SIZE="11">7</FONT></SUB> < 0.3 ?>, shape=box, color=red, fontcolor=red]; |
| 26 | + 3 [label="...", shape=none]; |
| 27 | + 4 [label="...", shape=none]; |
| 28 | + 5 [label=<x<SUB><FONT POINT-SIZE="11">1</FONT></SUB> < 0.5 ?>, shape=box, color=red, fontcolor=red]; |
| 29 | + 6 [label="...", shape=none]; |
| 30 | + 7 [label="...", shape=none]; |
| 31 | + 8 [label="Predict +1.3", color=red, fontcolor=red]; |
| 32 | + 0 -> 1 [labeldistance=2.0, labelangle=45, headlabel="Yes/Missing "]; |
| 33 | + 0 -> 2 [labeldistance=2.0, labelangle=-45, |
| 34 | + headlabel="No", color=red, fontcolor=red]; |
| 35 | + 1 -> 3 [labeldistance=2.0, labelangle=45, headlabel="Yes"]; |
| 36 | + 1 -> 4 [labeldistance=2.0, labelangle=-45, headlabel=" No/Missing"]; |
| 37 | + 2 -> 5 [labeldistance=2.0, labelangle=-45, headlabel="Yes", |
| 38 | + color=red, fontcolor=red]; |
| 39 | + 2 -> 6 [labeldistance=2.0, labelangle=-45, headlabel=" No/Missing"]; |
| 40 | + 5 -> 7; |
| 41 | + 5 -> 8 [color=red]; |
| 42 | + } |
| 43 | + """ |
| 44 | + Source(source, format='png').render('../_static/feature_interaction_illustration1', view=False) |
| 45 | + Source(source, format='svg').render('../_static/feature_interaction_illustration1', view=False) |
| 46 | + |
| 47 | +.. raw:: html |
| 48 | + |
| 49 | + <p> |
| 50 | + <img src="../_static/feature_interaction_illustration1.svg" |
| 51 | + onerror="this.src='../_static/feature_interaction_illustration1.png'; this.onerror=null;"> |
| 52 | + </p> |
| 53 | + |
| 54 | +When the tree depth is larger than one, many variables interact on |
| 55 | +the sole basis of minimizing training loss, and the resulting decision tree may |
| 56 | +capture a spurious relationship (noise) rather than a legitimate relationship |
| 57 | +that generalizes across different datasets. **Feature interaction constraints** |
| 58 | +allow users to decide which variables are allowed to interact and which are not. |
| 59 | + |
| 60 | +Potential benefits include: |
| 61 | + |
| 62 | +* Better predictive performance from focusing on interactions that work -- |
| 63 | + whether through domain specific knowledge or algorithms that rank interactions |
| 64 | +* Less noise in predictions; better generalization |
| 65 | +* More control to the user on what the model can fit. For example, the user may |
| 66 | + want to exclude some interactions even if they perform well due to regulatory |
| 67 | + constraints |
| 68 | + |
| 69 | +**************** |
| 70 | +A Simple Example |
| 71 | +**************** |
| 72 | + |
| 73 | +Feature interaction constraints are expressed in terms of groups of variables |
| 74 | +that are allowed to interact. For example, the constraint |
| 75 | +``[0, 1]`` indicates that variables :math:`x_0` and :math:`x_1` are allowed to |
| 76 | +interact with each other but with no other variable. Similarly, ``[2, 3, 4]`` |
| 77 | +indicates that :math:`x_2`, :math:`x_3`, and :math:`x_4` are allowed to |
| 78 | +interact with one another but with no other variable. A set of feature |
| 79 | +interaction constraints is expressed as a nested list, e.g. |
| 80 | +``[[0, 1], [2, 3, 4]]``, where each inner list is a group of indices of features |
| 81 | +that are allowed to interact with each other. |
| 82 | + |
| 83 | +In the following diagram, the left decision tree is in violation of the first |
| 84 | +constraint (``[0, 1]``), whereas the right decision tree complies with both the |
| 85 | +first and second constraints (``[0, 1]``, ``[2, 3, 4]``). |
| 86 | + |
| 87 | +.. plot:: |
| 88 | + :nofigs: |
| 89 | + |
| 90 | + from graphviz import Source |
| 91 | + source = r""" |
| 92 | + digraph feature_interaction_illustration2 { |
| 93 | + graph [fontname = "helvetica"]; |
| 94 | + node [fontname = "helvetica"]; |
| 95 | + edge [fontname = "helvetica"]; |
| 96 | + 0 [label=<x<SUB><FONT POINT-SIZE="11">0</FONT></SUB> < 5.0 ?>, shape=box]; |
| 97 | + 1 [label=<x<SUB><FONT POINT-SIZE="11">2</FONT></SUB> < -3.0 ?>, shape=box]; |
| 98 | + 2 [label="+0.6"]; |
| 99 | + 3 [label="-0.4"]; |
| 100 | + 4 [label="+1.2"]; |
| 101 | + 0 -> 1 [labeldistance=2.0, labelangle=45, headlabel="Yes/Missing "]; |
| 102 | + 0 -> 2 [labeldistance=2.0, labelangle=-45, headlabel="No"]; |
| 103 | + 1 -> 3 [labeldistance=2.0, labelangle=45, headlabel="Yes"]; |
| 104 | + 1 -> 4 [labeldistance=2.0, labelangle=-45, headlabel=" No/Missing"]; |
| 105 | + } |
| 106 | + """ |
| 107 | + Source(source, format='png').render('../_static/feature_interaction_illustration2', view=False) |
| 108 | + Source(source, format='svg').render('../_static/feature_interaction_illustration2', view=False) |
| 109 | + |
| 110 | +.. plot:: |
| 111 | + :nofigs: |
| 112 | + |
| 113 | + from graphviz import Source |
| 114 | + source = r""" |
| 115 | + digraph feature_interaction_illustration3 { |
| 116 | + graph [fontname = "helvetica"]; |
| 117 | + node [fontname = "helvetica"]; |
| 118 | + edge [fontname = "helvetica"]; |
| 119 | + 0 [label=<x<SUB><FONT POINT-SIZE="11">3</FONT></SUB> < 2.5 ?>, shape=box]; |
| 120 | + 1 [label="+1.6"]; |
| 121 | + 2 [label=<x<SUB><FONT POINT-SIZE="11">2</FONT></SUB> < -1.2 ?>, shape=box]; |
| 122 | + 3 [label="+0.1"]; |
| 123 | + 4 [label="-0.3"]; |
| 124 | + 0 -> 1 [labeldistance=2.0, labelangle=45, headlabel="Yes"]; |
| 125 | + 0 -> 2 [labeldistance=2.0, labelangle=-45, headlabel=" No/Missing"]; |
| 126 | + 2 -> 3 [labeldistance=2.0, labelangle=45, headlabel="Yes/Missing "]; |
| 127 | + 2 -> 4 [labeldistance=2.0, labelangle=-45, headlabel="No"]; |
| 128 | + } |
| 129 | + """ |
| 130 | + Source(source, format='png').render('../_static/feature_interaction_illustration3', view=False) |
| 131 | + Source(source, format='svg').render('../_static/feature_interaction_illustration3', view=False) |
| 132 | + |
| 133 | +.. raw:: html |
| 134 | + |
| 135 | + <p> |
| 136 | + <img src="../_static/feature_interaction_illustration2.svg" |
| 137 | + onerror="this.src='../_static/feature_interaction_illustration2.png'; this.onerror=null;"> |
| 138 | + <img src="../_static/feature_interaction_illustration3.svg" |
| 139 | + onerror="this.src='../_static/feature_interaction_illustration3.png'; this.onerror=null;"> |
| 140 | + </p> |
| 141 | + |
| 142 | +**************************************************** |
| 143 | +Enforcing Feature Interaction Constraints in XGBoost |
| 144 | +**************************************************** |
| 145 | + |
| 146 | +It is very simple to enforce monotonicity constraints in XGBoost. Here we will |
| 147 | +give an example using Python, but the same general idea generalizes to other |
| 148 | +platforms. |
| 149 | + |
| 150 | +Suppose the following code fits your model without monotonicity constraints: |
| 151 | + |
| 152 | +.. code-block:: python |
| 153 | +
|
| 154 | + model_no_constraints = xgb.train(params, dtrain, |
| 155 | + num_boost_round = 1000, evals = evallist, |
| 156 | + early_stopping_rounds = 10) |
| 157 | +
|
| 158 | +Then fitting with monotonicity constraints only requires adding a single |
| 159 | +parameter: |
| 160 | + |
| 161 | +.. code-block:: python |
| 162 | +
|
| 163 | + params_constrained = params.copy() |
| 164 | + # Use nested list to define feature interaction constraints |
| 165 | + params_constrained['interaction_constraints'] = '[[0, 2], [1, 3, 4], [5, 6]]' |
| 166 | + # Features 0 and 2 are allowed to interact with each other but with no other feature |
| 167 | + # Features 1, 3, 4 are allowed to interact with one another but with no other feature |
| 168 | + # Features 5 and 6 are allowed to interact with each other but with no other feature |
| 169 | +
|
| 170 | + model_with_constraints = xgb.train(params_constrained, dtrain, |
| 171 | + num_boost_round = 1000, evals = evallist, |
| 172 | + early_stopping_rounds = 10) |
| 173 | +
|
| 174 | +**Choice of tree construction algorithm**. To use feature interaction |
| 175 | +constraints, be sure to set the ``tree_method`` parameter to either ``exact`` |
| 176 | +or ``hist``. Currently, GPU algorithms (``gpu_hist``, ``gpu_exact``) do not |
| 177 | +support feature interaction constraints. |
0 commit comments