You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/mllib-decision-tree.md
+20-10Lines changed: 20 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,29 +3,29 @@ layout: global
3
3
title: MLlib - Decision Tree
4
4
---
5
5
6
-
Decision trees and their ensembles are popular methods for the machine learning tasks of classification and regression. Decision trees are widely used since they are easy to interpret, handle categorical variables, extend to the multi-class classification setting, do not require feature scaling and are able to capture non-linearities and feature interactions. Tree ensemble algorithms such as decision forest and boosting are among the top performers for classification and regression tasks.
6
+
Decision trees and their ensembles are popular methods for the machine learning tasks of classification and regression. Decision trees are widely used since they are easy to interpret, handle categorical variables, extend to the multiclass classification setting, do not require feature scaling and are able to capture nonlinearities and feature interactions. Tree ensemble algorithms such as decision forest and boosting are among the top performers for classification and regression tasks.
7
7
8
8
## Basic Algorithm
9
9
10
10
The decision tree is a greedy algorithm that performs a recursive binary partitioning of the feature space by choosing a single element from the *best split set* where each element of the set maximimizes the information gain at a tree node. In other words, the split chosen at each tree node is chosen from the set `$\underset{s}{\operatorname{argmax}} IG(D,s)$` where `$IG(D,s)$` is the information gain when a split `$s$` is applied to a dataset `$D$`.
11
11
12
12
### Node Impurity and Information Gain
13
13
14
-
The *node impurity* is a measure of the homogeneity of the labels at the node. The current implementation provides two impurity measures for classification (Gini index and entropy) and one impurity measure for regression (variance).
14
+
The *node impurity* is a measure of the homogeneity of the labels at the node. The current implementation provides two impurity measures for classification (Gini impurity and entropy) and one impurity measure for regression (variance).
<td>Gini index</td><td>Classification</td><td>$\sum_{i=1}^{M} f_i(1-f_i)$</td><td>$f_i$ is the frequency of label $i$ at a node and $M$ is the number of unique labels.</td>
22
+
<td>Gini impurity</td><td>Classification</td><td>$\sum_{i=1}^{M} f_i(1-f_i)$</td><td>$f_i$ is the frequency of label $i$ at a node and $M$ is the number of unique labels.</td>
23
23
</tr>
24
24
<tr>
25
25
<td>Entropy</td><td>Classification</td><td>$\sum_{i=1}^{M} -f_ilog(f_i)$</td><td>$f_i$ is the frequency of label $i$ at a node and $M$ is the number of unique labels.</td>
26
26
</tr>
27
27
<tr>
28
-
<td>Variance</td><td>Classification</td><td>$\frac{1}{n} \sum_{i=1}^{N} (x_i - \mu)^2$</td><td>$y_i$ is label for an instance, $N$ is the number of instances and $\mu$ is the mean given by $\frac{1}{N} \sum_{i=1}^n x_i$.</td>
28
+
<td>Variance</td><td>Regression</td><td>$\frac{1}{n} \sum_{i=1}^{N} (x_i - \mu)^2$</td><td>$y_i$ is label for an instance, $N$ is the number of instances and $\mu$ is the mean given by $\frac{1}{N} \sum_{i=1}^n x_i$.</td>
29
29
</tr>
30
30
</tbody>
31
31
</table>
@@ -57,16 +57,20 @@ The recursive tree construction is stopped at a node when one of the two conditi
57
57
58
58
### Practical Limitations
59
59
60
-
The tree implementation stores an Array[Double] of size *O(#features \* #splits \* 2^maxDepth)* in memory for aggregating histograms over partitions. The current implementation might not scale to very deep trees since the memory requirement grows exponentially with tree depth.
61
-
62
-
Please drop us a line if you encounter any issues. We are planning to solve this problem in the near future and real-world examples will be great.
60
+
1. The tree implementation stores an Array[Double] of size *O(#features \* #splits \* 2^maxDepth)* in memory for aggregating histograms over partitions. The current implementation might not scale to very deep trees since the memory requirement grows exponentially with tree depth.
61
+
2. The implemented algorithm reads both sparse and dense data. However, it is not optimized for sparse input.
62
+
3. Python is not supported in this release.
63
+
64
+
We are planning to solve these problems in the near future. Please drop us a line if you encounter any issues.
63
65
64
66
## Examples
65
67
66
68
### Classification
67
69
68
-
The example below demonstrates how to load a CSV file, parse it as an RDD of LabeledPoint and then perform classification using a decision tree using Gini index as an impurity measure and a maximum tree depth of 5. The training error is calculated to measure the algorithm accuracy.
70
+
The example below demonstrates how to load a CSV file, parse it as an RDD of `LabeledPoint` and then perform classification using a decision tree using Gini impurity as an impurity measure and a maximum tree depth of 5. The training error is calculated to measure the algorithm accuracy.
69
71
72
+
<divclass="codetabs">
73
+
<divdata-lang="scala">
70
74
{% highlight scala %}
71
75
import org.apache.spark.SparkContext
72
76
import org.apache.spark.mllib.tree.DecisionTree
@@ -94,12 +98,16 @@ val labelAndPreds = parsedData.map { point =>
94
98
val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / parsedData.count
95
99
println("Training Error = " + trainErr)
96
100
{% endhighlight %}
101
+
</div>
102
+
</div>
97
103
98
104
### Regression
99
105
100
-
The example below demonstrates how to load a CSV file, parse it as an RDD of LabeledPoint and then perform regression using a decision tree using variance as an impurity measure and a maximum tree depth of 5. The Mean Squared Error is computed at the end to evaluate
106
+
The example below demonstrates how to load a CSV file, parse it as an RDD of `LabeledPoint` and then perform regression using a decision tree using variance as an impurity measure and a maximum tree depth of 5. The Mean Squared Error (MSE) is computed at the end to evaluate
101
107
[goodness of fit](http://en.wikipedia.org/wiki/Goodness_of_fit).
102
108
109
+
<divclass="codetabs">
110
+
<divdata-lang="scala">
103
111
{% highlight scala %}
104
112
import org.apache.spark.SparkContext
105
113
import org.apache.spark.mllib.tree.DecisionTree
@@ -126,4 +134,6 @@ val valuesAndPreds = parsedData.map { point =>
0 commit comments