spark-instrumented-optimizer/docs/ml-decision-tree.md
sachin aggarwal 51d41e4b1a [SPARK-11552][DOCS][Replaced example code in ml-decision-tree.md using include_example]
I have tested it on my local, it is working fine, please review

Author: sachin aggarwal <different.sachin@gmail.com>

Closes #9539 from agsachin/SPARK-11552-real.
2015-11-09 14:25:42 -08:00

5.9 KiB

layout title displayTitle
global Decision Trees - SparkML <a href="ml-guide.html">ML</a> - Decision Trees

Table of Contents

  • This will become a table of contents (this text will be scraped). {:toc}

Overview

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 features, extend to the multiclass classification setting, do not require feature scaling, and are able to capture non-linearities and feature interactions. Tree ensemble algorithms such as random forests and boosting are among the top performers for classification and regression tasks.

MLlib supports decision trees for binary and multiclass classification and for regression, using both continuous and categorical features. The implementation partitions data by rows, allowing distributed training with millions or even billions of instances.

Users can find more information about the decision tree algorithm in the MLlib Decision Tree guide. In this section, we demonstrate the Pipelines API for Decision Trees.

The Pipelines API for Decision Trees offers a bit more functionality than the original API. In particular, for classification, users can get the predicted probability of each class (a.k.a. class conditional probabilities).

Ensembles of trees (Random Forests and Gradient-Boosted Trees) are described in the Ensembles guide.

Inputs and Outputs

We list the input and output (prediction) column types here. All output columns are optional; to exclude an output column, set its corresponding Param to an empty string.

Input Columns

Param name Type(s) Default Description
labelCol Double "label" Label to predict
featuresCol Vector "features" Feature vector

Output Columns

Param name Type(s) Default Description Notes
predictionCol Double "prediction" Predicted label
rawPredictionCol Vector "rawPrediction" Vector of length # classes, with the counts of training instance labels at the tree node which makes the prediction Classification only
probabilityCol Vector "probability" Vector of length # classes equal to rawPrediction normalized to a multinomial distribution Classification only

Examples

The below examples demonstrate the Pipelines API for Decision Trees. The main differences between this API and the original MLlib Decision Tree API are:

  • support for ML Pipelines
  • separation of Decision Trees for classification vs. regression
  • use of DataFrame metadata to distinguish continuous and categorical features

Classification

The following examples load a dataset in LibSVM format, split it into training and test sets, train on the first dataset, and then evaluate on the held-out test set. We use two feature transformers to prepare the data; these help index categories for the label and categorical features, adding metadata to the DataFrame which the Decision Tree algorithm can recognize.

More details on parameters can be found in the Scala API documentation.

{% include_example scala/org/apache/spark/examples/ml/DecisionTreeClassificationExample.scala %}

More details on parameters can be found in the Java API documentation.

{% include_example java/org/apache/spark/examples/ml/JavaDecisionTreeClassificationExample.java %}

More details on parameters can be found in the Python API documentation.

{% include_example python/ml/decision_tree_classification_example.py %}

Regression

The following examples load a dataset in LibSVM format, split it into training and test sets, train on the first dataset, and then evaluate on the held-out test set. We use a feature transformer to index categorical features, adding metadata to the DataFrame which the Decision Tree algorithm can recognize.

More details on parameters can be found in the Scala API documentation.

{% include_example scala/org/apache/spark/examples/ml/DecisionTreeRegressionExample.scala %}

More details on parameters can be found in the Java API documentation.

{% include_example java/org/apache/spark/examples/ml/JavaDecisionTreeRegressionExample.java %}

More details on parameters can be found in the Python API documentation.

{% include_example python/ml/decision_tree_regression_example.py %}