Skip to content

Commit 72e4804

Browse files
committed
one pass over tree guide
1 parent 64f8995 commit 72e4804

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

docs/mllib-decision-tree.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ layout: global
33
title: MLlib - Decision Tree
44
---
55

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.
77

88
## Basic Algorithm
99

1010
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$`.
1111

1212
### Node Impurity and Information Gain
1313

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).
1515

1616
<table class="table">
1717
<thead>
1818
<tr><th>Impurity</th><th>Task</th><th>Formula</th><th>Description</th></tr>
1919
</thead>
2020
<tbody>
2121
<tr>
22-
<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>
2323
</tr>
2424
<tr>
2525
<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>
2626
</tr>
2727
<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>
2929
</tr>
3030
</tbody>
3131
</table>
@@ -57,16 +57,20 @@ The recursive tree construction is stopped at a node when one of the two conditi
5757

5858
### Practical Limitations
5959

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.
6365

6466
## Examples
6567

6668
### Classification
6769

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.
6971

72+
<div class="codetabs">
73+
<div data-lang="scala">
7074
{% highlight scala %}
7175
import org.apache.spark.SparkContext
7276
import org.apache.spark.mllib.tree.DecisionTree
@@ -94,12 +98,16 @@ val labelAndPreds = parsedData.map { point =>
9498
val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / parsedData.count
9599
println("Training Error = " + trainErr)
96100
{% endhighlight %}
101+
</div>
102+
</div>
97103

98104
### Regression
99105

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
101107
[goodness of fit](http://en.wikipedia.org/wiki/Goodness_of_fit).
102108

109+
<div class="codetabs">
110+
<div data-lang="scala">
103111
{% highlight scala %}
104112
import org.apache.spark.SparkContext
105113
import org.apache.spark.mllib.tree.DecisionTree
@@ -126,4 +134,6 @@ val valuesAndPreds = parsedData.map { point =>
126134
}
127135
val MSE = valuesAndPreds.map{ case(v, p) => math.pow((v - p), 2)}.reduce(_ + _)/valuesAndPreds.count
128136
println("training Mean Squared Error = " + MSE)
129-
{% endhighlight %}
137+
{% endhighlight %}
138+
</div>
139+
</div>

0 commit comments

Comments
 (0)