-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_tree
executable file
·148 lines (125 loc) · 8.19 KB
/
decision_tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Introduction to Decision Trees
To explain decision trees, we'll use a cat classification example. Imagine running a cat adoption center where you want
to classify animals as cats or not based on features like ear shape, face shape, and the presence of whiskers.
The dataset includes 10 examples with features and labels indicating whether the animal is a cat.
Features and Labels
Features (X): Ear shape, face shape, whiskers (categorical values)
Labels (Y): Is this a cat? (binary classification: 1 for cat, 0 for not cat)
Decision Tree Model
A decision tree model looks like a tree with nodes and branches. Here's how it works:
Root Node: The topmost node where the decision process starts.
Decision Nodes: Nodes that look at a feature and decide which branch to follow.
Leaf Nodes: Nodes at the bottom that make the final prediction.
Example Process
For a new test example (e.g., pointy ears, round face, whiskers present):
Start at the root node (ear shape).
Follow the branch based on the feature value (pointy).
Move to the next decision node (face shape).
Continue until reaching a leaf node that makes the prediction (cat).
Multiple Decision Trees
Different decision trees can be built for the same application. The goal of the decision tree learning algorithm is
to select a tree that performs well on the training set and generalizes well to new data (cross-validation and test sets).
Building a Decision Tree: Key Steps
Choose the Root Node Feature:
Start with a training set (e.g., 10 examples of cats and dogs).
Decide which feature to use at the root node (e.g., ear shape).
Split the training examples based on the chosen feature.
Split the Data:
For each branch, decide the next feature to split on (e.g., face shape).
Continue splitting the data into subsets based on feature values.
Create Leaf Nodes:
When a subset contains only one class (all cats or all dogs), create a leaf node that makes a prediction.
Repeat the process for both branches of the tree.
Key Decisions in Building a Decision Tree:
Feature Selection: Choose features that maximize purity (i.e., subsets with all cats or all dogs).
Stopping Criteria: Decide when to stop splitting:
When nodes are pure (100% cats or dogs).
When the tree reaches a maximum depth.
When improvements in purity are minimal.
When the number of examples in a node is below a threshold.
Handling Complexity:
Decision tree algorithms have evolved with various refinements.
Despite seeming complicated, these algorithms are effective.
Use open-source packages to simplify the process and make decisions easier.
Next Steps:
Learn about entropy to measure impurity and how to minimize it.
Regression Trees Overview:
Purpose: Regression trees predict a numerical value (Y) rather than a category.
Example: Predicting the weight of an animal using features (X) such as ear shape and face shape.
Key Points:
Target Output: The weight (Y) is the target output, not an input feature.
Tree Structure:
Root node splits on ear shape.
Subsequent nodes split on face shape.
Splitting on the same feature in different branches is acceptable.
Prediction Method:
At each leaf node, the prediction is the average weight of the training examples that reach that node.
Example: For animals with pointy ears and a round face, the predicted weight is the average of the weights at that node
(e.g., 8.35).
Splitting Criteria:
The choice of feature to split on at each node is crucial.
Example splits:
Ear shape: Results in two branches with different sets of weights.
Face shape: Another possible split with different weight distributions.
Presence of whiskers: Yet another split option.
Variance Reduction:
Instead of reducing entropy (used in classification), regression trees aim to reduce the variance of the target values
(Y) in each subset of data.
Choosing Splits in Regression Trees:
Splitting Criteria:
When building a regression tree, the goal is to choose the feature that best predicts the target value (Y).
Instead of reducing entropy (used in classification), regression trees aim to reduce the variance of the target values (Y).
Variance Calculation:
Variance measures how widely a set of numbers varies.
Example: For weights 7.2, 9.2, and 10.2, the variance is 1.47 (low variance). For weights 8.8, 15, 11, 18, and 20, the
variance is 21.87 (high variance).
Weighted Average Variance:
Compute the weighted average variance after a split.
Example: If splitting on ear shape, calculate the variance for each branch and then the weighted average variance.
Reduction in Variance:
Measure the reduction in variance to evaluate the quality of the split.
Example: If the root node variance is 20.51 and the weighted average variance after splitting on ear shape is 8.84, the
reduction in variance is 8.84.
Choosing the Best Split:
Compare the reduction in variance for different features.
Choose the feature with the largest reduction in variance.
Using a single decision tree can be problematic because it's highly sensitive to small changes in the data. To address
this, we can build multiple decision trees, known as a tree ensemble, to make the algorithm more robust.
For example, if we change just one training example in our dataset, the best feature to split on might change, leading
to a completely different tree. This sensitivity makes single decision trees less reliable. By using an ensemble of
trees, we can mitigate this issue.
In a tree ensemble, each tree is trained on a different subset of the data. When making a prediction, each tree votes,
and the majority vote determines the final prediction. This approach reduces the impact of any single tree's errors,
making the overall algorithm more robust.
In conclusion, XGBoost has become the most widely used algorithm for building decision tree ensembles due to its speed,
ease of use, and success in both competitions and commercial applications. The key innovation in XGBoost is its
focus on misclassified examples, similar to the concept of deliberate practice in learning. By assigning higher
probabilities to these challenging examples, XGBoost improves the performance of each subsequent decision tree
in the ensemble. This approach, combined with built-in regularization and efficient implementation, makes XGBoost
a powerful tool for both classification and regression tasks.
Both decision trees (including tree ensembles) and neural networks are powerful and effective learning algorithms,
but they have different strengths and are suited to different types of data and tasks.
Decision Trees and Tree Ensembles
Pros:
Effective on Tabular Data: They work well on structured data, like datasets that resemble spreadsheets (e.g.,
housing price prediction with features like size, number of bedrooms, etc.).
Fast Training: They are generally quick to train, allowing for faster iterations in the machine learning development loop.
Interpretability: Small decision trees can be human-interpretable, making it easier to understand how decisions are made.
Preferred Algorithm: For most applications, using a tree ensemble like XGBoost is recommended due to its performance.
Cons:
Not Suitable for Unstructured Data: They are less effective on unstructured data such as images, video, audio, and text.
Complexity in Interpretation: Large ensembles of trees can be difficult to interpret without specialized visualization
techniques.
Computational Cost: Tree ensembles are more computationally expensive than single decision trees.
Neural Networks
Pros:
Versatility: They work well on both structured and unstructured data, including images, video, audio, and text.
Transfer Learning: Neural networks can leverage transfer learning, which is beneficial for applications with small datasets.
Integration: Easier to integrate and train multiple neural networks together using gradient descent.
Cons:
Training Time: Large neural networks can take a long time to train, which can slow down the development process.
Summary
Use Decision Trees/Tree Ensembles: When working with structured/tabular data and when fast training and interpretability
are important.
Use Neural Networks: When dealing with unstructured data or when transfer learning and integration of multiple models are
needed.