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-classification-regression.md
-135Lines changed: 0 additions & 135 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,67 +240,6 @@ Here `$\mathop{sign}(\wv)$` is the vector consisting of the signs (`$\pm1$`) of
240
240
of `$\wv$`.
241
241
Also, note that `$A_{i:} \in \R^d$` is a row-vector, but the gradient is a column vector.
242
242
243
-
## Decision Tree Classification and Regression
244
-
245
-
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.
246
-
247
-
### Basic Algorithm
248
-
249
-
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$`.
250
-
251
-
#### Node Impurity and Information Gain
252
-
253
-
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).
<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>
262
-
</tr>
263
-
<tr>
264
-
<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>
265
-
</tr>
266
-
<tr>
267
-
<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>
268
-
</tr>
269
-
</tbody>
270
-
</table>
271
-
272
-
The *information gain* is the difference in the parent node impurity and the weighted sum of the two child node impurities. Assuming that a split $s$ partitions the dataset `$D$` of size `$N$` into two datasets `$D_{left}$` and `$D_{right}$` of sizes `$N_{left}$` and `$N_{right}$`, respectively:
For small datasets in single machine implementations, the split candidates for each continuous feature are typically the unique values for the feature. Some implementations sort the feature values and then use the ordered unique values as split candidates for faster tree calculations.
281
-
282
-
Finding ordered unique feature values is computationally intensive for large distributed datasets. One can get an approximate set of split candidates by performing a quantile calculation over a sampled fraction of the data. The ordered splits create "bins" and the maximum number of such bins can be specified using the `maxBins` parameters.
283
-
284
-
Note that the number of bins cannot be greater than the number of instances `$N$` (a rare scenario since the default `maxBins` value is 100). The tree algorithm automatically reduces the number of bins if the condition is not satisfied.
285
-
286
-
**Categorical Features**
287
-
288
-
For `$M$` categorical features, one could come up with `$2^M-1$` split candidates. However, for binary classification, the number of split candidates can be reduced to `$M-1$` by ordering the categorical feature values by the proportion of labels falling in one of the two classes (see Section 9.2.4 in [Elements of Statistical Machine Learning](http://statweb.stanford.edu/~tibs/ElemStatLearn/) for details). For example, for a binary classification problem with one categorical feature with three categories A, B and C with corresponding proportion of label 1 as 0.2, 0.6 and 0.4, the categorical features are orded as A followed by C followed B or A, B, C. The two split candidates are A \| C, B and A , B \| C where \| denotes the split.
289
-
290
-
#### Stopping Rule
291
-
292
-
The recursive tree construction is stopped at a node when one of the two conditions is met:
293
-
294
-
1. The node depth is equal to the `maxDepth` training paramemter
295
-
2. No split candidate leads to an information gain at the node.
296
-
297
-
### Practical Limitations
298
-
299
-
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.
300
-
301
-
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.
302
-
303
-
304
243
## Implementation in MLlib
305
244
306
245
#### Linear Methods
@@ -331,13 +270,6 @@ gradient descent primitive in MLlib, see the
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.
val data = sc.textFile("mllib/data/sample_tree_data.csv")
450
-
val parsedData = data.map { line =>
451
-
val parts = line.split(',').map(_.toDouble)
452
-
LabeledPoint(parts(0), Vectors.dense(parts.tail))
453
-
}
454
-
455
-
// Run training algorithm to build the model
456
-
val maxDepth = 5
457
-
val model = DecisionTree.train(parsedData, Classification, Gini, maxDepth)
458
-
459
-
// Evaluate model on training examples and compute training error
460
-
val labelAndPreds = parsedData.map { point =>
461
-
val prediction = model.predict(point.features)
462
-
(point.label, prediction)
463
-
}
464
-
val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / parsedData.count
465
-
println("Training Error = " + trainErr)
466
-
{% endhighlight %}
467
-
468
-
#### Regression
469
-
470
-
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
471
-
[goodness of fit](http://en.wikipedia.org/wiki/Goodness_of_fit).
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.
7
+
8
+
## Basic Algorithm
9
+
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
+
12
+
### Node Impurity and Information Gain
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).
<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>
23
+
</tr>
24
+
<tr>
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
+
</tr>
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>
29
+
</tr>
30
+
</tbody>
31
+
</table>
32
+
33
+
The *information gain* is the difference in the parent node impurity and the weighted sum of the two child node impurities. Assuming that a split $s$ partitions the dataset `$D$` of size `$N$` into two datasets `$D_{left}$` and `$D_{right}$` of sizes `$N_{left}$` and `$N_{right}$`, respectively:
For small datasets in single machine implementations, the split candidates for each continuous feature are typically the unique values for the feature. Some implementations sort the feature values and then use the ordered unique values as split candidates for faster tree calculations.
42
+
43
+
Finding ordered unique feature values is computationally intensive for large distributed datasets. One can get an approximate set of split candidates by performing a quantile calculation over a sampled fraction of the data. The ordered splits create "bins" and the maximum number of such bins can be specified using the `maxBins` parameters.
44
+
45
+
Note that the number of bins cannot be greater than the number of instances `$N$` (a rare scenario since the default `maxBins` value is 100). The tree algorithm automatically reduces the number of bins if the condition is not satisfied.
46
+
47
+
**Categorical Features**
48
+
49
+
For `$M$` categorical features, one could come up with `$2^M-1$` split candidates. However, for binary classification, the number of split candidates can be reduced to `$M-1$` by ordering the categorical feature values by the proportion of labels falling in one of the two classes (see Section 9.2.4 in [Elements of Statistical Machine Learning](http://statweb.stanford.edu/~tibs/ElemStatLearn/) for details). For example, for a binary classification problem with one categorical feature with three categories A, B and C with corresponding proportion of label 1 as 0.2, 0.6 and 0.4, the categorical features are orded as A followed by C followed B or A, B, C. The two split candidates are A \| C, B and A , B \| C where \| denotes the split.
50
+
51
+
### Stopping Rule
52
+
53
+
The recursive tree construction is stopped at a node when one of the two conditions is met:
54
+
55
+
1. The node depth is equal to the `maxDepth` training paramemter
56
+
2. No split candidate leads to an information gain at the node.
57
+
58
+
### Practical Limitations
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.
63
+
64
+
## Examples
65
+
66
+
### Classification
67
+
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.
val data = sc.textFile("mllib/data/sample_tree_data.csv")
80
+
val parsedData = data.map { line =>
81
+
val parts = line.split(',').map(_.toDouble)
82
+
LabeledPoint(parts(0), Vectors.dense(parts.tail))
83
+
}
84
+
85
+
// Run training algorithm to build the model
86
+
val maxDepth = 5
87
+
val model = DecisionTree.train(parsedData, Classification, Gini, maxDepth)
88
+
89
+
// Evaluate model on training examples and compute training error
90
+
val labelAndPreds = parsedData.map { point =>
91
+
val prediction = model.predict(point.features)
92
+
(point.label, prediction)
93
+
}
94
+
val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / parsedData.count
95
+
println("Training Error = " + trainErr)
96
+
{% endhighlight %}
97
+
98
+
### Regression
99
+
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
101
+
[goodness of fit](http://en.wikipedia.org/wiki/Goodness_of_fit).
0 commit comments